2

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

1 Answer 1

4

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);
}});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.