开发者

How to localize a non-embedded javascript file with resource?

开发者 https://www.devze.com 2023-01-14 13:19 出处:网络
In my project, there are embedded and non-embedded javascript codes. And i did localization of the embedded codes like below and it works.

In my project, there are embedded and non-embedded javascript codes. And i did localization of the embedded codes like below and it works.

CN.DEFAULT_CONFIG = {
    strings : {
        year: '<%=GetGlobalResourceObject("Resource", "YEAR") %>'

... .. .

But, this method does not work for non-embedded javascript codes. How can i do locali开发者_如何学运维zation for them?


There's several ways to accomplish the goal (embedded resources, handlers, and ajax methods exist with various pros/cons) but if you need to do this on a native file basis, you can use the ajax scriptmanagers as described here.

In a nutshell you create a base localised script containing whatever functions, objects, strings, foo you need:

\webresources\scripts\localised\base.js

...then add whatever localised variations you like using the resource naming pattern:

\webresources\scripts\localised\base.fr.js
\webresources\scripts\localised\base.en-US.js

...then add a reference in script manager thus:

<asp:ScriptManager ID="YourScriptManager" runat="server" EnableScriptLocalization="true">
  <Scripts>
    <asp:ScriptReference Path="\webresources\scripts\localised\base.js" ResourceUICultures="fr,en-US"/>
  </Scripts>
</asp:ScriptManager>

(I'm not 100% sure about the CSV list of cultures there, but I think that's correct)

Edit: Usage

So you could populate base.js with

// Default culture version
var translations = {
  hello : "Hello!"
}

...base.fr.js with:

// En Français
var translations = {
  hello : "Bonjour!"
}

..base.en-US.js with:

// Americanified
var translations = {
  hello : "Howdy!"
}

...and then simply use translations.hello in your invariant code and get whatever the culture comes up with.

You could add whole methods in these files (for date calcs, formatting or whatever) or anything. The key is to realise they've exclusive to each other and that it's therefore important that they have the same... Interface for lack of a better word so they can be orthogonal to whatever consumes them.

0

精彩评论

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