String as resource file name - c#

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.

Related

How to manually set language in ASP MVC without reconfiguring routing

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/

What is the best way to add i18n to an existing custom CMS system (build with C#)?

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.

ASP.Net Resource Strings in Link format display

The requirement goes this way. I have localized strings in my asp.net web application. I have a aspx page. Consider there is a string key-value pair in the resource file. The value is translated in different languages and available in different resource files. Ex: Strings.fr-FR.resx. Consider the value in resource file is "Hello World", that is translated to different languages.
In my aspx page, I want to retrieve the string from the resource file and display it in the page. But, I want "World" only to be in link format. How can i do it? If I display entire string in an anchor tag , then entire word "Hello World" would be in link format.
Again, my question is how to display only "World" in link format after retrieving from the resource file.
Thanks in advance.
Normally you can't do that, if you need a specific string to be localized and you create a resource element for that purpose, than you should consider it as an atomic element. Therefore: create a hello resource item.
If you really need to split that string you can call .ToString(), then .Split() on whitespace and finally take the second element with [1].
But I don't advise you to do this because some languages can have the word corrisponding to world as first word or even using a different number of words to say hello world. Even if you have only N languages, all translating 'hello world' with 2 words and having world as second word, I'd not do that because you are creating a strong relation between string form and its semantic, which is a source of bugs in case of new supported languages or string change.

How to get value by giving the name from a resx file

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 :)

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?
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.

Categories