In the past few days i have be implementing Localization for my web-application written in MVC. There are 3 main task that brake-down the Lozalization "Problem" that I could point out:
There are several methods to accomplish each task that I have explored and I'm struggling with choosing between them. There are two questions that I'm interested in:
Create a structured XML file that will contain a parent node for each key and childe nodes for each culture specific value e.g.
<string key="myAppName">
<heIL>some Hebrew value</heIL>
<enUS>some English value</enUS>
</string>
Here is where it gets interesting and sometimes complicated. There where two options that i have explored here and I'm not sure how to proceed.
<script>
tagFrom this point dynamically create an Array of keys, or static variables in java script and place the required string inside them. Here is an example using Razor engine.
<script type="text/javascript">
k_CultureInfo = "@CultureInfo.CurrentCulture.Name";
K_ApplicationName = "@MyStrings.SomeStringKey";
....
</script>
I found the idea in a blog pos here: Localize text in JavaScript files in ASP.NET Here
Resource files:
Use the resx format. Translation tools will not be able to process the format you proposed since it contains multiple languages in the same file. A typical translation tool would take a source file and generate a matching file for each language after translation. I would avoid the format that you suggested at all costs.
If you want to use an XML, use a single file per language. Also, whatever format you choose, make sure you include context comments for each segment to help translators.
Server Side Access
Use the resx and save yourself the time to define the layer
Client Side Access
Server side generated script is simpler and more consistent with the sever side access in my opinion.
Culture switching mechanism
I assume you are asking about the logic and not the UI. The most common method is to get the user's browser language setting. If the language is set to a language that does not exist in your application, fall back to your default language. You also want to set a cookie with the users selection (in case they change the language manually) so that they are presented with their language of choice next time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With