简体   繁体   English

使用jquery-localize访问javascript函数中的本地化字符串

[英]accessing localized strings in javascript function using jquery-localize

I am using jquery-localize plugin for my static web project. 我在我的静态Web项目中使用jquery-localize插件。 I can localize strings in html file, like this: 我可以本地化html文件中的字符串,如下所示:

<p rel=localize[hello]></p>

But I also need to use localized strings in js functions. 但是我还需要在js函数中使用本地化的字符串。 How can I correctly access those jquery-localize strings from a function? 如何从函数中正确访问那些jquery-localize字符串?

alert( localized_strings[hello] ??? );

here is the jquery-localize plugin: https://github.com/coderifous/jquery-localize/blob/master/README.markdown 这是jquery-localize插件: https : //github.com/coderifous/jquery-localize/blob/master/README.markdown

I've no experience with this plugin, but after a look at the code, I believe you can access the data of the loaded package through 我没有使用此插件的经验,但是看了一下代码后,我相信您可以通过以下方式访问已加载包的数据

$.localize.data.PACKAGE.KEY

where PACKAGE is the language package you've loaded via PACKAGE是您通过其加载的语言包

$("[data-localize]").localize("PACKAGE");

and KEY is the whatever key you want to retrieve (in your example hello ). KEY是您想要检索的任何键(在您的示例中, hello )。

Since the package is loaded via AJAX you might have to ensure that the data is actually available when you need it. 由于软件包是通过AJAX加载的,因此您可能必须确保在需要时可以实际使用数据。 The plugin seems to define a callback method for when the data is loaded an expose it through an option. 该插件似乎定义了一种回调方法,用于在加载数据时通过选项公开它。 So you could do something like this: 因此,您可以执行以下操作:

$("[data-localize]").localize("PACKAGE", { 
    callback: function(data, defaultCallback) {
        console.log(data.KEY); // <-- do whatever here
        defaultCallback(data);
}});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM