Shane Grant
Shane Grant

Reputation: 2654

How can I convert a C# .NET application to support multiple languages?

I have a C# application that I need to convert to support English and Spanish, is there a semi easy way to add that in and be able to add other languages later on?

Upvotes: 4

Views: 4415

Answers (4)

Dennis R
Dennis R

Reputation: 51

Without installing any 3rd party tool, APIs, or dll objects, I am able to utilize the App_LocalResources. Although I still use Google Translate for the words and sentences to be translated and copy and paste it to the file as you can see in one of the screenshots below (or you can have a person translator and type manually to add). In your Project folder (using MS Visual Studio as editor), add an App_LocalResources folder and create the English and other language (resx file). In my case, it's Spanish (es-ES) translation. See screenshot below. enter image description here

Next, on your aspx, add the meta tags (meta:resourcekey) that will match in the App_LocalResources. One for English and another to the Spanish file. See screenshots below:

enter image description here

Spanish: (filename.aspx.es-ES.resx)

enter image description here

English: (filename.aspx.resx)

enter image description here

.

Then create a link on your masterpage file with a querystring that will switch the page translation and will be available on all pages:

<%--ENGLISH/SPANISH VERSION BUTTON--%>
<asp:HyperLink ID="eng_ver" runat="server" Text="English" Font-Underline="false"></asp:HyperLink> | 
<asp:HyperLink ID="spa_ver" runat="server" Text="Español" Font-Underline="false"></asp:HyperLink>
<%--ENGLISH/SPANISH VERSION BUTTON--%>

.

On your masterpage code behind, create a dynamic link to the Hyperlink tags:

////LOCALIZATION
string thispage = Request.Url.AbsolutePath;
eng_ver.NavigateUrl = thispage;
spa_ver.NavigateUrl = thispage + "?ver=es-ES";
////LOCALIZATION

.

Now, on your page files' code behind, you can set a session variable to make all links or redirections to stick to the desired translation by always adding a querystring to urls.

On PageLoad:

        ///'LOCALIZATION
        //dynamic querystring; add this to urls ---> ?" + Session["add2url"]
        {
            if (Session["version"] != null)
            {
                Session["add2url"] = "?ver=" + Session["version"]; //SPANISH version
            }
            else
            {
                Session["add2url"] = ""; // ENGLISH as default
            }
        }
        ///'LOCALIZATION

. On Click Events sample:

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["FileName.aspx"] = null;
        Response.Redirect("FileName.aspx" + Session["add2url"]);
    }

I hope my descriptions were easy enough.

Upvotes: 0

Mr. Elusive
Mr. Elusive

Reputation: 496

You can make a multilingual application in two ways:

  1. By making the application Localizable, so when the user changes the culture of the device, the application will switch automatically to culture's UI if you added this language already to the supported languages in the application. You can perform this by setting each form's Localizable property on the project to Localizable, and then changing the UI to the new culture.

  2. By making a language option and a resource file (.resx) for each added language in your application, and depending on the selected language, you can load the images or the strings from selected language's resource file.

Upvotes: 0

ChrisWue
ChrisWue

Reputation: 19060

You mark all your forms and controls as localizable. This will put all UI related text (labels etc.) in resource files. If you need to create strings in code then you use string resource files and look up the string by the resource key (e.g StringResource.Get("My_Message")). Then you can use a tool to translate all your resources. Typically you create a localized .dll for each language. We use Passolo for that but there are other tools around.

Upvotes: 0

Ry-
Ry-

Reputation: 225174

Yes! It's called resource (.resx) files. What you do is this:

  1. Change the Localizable property of your localizable forms to true. This will make the designer fetch text and other properties from the .resx files instead of hard-coding them.
  2. Create your program in one language, let's say English.
  3. Next, change all your forms to another language like so:
    1. Change the Language property of the form to the other language, let's say Spanish.
    2. Change the text on all your controls. The designer will automatically generate a new .resx file for the language.
    3. Swap back and forth as needed during development.
  4. When publishing, go into your Assembly Settings and change the language. You can also change the language in code, I think.

And voilà! You're done!

Upvotes: 8

Related Questions