简体   繁体   中英

accessing localized strings in javascript function using jquery-localize

I am using jquery-localize plugin for my static web project. I can localize strings in html file, like this:

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

But I also need to use localized strings in js functions. How can I correctly access those jquery-localize strings from a function?

alert( localized_strings[hello] ??? );

here is the jquery-localize plugin: 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

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

and KEY is the whatever key you want to retrieve (in your example hello ).

Since the package is loaded via AJAX you might have to ensure that the data is actually available when you need it. 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);
}});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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