locked
(ASP.NET) Where to load localization info. from? RRS feed

  • Question

  • User371911926 posted

    I am adding culture information to my web application for converting strings to the correct language.

    I am wondering where I should load this information from. I will have many pages. Right now I am loading all the information from the page_load event. I'm wondering if there is a better place to load the data from.

    It seems like anytime someone clicks a button, etc. on my page that information has to be loaded, and I'm worried about a performance hit.

    Also, do I need threading to use the culture data? The article I used to learn about localization used threads but didn't explain why.

    My code is below just incase its needed.

    Thanks alot!

    [pre]

    private void Page_Load(object sender, System.EventArgs e)

    {

    SetLocalizationStrings();

    }

    private void SetLocalizationStrings()

    {

    Thread.CurrentThread.CurrentUICulture = new CultureInfo(

    ConfigurationSettings.AppSettings["CultureType"]);

    ResourceManager rm = new ResourceManager(

    "Server.Languages.AppStrings", typeof(regform).Assembly);

    labelLoginID.Text = rm.GetString("regform_loginID");

    labelPassword.Text = rm.GetString("regform_password");

    labelPassword2.Text = rm.GetString("regform_confirmPassword");

    buttonSubmit.Text = rm.GetString("regform_submitButton");

    buttonCancel.Text = rm.GetString("regform_cancelButton");

    buttonCheckLoginID.Text = rm.GetString("regform_checkLoginIDButton");

    }

    [/pre]

    Tuesday, January 3, 2006 8:56 AM

All replies

  • User-169647723 posted

    You may create a Base Page class and adding the localizing code (I have a much better code for Localization), then you need to inherits all pages in your application from that Base Page.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    Using caching will save performance.

    Tuesday, January 3, 2006 10:27 AM
  • User89244710 posted

    if you are not going to change the culture , you can put it in the not postback

     

    and reload it when your current culture changes,

     

    or put it in the application begin reguest to get the current culture in every page and load it ,

     

    thnx

    Tuesday, January 3, 2006 12:41 PM
  • User371911926 posted

    Thanks for the replies.

    All I know now is this resource tool in visual studio has some major issues. It's not working correctly. I have added the resource files to my applicaiton, and they are set as "Embeded Resource" for the build option. I'm not sure what that means.

    Anyway, they were working fine. Because it pulls the data out of the files for the language, however I modified one of the files, saved the changes and recompile but the changes never reflect. I have deleted all the resource files in my project and started over from scratch and deleted all the bin and obj directories, however its still pulling data from files that dont even exist anymore.

    I have deleted all the cache/temp files, etc... in IE but nothing will get rid of these values. I don't understand it because the files dont even exist anymore. How can it pull data from resource files that dont even exist...

    I have rebuilt everything and something is majorly wrong. I think I may just use XML and XMLReader to build my own resource files because the ones that come with visual studio are not working.

    Tuesday, January 3, 2006 9:41 PM
  • User-169647723 posted

    "Embedded Resource" means that this file will be compiled with the assembly and you can use it within your code. I don't prefer to use this way for many reasons, one of them is what if you deployed your application on the production machine then you figure out that one resource string is missing or incorrect ? You may need to rebuild the application and re-deploy it again.

    Wednesday, January 4, 2006 1:27 AM
  • User371911926 posted

    Hmm... this code I am using must cache the information on the server itself some how. I deleted the bin/obj directories many times so I know it wasn't stored in the assembly file itself.

    After playing with this for several hours I just decided to restart then entire server completely, and BAM! problem was fixed.

    I don't understand why the machine needed to be restarted, something must have still been in RAM from the old version of the resource files and the new ones wern't taking effect.

    Can anyone explain why this happens? Is that code I am using storing something in RAM that's not being deleted? Is there a better code example I should use instead?

    Thanks!

    Wednesday, January 4, 2006 1:47 AM
  • User371911926 posted

    "Embedded Resource" means that this file will be compiled with the assembly and you can use it within your code. I don't prefer to use this way for many reasons, one of them is what if you deployed your application on the production machine then you figure out that one resource string is missing or incorrect ? You may need to rebuild the application and re-deploy it again.

    I guess I don't know what other options I have then. I tried using just a plain text file and using the resgen tool to compile a seperate file. But then I have to use the IReasourceReader and it doesn't have a built in way to get a single string. I didn't see a "GetString("")" method.

    So working with the embedded resource it seems a little easier.

    Wednesday, January 4, 2006 4:46 AM
  • User371911926 posted

    I have found a solution!

    I have a much better way of doing this. I just create the plain text file and compile it manually with resgen.

    Then I use the following code, and it works correctly. I can also change the file if I need to later because its not embeded.

    I just set the RR to read from my web.config file in my real application, this way the user can specify the correct resource file to use in case they dont want english, or there are any spelling mistakes in it.

    [pre]

    IResourceReader myReader = new System.Resources.ResourceReader("en-US.resources");

    IDictionaryEnumerator er = myReader.GetEnumerator();

    NameValueCollection nvc = new NameValueCollection();

    while(er.MoveNext())

    {

    //Console.WriteLine(er.Key + " " + er.Value);

    nvc.Add(er.Key.ToString(), er.Value.ToString());

    }

    label1.Text = nvc.Get("regform_loginID");

    myReader.Close();

    [/pre]

    Wednesday, January 4, 2006 5:09 AM
  • User89244710 posted

    try this post , it will help you

    http://forums.asp.net/911614/ShowPost.aspx

    Wednesday, January 4, 2006 6:02 AM
  • User371911926 posted

    try this post , it will help you

    http://forums.asp.net/911614/ShowPost.aspx

    Hmm that is a thread about VS05, I don't have that. I am using VS03, sorry for the confusion.

    In any case, I have the problem resolved now.

    Thanks!

    Wednesday, January 4, 2006 9:29 AM
  • User1183903743 posted

    I just create the plain text file and compile it manually with resgen...
    I don't see how that is different from out of the box v1.x ResourceManager, code and all.

    However not embedding the resources into sat assemblies will bite you if you deploy a heavily trafficked site.

    Wednesday, January 4, 2006 2:23 PM
  • User371911926 posted

    I just create the plain text file and compile it manually with resgen...
    I don't see how that is different from out of the box v1.x ResourceManager, code and all.

    However not embedding the resources into sat assemblies will bite you if you deploy a heavily trafficked site.

    So what is the difference between the "en-US.resources.dll" file that VS generates, and the "en-US.resources" file that rengen.exe compiles? To me it just looks like resgen doesn't add the .dll extension.

    Are they actually different file formats?

    The ResourceManager doesn't work for me for a few different reasons, mainly because its using the Threading class, and since I only want one resource file for my solution which contains multiple projects, its not working correctly.

    I like this way because then I can just drop the resource file in the bin directory and it works correctly.

    I'm not sure how embeded resources work, but if I generated my file with resgen, then added it to my solution and on the files property page clicked "Embeded Resource" would this make a difference in speed?

    I guess it would be the same thing as ResourceManager does, without actually using the ResourceManager class.

    Thursday, January 5, 2006 3:29 AM
  • User371911926 posted

    Thanks,

    I figured out how to fix it and still use the embeded resource. I just put my resource base name and culture type into my web.config file, and made a few other modifications and its working now, actually it wasnt that hard once I modified the resource manager to pull the data from a config file.

    Now I can use the .resx files, and VS makes them an embeded resource so its much smoother now.

    Its also much easier to edit the language strings, because VS gives you a nice grid UI to modify them with, after you have a few hundered strings in a plain text file with no formatting, its very hard to read the file and make changes, or notice errors. It was giving me a headache.

    Thursday, January 5, 2006 5:00 AM
  • User1183903743 posted
    why .config and not just any xml?
    Thursday, January 5, 2006 1:31 PM
  • User371911926 posted

    why .config and not just any xml?

    Web.config is better suited for storing configuration data then plain XML, hince its a config file. .Net Framework has special classes designed for reading information inside appSetting tags in config files, much faster, and with less code then reading XML files.

    You can read data from the config file with 1 statement, that is optimized for pulling the data out quickly,

    string myString = ConfigurationSettings.AppSetting["MyDataKey"];

    To perform this same statement with XML would take several lines at least, you'll need XMLReaders, etc.

    That single statement is all you need to pull data from a XML formatted config file. No messing with XMLDocuments, XMLReaders, Nodes, XPath, etc...

    Also, since every web application by default includes a predefined config file, an XML file would be just one more file I would have to keep track of, and one more file the user would have to locate and edit.

    Thursday, January 5, 2006 2:31 PM
  • User1183903743 posted

    You are correct about it having an API but a config file was meant to be for admin purposes. So your keys and data would be exposed to possible inadvertent intervention long after the dev stage.

    You can easily wrap a dedicated reader to cut down, the not so chatty to start with, xmldom to single line methods.

    HTH

    Thursday, January 5, 2006 6:11 PM
  • User371911926 posted

    You are correct about it having an API but a config file was meant to be for admin purposes. So your keys and data would be exposed to possible inadvertent intervention long after the dev stage.

    You can easily wrap a dedicated reader to cut down, the not so chatty to start with, xmldom to single line methods.

    HTH

    I'm not sure I understand what you ment. I want the Admin to be able to change information, this is why its in the config file. If the admin wants to use a different culture setting for the program, they just change the culture type in the config file to what ever they want to use.

    There is a special tag called <appSettings> which can be used for storing this stuff inside it.

    I am still wondering what the threading statement is for in my code example. I don't understand why that line is needed, I have commented out that line and the language strings still work.

    Thursday, January 5, 2006 6:43 PM