locked
Dynamic Localization using .resx files RRS feed

  • Question

  • User1358270947 posted

     Hi all

    I am currently looking into ways of localizing a web application, and providing the means for users to add their own language. Imaging a list of english strings on one side, and a list of text boxes on the other where the user will fill in the equivilant in their own language.

    What I want to know is if its possible to add values to resource files and to then use those resources without having to rebuild the application? I want to include say 20 blank resource files, one for each of the languages we will support, the user then populates the text boxes and these values are inserted into the correct resource file, and the culture changed to this language.

    Can anyone see an issue with this route? I havent tested any code yet.

    Regards

    Endo

    Tuesday, June 2, 2009 5:42 AM

Answers

  • User-322463337 posted
    BTW, I've tested with <%= Resources.language.MyResourceName %> tags in the aspx file and I have set the CurrentCulture and CurrentUICulture of the current thread to "en-GB". It works on my side.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 2, 2009 10:41 AM

All replies

  • User-322463337 posted
    Hi Endurance Man, Actually, you CAN write into resource file at runtime by using the System.Resources.ResXResourceWriter class. The thing is that when you save the changes, the application will automatically rebuild, losing all contextual information (eventually, you will have to wait for the application to rebuild, then get directed to your default page). Is this acceptable? Take a look at this post for more details: http://forums.asp.net/t/1152145.aspx Best regards,
    Tuesday, June 2, 2009 7:47 AM
  • User1358270947 posted

     Thanks for the reply!

    I have managed to write to the resx files using the code from that link, but I can't seem to change the culture, here is my code:

     

    1    if (!Page.IsPostBack)
    2                {
    3                    CultureInfo culture_object = new CultureInfo("en-GB", true);
    4    
    5                    System.Threading.Thread.CurrentThread.CurrentCulture = culture_object;
    6    
    7                    System.Threading.Thread.CurrentThread.CurrentUICulture = culture_object;
    8                }
    

     

    I have 4 .resx files named: Default.aspx.de.-DE.resx / Default.aspx.en-GB.resx / Default.aspx.en-US.resx / Default.aspx.resx

    My web form uses the en-US resources no matter what I do. If I remove the US version it uses default, only if I remove both does it use the british version. Any ideas why the culture is not changing? I have removed culture="auto" and UICulture="auto" from my aspx page.

     Any help appreciated.

     

          

    Tuesday, June 2, 2009 10:24 AM
  • User1358270947 posted

     Thanks for the reply!

    I have managed to write to the resx files using the code from that link, but I can't seem to change the culture, here is my code:

     

    1    if (!Page.IsPostBack)
    2 {
    3 CultureInfo culture_object = new CultureInfo("en-GB", true);
    4
    5 System.Threading.Thread.CurrentThread.CurrentCulture = culture_object;
    6
    7 System.Threading.Thread.CurrentThread.CurrentUICulture = culture_object;
    8 }

     

    I have 4 .resx files named: Default.aspx.de.-DE.resx / Default.aspx.en-GB.resx / Default.aspx.en-US.resx / Default.aspx.resx

    My web form uses the en-US resources no matter what I do. If I remove the US version it uses default, only if I remove both does it use the british version. Any ideas why the culture is not changing? I have removed culture="auto" and UICulture="auto" from my aspx page.

     Any help appreciated.

     

          

    Tuesday, June 2, 2009 10:28 AM
  • User-322463337 posted
    Could you post all your code here? It might be easier for me to identify the sequence of events. :)
    Tuesday, June 2, 2009 10:37 AM
  • User-322463337 posted
    BTW, I've tested with <%= Resources.language.MyResourceName %> tags in the aspx file and I have set the CurrentCulture and CurrentUICulture of the current thread to "en-GB". It works on my side.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 2, 2009 10:41 AM
  • User1358270947 posted
    Here is the code for my aspx form. The 4 resx files contain all the controls on this form, im refreshing the page and using the code i posted previously to change the culture. There are a few controls on this page I was using for testing, just the labels at the bottom that should change.

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="LanugageChanger._Default"  meta:resourcekey="PageResource1"%>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div style="height: 481px">
            
            <asp:Label ID="Label1" runat="server" Text="Hello" 
                meta:resourcekey="Label1Resource1"></asp:Label>
                       
            <asp:TextBox ID="TextBox1" runat="server" meta:resourcekey="TextBox1Resource1"></asp:TextBox>
            <br />
            <asp:Label ID="Label2" runat="server" Text="Goodbye" 
                meta:resourcekey="Label2Resource1"></asp:Label>
                 
            <asp:TextBox ID="TextBox2" runat="server" meta:resourcekey="TextBox2Resource1"></asp:TextBox>
            <br />
            <asp:Label ID="Label3" runat="server" Text="Welcome" 
                meta:resourcekey="Label3Resource1"></asp:Label>
                 
            <asp:TextBox ID="TextBox3" runat="server" meta:resourcekey="TextBox3Resource1"></asp:TextBox>
            <br />
            <asp:Label ID="Label4" runat="server" Text="Terminate" 
                meta:resourcekey="Label4Resource1"></asp:Label>
                 
            <asp:TextBox ID="TextBox4" runat="server" meta:resourcekey="TextBox4Resource1"></asp:TextBox>
            <br />
            <asp:Label ID="Label5" runat="server" Text="End" 
                meta:resourcekey="Label5Resource1"></asp:Label>
                          
            <asp:TextBox ID="TextBox5" runat="server" meta:resourcekey="TextBox5Resource1"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Add new values" 
                meta:resourcekey="Button1Resource1" onclick="Button1_Click1" />
    
            <br />
            <asp:Button ID="Button2" runat="server" Text="Change language" 
                meta:resourcekey="Button2Resource1" onclick="Button2_Click" />
    
            <br />
            <br />
            <asp:Label ID="Label6" runat="server" meta:resourcekey="Label6Resource1" 
                Text="English 1"></asp:Label>
            <br />
            <asp:Label ID="Label7" runat="server" meta:resourcekey="Label7Resource1" 
                Text="English 2"></asp:Label>
            <br />
            <asp:Label ID="Label8" runat="server" meta:resourcekey="Label8Resource1" 
                Text="English 3"></asp:Label>
            <br />
            <asp:Label ID="Label9" runat="server" meta:resourcekey="Label9Resource1" 
                Text="English 4"></asp:Label>
            <br />
            <asp:Label ID="Label10" runat="server" meta:resourcekey="Label10Resource1" 
                Text="Default" />></asp:Label>
    
            <br />
            <asp:TextBox ID="TextBox6" runat="server" Height="153px" Width="543px"></asp:TextBox>
    
        </div>
        </form>
    </body>
    </html>
    
      
    Thursday, June 4, 2009 7:21 AM
  • User-529861215 posted

    Hi, i have face a problem which is in my page have many label.  have anyobe know that how to automatically re-assign the label text (change the language) without do anything at backend.

    Wednesday, March 24, 2010 6:33 AM
  • User-1200560727 posted

    Hi,

    Please go through my below post:

    http://forums.asp.net/t/1526291.aspx

    I hope it will resolve your problem.

    Regards,

    Tapan

    Wednesday, March 24, 2010 8:52 AM