Thanks for the advice. I've decided to go down the .resx route, as it seems the most intuitive.
I've created one .resx file (langEn.resx) and started putting the strings in. I can retrieve them with no problems at all directly in the pages by accessing Resources.langEn.<string name>
This is enough for now, as I only have English to worry about. But in the future I would have to change "langEn" all through the site.
How can I dynamically change the resource file it looks at? Ideally I'd like to be able to set a variable somewhere (in Session_Start perhaps, from a cookie) which contains the name of the resource for that language (so "langEn" or "langDe" or "langFr") and then access it all through the application.
In PHP syntax (the dynamic naming bit I mean) I would imagine it would have something like {$langauge} in place of the "langEn" but I don't know how to create a dynamic object (is that even the right terminology?) in C#.
Thanks for your help so far; hopefully this will be the last bit for this question ;-)
Watch your naming conventions.
Your resources should be named in the following format:
nameOfResource.LanguageCode.resx
eg:
- myResource.resx <--this would be the default resource
- myResource.fr.resx <--this would be the default French resource
- myResource.es.resx <---this would be the default Spanish resource
- myResource.en-CA.resx <--this would be for Canadian English
See
this article for more information on naming conventions and packaging your resources and see
this article for a list of cultural/language codes.
If you set your page directive's UICulture to "auto" the ASPX page will automatically use the resource that matches the preferred culture set in the user's browser.
eg:
- <%@ Page .... Culture="en-US" UICulture="auto"%>
The above page directive sets the culture to default to US English and sets the page to automatically detect the user's browser's cultural settings.
If you want the user to be able to choose which language resource to use you will have to override the InitializeCulture method in your aspx page (you cannot override this in your master page).
In the InitializeCulture method you need to set the Thread.CurrentThread.CurrentUICulture and Thread.CurrentThread.CurrentCulture to the language/culture that the user selected.
You'll have to override this in every aspx page in your application (note that the InitializeCulture occurs before your page is loaded but after session is loaded so you can store the user's cultural settings in Session if you like....by the way, information stored in Session for a user is normally referred to as persistent data...)
See
this article for more information on the InitializeCulture method and how to implement Globalization into your asp.net application.
Cheers!
-Frinny