Localize Strings in Javascript

前端 未结 12 1482
长发绾君心
长发绾君心 2020-11-28 05:52

I\'m currently using .resx files to manage my server side resources for .NET.

the application that I am dealing with also allows developers to plugin Ja

12条回答
  •  渐次进展
    2020-11-28 05:56

    A basic JavaScript object is an associative array, so it can easily be used to store key/value pairs. So using JSON, you could create an object for each string to be localized like this:

    var localizedStrings={
        confirmMessage:{
            'en/US':'Are you sure?',
            'fr/FR':'Est-ce que vous êtes certain?',
            ...
        },
    
        ...
    }
    

    Then you could get the locale version of each string like this:

    var locale='en/US';
    var confirm=localizedStrings['confirmMessage'][locale];
    

提交回复
热议问题