Reputation: 1202
A number of times I have needed to support multiple languages when building web sites but have yet to come up with a good way of passing the language strings from the server to the client so that they can be used in Javascript for dialogs, messages, etc.
What are your recommendations or experience with this?
EDIT: I'm leaning towards generating the javascript language files on the fly and would appreciate it if anyone knows any third party libraries that can do this.
Upvotes: 5
Views: 605
Reputation: 15856
I usually move all string specification to separate files and then include the appropriate one on the page, something like this:
In st/js/messages-ru.js:
hello = "Привет"
bye = "Пока"
In st/js/messages-en.js:
hello = "Hello!"
bye = "Good bye!"
When the page is generated, you can determine the language and insert the appropriate js file on the page.
Upvotes: 2
Reputation: 10874
If you're using the ASP.NET AJAX framework, the ScriptManager control contains various mechanisms for automatically generating javascript from localized .resx files, and sending the correct language to the user based on the standard CultureInfo settings that the rest of the website is using. See this MSDN article for a tutorial.
Upvotes: 0
Reputation: 20049
We pass an object to the JavaScript method that requires translations, and populate that object from server side code:
<script type="text/javascript">
var dialogl10n = {
caption: '<%= Resources.GetString(x => x.DialogCaption) %>',
okButton: '<%= SharedResources.GetTerm(x => x.Ok) %>'
cancelButton: '<%= SharedResources.GetTerm(x => x.Cancel) %>'
};
</script>
In the javascript function:
Foo.Bar.doSomething = function(l10n) {
alert(l1n0.caption);
}
We're looking at enhancing this to automatically create the JSON object:
onClick='Foo.Bar.doSomething(<%= Resources.GetPackageAsJson(); %>);'
Which would create the equivalent object created by hand above.
Upvotes: 1
Reputation: 1475
Generate the language dependent portion of javascript on the fly handling locale. For performance cache the language specific javascript, once it is generated for a given locale. This saves from maintaining too many language specific javascripts
Upvotes: 1
Reputation: 145
no good ways to finish the problem.But i or our team has a heavy method that write different string in js for different language, for instance, in uk we write a js named uk.js and in fr we write a fr.js for static expression in dialogs or messages.
Upvotes: 0