ASP.NET 2.0 Localization (Video, Whitepaper, and Database Provider Support)

ASP.NET 2.0 and VS 2005 add a bunch of features that make localizing ASP.NET applications much easier.

To learn more about how the localization features work, I'd recommend first checking out this excellent 13 minute video in the ASP.NET 2.0 "How Do I?" video series.  In it Scott Stansfield walks-through how to build and localize an ASP.NET application from scratch, and how to support both dynamically picking the language used based on the incoming user-agent string of the client, as well as allowing a user to explictly choose their language preference from a dropdownlist.  I think you will walk away being surprised at how easy it is.  

Wei-Meng Lee has also written a great walkthrough article on ASP.NET 2.0 localization that you can read for free on the O'Reilly network hereFor more detailed information on ASP.NET 2.0 localization, you can also then read Michele Bustamente's excellent ASP.NET 2.0 Localization article on MSDN.  Bilal Haidar also has a great artlce on ASPAlliance here.

The articles and videos above use XML .resx files to store localized resource strings.  These can either by compiled into binaries or deployed as XML source with an ASP.NET 2.0 application.  Jeff Modzel recently published an article that shows how to build a custom Resource Provider that stores the resource strings in a database instead.  You can read about how he built this as well download his sample code here.

Hope this helps,

Scott

 

38 Comments

  • I am currently working on a white paper that will explain how to make resource changes in production while the site is precompiled (something that every serious web application will need).

    I think that such info should have been arrived from the MSDN.

    Thanks

  • Great video. Is there any "smart" way to handle this using Masterpages/ContentPlaceHolders. All my controls are renamed to something I don't quite know until runtime (like "ctl00_ContentPlaceHolder1_Button1" instead of just "Button1" as in the video)

  • Hi Scott!



    I am exactly at this moment searching on localization!!



    Tks!

  • Hi Peter,

    If you are using VS 2005, you can use the "Generate Local Resources" option within the Tools menu to auto-generate the .resx files for resources. Note that the control name you use is different from the client-side ID that is generated.

    Hope this helps,

    Scott

  • Hi Joe,

    The good news is that you can actually do this already. :-)

    If you use the VS 2005 Web Application Project option, you can place .resx files either in the compiled assembly, or within the app_localresources or app_globalresources folders. When you place them within the folders then they will by default stay as XML files (and not be compiled in). You can then reference these from your pages as well as by the code-behind files themselves.

    You can lookup the resource class names to reference from your code-behind by opening the .designer file below each .resx files in the app_localresources directory. This will give you a strongly typed API you can use from your compiled code to reference it. You can then deploy the .resx file as XML files and allow users to customize later.

    Hope this helps,

    Scott

  • Awesome! I was under the impression that putting them in those folders would cause them to be compiled into the assembly. You are saying it won't do that? I'm not sure I understand entirely, do I need to mark them as content only instead of embedded resource? Or is it that it will compile them but if those folders and the .resx files are deployed they will take precedence over the versions in the assembly? Thanks Again for your help! Joe

  • Hi Joe,

    If you mark them as content and keep them within the app_localresources or app_globalresources folder, then they won't be compiled into the project assembly. Instead they'll be compiled dynamically by ASP.NET at runtime.

    Hope this helps,

    Scott

  • Localization features are great but I have a question about updates. I have generated local resource file for a page. This works fine. How to update the generated local resource file on changing the page contents? Can I update the contents automatically or does it need to be done manually?

  • Hi Dee,

    When you add new controls or content to a ASP.NET page in VS, you need to either manually update the .resx files with the new entries, or re-run the local resources generator in order to re-gen the file.

    Hope this helps,

    Scott

  • Nice articles BY Wei-Meng Lee.

  • I am using String Resource file. The changes I make in resx is not reflecting if I save the resx with changes. It works only when i compile the application. The application is now in the production server and I cannot update the compiled code often there.

    Your valuable suggestion will be appreciated.

    If possible, please reply to gowthami_sharma@yahoo.com

    Thanks

  • Hi Sai,

    Can you send me email (scottgu@microsoft.com) describing the issue more? I can then loop in more folks to help.

    Thanks,

    Scott

  • Hi scott,

    Thanks for your reply. I have sent an email to you.


    Thanks
    Sai(Gowthami)

  • Hi Scott,
    I am using resources in a DLL. I created a reference to the resource DLL in my project. I could access them via aspx.cs files. But if I try to use them in aspx files using the <%Resources: syntax I am unable to get anything. Do you see a problem? (I am using import statement in aspx file to import the resource namespace). I am using Visual Studio 2005 team suite.

    Please help me out.
    Thanks

  • Hi Siva,

    Can you send me email with more details about your project and the issue you are running into? I can then loop you in with a few folks.

    Thanks,

    Scott

  • I need information on how to make resource changes in production while the site is precompiled. It will be a great help if anyone can point me to how to do that. Thanks.

  • If anyone found how to deploy only the resources while the site is pre-compiled, it would be a great help. I found that I can compile the whole project and just copy the resource dlls to the target and it works. But is there a way to create just the resource dll?

  • Hi Scott,

    I have a query: In the WAP model, we can edit the RESX files on the production server and the compiler will dynamically generate the class for the changed file, but does this mean that the entire application will be restarted? If yes, then do we need to maintain the resources in a separate project so that we can recompile the dll and update the dll on the fly. The application will pick up the new dll and continue to work.

    Please shed some light on this as I have read in MSDN that the application restart will happen if we change resource files or add new files.

    Thanks,
    Vivek

  • Hi Vivek,

    If you modify the .resx live on the server it will restart the application (to avoid you displaying old cached values from them).

    To make it truly dynamic, then you will probably want to pull the resources data from a database.

    Hope this helps,

    Scott

  • Hey Scott,

    Thanks for the reply!

    But what if we do not deploy the .resx but only its assembly (which comes from a separate project created specially for resource files). If we want to update the resources, we do so in the .RESX files in the project, compile it and place the generated DLL in the deployed web app's bin folder.

    Will this avoid the App will restart? I think so as there would be no dynamic compilation of the .resx files. Is this the recommended approach if we do not want to use a database?

    Again, I really appreciate you taking time out to answer things! My heartiest thanks to you!

    Regards,

    Vivek

  • Hi Scott,

    Thanks for taking time out to answer my query. Actually I had sent another comment but it is not getting reflected even after a day. Just to be sure that it got sent properly I am posting it again:

    If we do not want to use a database and instead would create a separate class library project just having resource files. Then we can put these DLLs in the deployed web application's bin folder and it should pick up these new modified resource DLLs at runtime without restarting the app.

    Is this recommended approach or do you foresee any potential gotchas here?

    Thanks again for taking time out of your busy schedule.

    Regards,

    Vivek

  • Hi Vivek,

    You can deploy the resource strings compiled into an assembly. However, that will restart the application (basically any changes to the \bin directory underneath an application will do this).

    Hope this helps,

    Scott

  • Scott,

    Thanks a lot!

    Cheers,

    Vivek

  • Hi Kevin,

    Any chance you could send me your question in an email format? I unfortunately don't know the answer to it myself, but can loop in some people who do.

    Thanks,

    Scott

  • Hi Venu,

    Unfortunately I don't know the answer to that one off the top of my head. But if you send me email describing it in detail I'll be able to loop you in with a few folks on my team who can help.

    thx!

  • We are building a web site that we want to localize. We've had no problem creating the resource files, but having to manually enter the translated text into each file, but compare/update every resource file after changes are made to controls/pages is time consuming and prone to errors (typos, missing items, etc.) As anyone come up with a database-driven approach using resource files where the transations are stored in a database?

  • Hi Boo,

    Here are two links to database based localization samples for ASP.NET 2.0:

    http://www.leftslipper.com/ShowFaq.aspx?FaqId=9

    http://www.codeproject.com/aspnet/customsqlserverprovider.asp

    Hope this helps,

    Scott

  • Hi Jothi,

    When you update or add a new DLL to your application, it will cause the application to restart.

    This shouldn't cause users to log-out though if you are using Forms Authentication to track them. It will cause session state to be lost if you are using in-proc session state (the default). However, if you use the SQL Session State or StateServer option it will be preserved across application restarts.

    Hope this helps,

    Scott

  • Hi Scott,

    cool video! Is there any possibility to set the resourcekey from codebehind?

    I've tried to paste button1.Attributes.Add("meta:resourceKey", "button1") into OnInit section but it didn't work..

    I always want to set the same resourcekey as the name of the control and I would like to do it in the MasterPage because I don't want to set them individualy.

    Thanks!

    ap

  • Hi AP,

    I don't believe you can set the resourcekey from a code-behind, but you can access the resource value and set it yourself this way.

    Hope this helps,

    Scott

  • Hi Scott,
    I have been asked to prepare a current website for Internationalization. The current requirement is to have it capable of displaying in 4 - 6 languages. There are approx. 65 aspx pages, an Access DB of approx 70 tables and 150 queries. I have developed this website using VWD but with the requirement to Localize i am wondering whether or not to use Visual Studio 2005 Standard Edition. Could you perhaps explain whether or not the benefits of VS 2005's built in features of using localizaion are worth moving to VS 2005 or do you think that VWD would work just as well?
    thx,
    ciao, Dan

  • Hi Dan,

    VS 2005 Standard includes support for auto-generating the .resx files that contain localized resources from a .aspx page. So you might find this useful as you start to localize the site.

    Hope this helps,

    Scott

  • Hi Scott, Regarding VS 2005 Standards auto-generating of the .resx files, if one needs to be able to display the website in, for this example, 4 different languages, are you saying that VS 2005 Std Ed will auto generate 4 different .resx files for each .aspx page? I am trying to understand how much benefits going to VS 2005 Std Ed will bring in my situation.
    thx,
    ciao, Dan

  • Hi Dan,

    VS 2005 will generate one .resx file for you that has all of the english strings from the page encapsulated in the resource file. You can then copy this resource file and add one for each language you want to localize (replacing the strings).

    Hope this helps,

    Scott

  • Hi Scott
    Many thanks for the article. Useful as always and much appreciated.

    Currently i'm in the process of developing a website for a travel company. The company already has a SQL DB with the contents for the website, each language in a different table (contents, contents_EN, contents_ES and so on). Now here's the question: how can i pull the content from the correct table according to the selected language? I know that i could write all the SQL-stuff by myself, but i'd like to use the designer-generated datasets and table-adapters. Do you have any hint for me?

  • Hi Pete,

    Eilon has a good sample that you might want to check out here: http://www.leftslipper.com/ShowFaq.aspx?FaqId=9

    It describes how to store localized content within a database and dynamically pull it based on the locale of the client browser.

    Hope this helps,

    Scott

  • I hope someone can help me with this question...

    I read both Jeff Modzel and the "leftslipper" articles but i agree with another poster here that...Jeff Modzel's article doesnt show how to call the ResourceReader...

    Basic Question: how do I call this piece of code to localize, for example, 5 lables on a page??

  • Hi Andrew,

    Can you send me an email with this question? I can then loop you in with Eilon who should be able to help provide it.

    Thanks,

    Scott

Comments have been disabled for this content.