I have a form which is localized for the language English, my default language for the application is Dutch.
What I want is the following:
I want to get the value by giving the name (of a label) from a specific resx file (the Dutch version beneath my form)
Name Value
This because I want to get the lenght of the default language label text.
I am currently using this to translate my app to french...use ResourceManager
ResourceManager rm = Resources.fr.ResourceManager;
rm.GetString("your_var_name")
else use
ResourceManager rm = new ResourceManager (typeof(your_form_class))
rm.GetString("your_var_name")
Hope this will help you :)
Related
I need to solve the following problem. It seems simple but I'm stuck.
I'm writing a converter which takes as input the Geographic Region Code of a country and should return the name of the country in the display language of the application.
Example (target functionality)
Application language: "FR"
Input: "US"
Output: "États Unis"
To this end I use the following code.
public object Convert(object value, Type targetType, object parameter, string language)
{
string countryCode = (string)value;
var country = new Windows.Globalization.GeographicRegion(countryCode);
return country.DisplayName;
}
The problem with this is that it returns the country in the system wide selected language. For example.
Example 2
System Language: "en-US"
Application Language (set via PrimaryLanguageOverride) : "FR"
Input: "US"
Output: United States
I suspect that what happens is the Windows.Globalization.GeographicRegion class gets its culture data from the System wide settings and not from the Application culture settings .
Does anybody know a workaround for this?
Is there a way to set a different culture for the thread the converter will run in?
GeographicRegion.DisplayName can get a localized string that is suitable for display to the user for identifying the region. However this function requires corresponding language pack to be installed.
For example, to display the region in French, we need to install Français language pack by opening Settings → Time & Language → Region & language → Add a language.
Note that we don't need to set French as the default language, so the system language is still English.
After French language pack is installed, your code should be able to return the localized region name in French. For example, when you input "US", the output should be "États-Unis".
EDIT
The mistake was in the country code. Albania's country code is sq-AL, and not al-AL.
For a full list of country codes: http://timtrott.co.uk/culture-codes/
I don't need to have different routing for different languages.
All I need is by the click of a button to toggle between two languages, Albanian, and English.
What I can't figure out: how to change the language? I'm not worried on how to detect which language needs to be selected, but how to actually change the language.
I have these 2 resource files:
Resources.resx, Resources.al-AL.resx
Always the strings from Resources.resx are used, how can I change so that the strings from Resources.al-AL.resx are used?
I tried something like this:
//I'm just trying stuff I read here in SO, none worked so far.
Resource.Culture = CultureInfo.CreateSpecificCulture("al-AL");
HttpContext.Session["culture"] = "al-AL";
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("al-AL");
return RedirectToAction("Index", "Home");
And in my view I have this:
#Resource.Culture
#Html.Label(Resource.CustomerName, new { #class = "control-label"})
When I first open the view, the first line is empty, after selecting one of the buttons to change the language, I verify that the Resource's culture is being changed, but the text stays the same.
EDIT:
I even added this to web.config <globalization uiCulture="al-AL" culture="al-AL"/>
Custom folder to hold the resource files
Instead of using culture code al-AL I had to use sq-AL. For a full list of culture codes:
http://timtrott.co.uk/culture-codes/
I am currently responsible for an existing CMS, built a few years ago with C# .Net and a few clients are requesting to have their website in Spanish. The websites are pre-populated with pages from a library and currently the application has no support for i18n. What is the quickest way to allow the client to have his website in Spanish? And what is the best way to allow any language?
I understand those are two different answers, but I want to plan for future and at the same time provide a quick solution to please the clients.
The simplest i18n solutions usually involve using a string dictionary for any text in the program.
Instead of doing things like:
TextBox t = new TextBox();
t.Text = "Hello, user!";
You'd do something like:
TextBox t = new TextBox();
t.Text = i18nStringDictionary["HelloUserMessage"]; //some people prefer an enum to
//bare strings in this case
Where i18nStringDictionary is a global or semi global dictionary. Then, on app start up you can initialize it with the proper values from a file as dictated by a config file. You'll have one file for each language. Illustrating the above example, in your english file, you'd have:
HelloUserMessage, "Hello, user!"
//More message pairs in english
and in your spanish file:
HelloUserMessage, "Hola, usario!"
//More message pairs in spanish.
Finally, you'd parse these pairs into a dictionary type.
Of course, this is only if you want to roll your own, but it demonstrates what most i18n libraries do under the covers. Otherwise, you can use the built in features of C# (https://msdn.microsoft.com/en-us/goglobal/bb688096.aspx), or find plenty of third party tools.
In any case, it probably won't be quick at all, since it involves replacing all string literals in the site/program with references to some dictionary or library. Once it is done, though, you will only need to do the translation and set config file variable and you'll be good to go.
Final edit:
Since you are talking about a website, you probably have views or templates with text in them as well. These would generally also need to be replaced with references to a string library.
Just search for ASP.NET globalization and localization.
Resource Files
You could add resource files (RESX) for all static text used in the ASP.NET controls.
Here is an MSDN article even covering Spanish.
Creating a new RESX file in Visual Studio will give you a table view. Let's say you add Text.resx to the project. This will be the default language.
| Name | Value |
| HelloWorld | Hello World ! |
| WelcomeMessage | Welcome ! |
You can now reference the Names in the code behind:
Label label = new Label();
label.Text = Text.WelcomeMessage;
Or you can set the binding in the designer
<asp:Label ID="Button1" runat="server"
Text="<%$ Resources:Text, WelcomeMessage %>" />
Switching language
Now to add another language simply create a new file named Text.language-country.resx. So in your case Text.es-ES.resx.
Each entry you add here will overwrite the default language when you switch to Spanish. If it is missing, then the default value will be shown.
| Name | Value |
| WelcomeMessage | ¡Bienvenidos! |
You can switch languages by changing the culture property of the current thread
Thread.CurrentThread.CurrentUICulture =
new CultureInfo("es-ES");
Otherwise the language can be set depending on browser settings.
More info here: https://msdn.microsoft.com/en-us/library/ff647353.aspx
Translation
RESX files are actually XML documents that can be embedded into an assembly are referenced as content files.
So you could send the default resource file to a translation agency for them to edit. Just remind them not to translate any entries in the Name column.
I'm not sure of the terminology here, but I need to access a specific resource file who's name is the string that is calculated.
For example:
I have a resource file named "resFile-en-GB" I can access a string inside by using "resFile-en-GB.stringKey". Now I need to add the culture info to the end of a string.
string stringA = "resfile-";
string stringB = "en-GB";
string stringC = stringA + stringB;
Now I need to use "stringC" as the resource file name like "stringC.stringKey" How would I go about doing this?
you can access resources via the ResourceManager and then use GetString(key), probably easier in this case:
var resourceManager = new ResourceManager(stringC, Assembly.GetExecutingAssembly())
resourceValue = resourceManager.GetString(key);
You can make your application multi lingual with the use of resource files.
Add the resource files to your solution
That's the obvious first step. For example add a folder to your solution ResourceFiles and add the resx files:
LanguageStrings.resx
LanguageStrings-fr-FR.resx
LanguageStrings-nl-NL.resx
The LanguageStrings.resx will contain the default language, let's say english. The fr-FR will contain the French language and nl-NL the Dutch language. You can just add items to the resource files. For example HelloWorld = Hello world.
Right, so you added the HelloWorld key to your three language strings .resx files with the proper translations.
Set the culture
Somewhere in your application you want to be able to set the language. It could be in some event handler (a listbox or whatever you thought up). There you want to set the chosen culture info on the LanguageStrings.
var cultureInfo = new System.Globalization.CultureInfo("nl-NL");
System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
LanguageStrings.Culture = cultureInfo;
This sets the language hardcoded to the Dutch language. Obviously you want to make the "nl-NL" variable so that you can set the language to whatever language the user choose.
Get your localized strings
Finally you want to retrieve the localized strings from the resources files. For example something like:
label.Text = LanguageStrings.HelloWorld;
Here the magic happens. You don't have to define which language resource file should be used to retrieve the localized string. That is done for you because you already set the desired language to your resource file (LanguageStrings.Culture = ..).
That's the basics, I hope this helps you on your way.
ResourceManager.GetString method should help you achieving this.
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?
Yes! It's called resource (.resx) files. What you do is this:
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.
Create your program in one language, let's say English.
Next, change all your forms to another language like so:
Change the Language property of the form to the other language, let's say Spanish.
Change the text on all your controls. The designer will automatically generate a new .resx file for the language.
Swap back and forth as needed during development.
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!
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.
You can make a multilingual application in two ways:
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.
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.
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.
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:
Spanish: (filename.aspx.es-ES.resx)
English: (filename.aspx.resx)
.
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.