1

I have several projects in a solution in visual studio 2010. This is the structure:

alt text

In the WebSite project called TerapiaFisica i'm using localResources for multilanguage support, and that's ok.
My problem is in the ModelosEF folder where i have several entityFramework projects.
Here's exactly my problem:

public void ModificarProducto(Dictionary<string, string> valores)
    {
        #region Obtencion de Datos y Validaciones de estos
        int productoID;
        try
        {
            productoID = Convert.ToInt32(valores["ProductoID"]);
        }
        catch (Exception)
        {
            throw new Exception("Debe seleccionarse una Categoría válida.");
        }

in this method i'm using plain spanish in the exception, i need a way of changing that text to english in the simpliest way possible if the lang is set to EN (Productos.aspx?lang=EN).
How can i do that? How can i know which language is being used in the Website so i can use the aproppiate language in the ModeloInventarioLogica.cs class? Should i send the current culture by parameter to know wich is? and if so, how can i use Visual C# to pick the corresponding language text from where?
A lot of questions but only one matter, make a class project multilanguage!
Thank you very much.

3 Answers 3

2

As mentioned in other responses, you can use the ResourceManager to get an appropriately localized string to display. To answer your other question... ASP.NET will pick up the current preferred culture that is sent with the browser in the Accept-Language header. For example, my browser sends:

Accept-Language: en-ca,en-us;q=0.7,en;q=0.3

Use Canadian English if available, otherwise fallback to US English, then English. (This could equally have been Turkish, French, Zulu, then English.) This fallback behaviour is specified in your browser's options. In FireFox, Options... Content... Languages... Choose...

As I mentioned, ASP.NET detects this choice automatcally and sets the current culture (and UI culture) of the request processing thread appropriately. The problem you have to consider is what if you want to provide an option on your site to switch between languages? You probably don't want to force users to change the default language for their browser. You need to provide a button, dropdown list, or other mechanism that allows your user to select a language. You then need to store that choice in a cookie, on the querystring, in a hidden form variable, or some other place so that you can set it at the beginning of every request.

Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedCulture, false);
Sign up to request clarification or add additional context in comments.

1 Comment

thank you I´ve already done that in my webapplication and it works like a charm.
2

In your C# function, the follow variable exceptionMessage :

string exceptionMessage = ResourcesNameSpace.ResourceFileName.strYouMustSelectAValidCategory;

can assume the correct value (the spanish or the english text), if you have the key "strYouMustSelectAValidCategory" in both files ResourceFileName.resx and ResourceFileName.en-US.resx.

So, you can then write:

throw new Exception(exceptionMessage);

The application will "know" the current culture in runtime :)

To set the current culture in your application, you can use, say, a link button with code-behind:

// Changes the CurrentUICulture of the current thread:
Thread.CurrentThread.CurrentUICulture = new CultureInfo( "en-US", false );

(source: http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentuiculture.aspx)


To add resource files to the project, you can right click your project icon (ModeloInventario), select "Add New Item", select "Resource file" and type a name for your file (I'm using vs2008, hope it isn't too different!).

Your default Resource file can be named: MIResources.resx

Then, you add another one, and name it: MIResources.en-US.resx, for english... MIResources.fr for french... And so on, one file for each additional language you would like (Language codes here: http://msdn.microsoft.com/en-us/library/ms533052%28VS.85%29.aspx).

The default resource file is the default place where a value is searched when the item is not present in the current culture resource file or when there is no resource file for the current culture.

You then start by adding the keys and values to your default resource file (strYouMustSelectAValidCategory: Debe seleccionarse una Categoría válida.), and then add the same keys to the MIResources.en-US.resx with the transation (strYouMustSelectAValidCategory: You must select a valida category.)... So, same keys, different values.

After all this, you can get a key's (string) value anywhere in the application, using:

string str = ResourcesNameSpace.MIResources.strYouMustSelectAValidCategory;

The ResourcesNameSpace is the namespace that's created with the resource file, if any! You can check it or edit it in the code-behind of the default resource file... If none, you don't need to use it (as long as you get to your MIResources.strYouMustSelectAValidCategory).

3 Comments

hello naruu ! that's exactly what i want to do but this line ResourcesNameSpace.ResourceFileName.strYouMustSelectAValidCategory; i don't know how to accomplished that! where do I place my resx files in ModeloInventario project (see image in my question) In the root? also ResourceNamespace I don´t know wich is! Thanks for your help.
Just want you to remember ModeloInventario is not a WebSite project is a class project
I added some info about resx files to my response, not sure it's the same in vs2010 (I'm using vs2008)... Hope it helps though! :) And yes, actually I was thinking "web application", but you can also add .resx files to a class library, so I believe it will work on your project too.
1

Use the ResourceManager to get the String from Resource XML file.

2 Comments

i was a little skeptical about using ResourceMananger because MSDN documentation didn´t convince me. I search for examples and i found out is extremely simple. Thank you xandy.
what i did was this: ResourceManager resourceManager = new ResourceManager("ModeloInventario.InventarioR", Assembly.GetExecutingAssembly()); and InventarioR is a resource wich is located in my ModeloInventario class project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.