在Javascript本地化字符串 [英] Localize Strings in Javascript

查看:262
本文介绍了在Javascript本地化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的.resx 文件来管理我的服务器端资源为.NET。

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

这是我处理还允许开发人员插件的JavaScript成客户端验证等各种事件处理程序的应用程序。什么是对我来说,我的本地化的JavaScript消息和字符串的最佳方式是什么?

the application that I am dealing with also allows developers to plugin JavaScript into various event handlers for client side validation, etc.. What is the best way for me to localize my JavaScript messages and strings?

在理想情况下,我想存储的字符串中的的.resx 文件,让他们与本地化资源的其余部分。

Ideally, I would like to store the strings in the .resx files to keep them with the rest of the localized resources.

我很开放的建议。

推荐答案

一个基本的JavaScript对象是关联数组,因此它很容易可以用来存储键/值对。因此,使用 JSON ,您可以创建一个对象,每个字符串进行本地化,像这样的:

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];

这篇关于在Javascript本地化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆