开发者

What is the recommended way to enable multiple language support for Javascript displayed text

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 yo开发者_Python百科ur 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.


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.


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜