Skip to content

Localize from object #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Next Next commit
Using raw object as data source
New code has been added so that raw javascript objects can now be used as
a source for the translations.

If localize receives an object as a first parameter, items with
'data-localize' tags will be updated with the text contained in the
matching object properties. Said object will be converted to a valid json
object beforehand, so no problems shall arise if the given object include
functions as a value for any of its keys.

In the case localize method receives any parameter that is not an object,
the default behaviour where the translations are retrieved from an
external file will be applied.

This should solve the following issue:
#62
  • Loading branch information
LonelyPrincess committed Jul 8, 2017
commit 5d89c7a7e9d16f240df584c7b760cb27930e2ba6
24 changes: 19 additions & 5 deletions dist/jquery.localize.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ http://keith-wood.name/localisation.html
};
$.defaultLanguage = normaliseLang(navigator.languages && navigator.languages.length > 0 ? navigator.languages[0] : navigator.language || navigator.userLanguage);
$.localize = function(pkg, options) {
var defaultCallback, deferred, fileExtension, intermediateLangData, jsonCall, lang, loadLanguage, localizeElement, localizeForSpecialKeys, localizeImageElement, localizeInputElement, localizeOptgroupElement, notifyDelegateLanguageLoaded, regexify, setAttrFromValueForKey, setTextFromValueForKey, valueForKey, wrappedSet;
var defaultCallback, deferred, fileExtension, intermediateLangData, jsonCall, loadLanguage, localizeElement, localizeForSpecialKeys, localizeImageElement, localizeInputElement, localizeOptgroupElement, notifyDelegateLanguageLoaded, regexify, setAttrFromValueForKey, setTextFromValueForKey, translateFromFile, translateFromObject, valueForKey, wrappedSet;
if (options == null) {
options = {};
}
Expand Down Expand Up @@ -180,11 +180,25 @@ http://keith-wood.name/localisation.html
return string_or_regex_or_array;
}
};
lang = normaliseLang(options.language ? options.language : $.defaultLanguage);
if (options.skipLanguage && lang.match(regexify(options.skipLanguage))) {
deferred.resolve();
translateFromFile = function() {
var lang;
lang = normaliseLang(options.language ? options.language : $.defaultLanguage);
if (options.skipLanguage && lang.match(regexify(options.skipLanguage))) {
return deferred.resolve();
} else {
return loadLanguage(pkg, lang, 1);
}
};
translateFromObject = function(object) {
var data;
data = JSON.parse(JSON.stringify(object));
defaultCallback(data);
return deferred.resolve();
};
if (typeof pkg === "object") {
translateFromObject(pkg);
} else {
loadLanguage(pkg, lang, 1);
translateFromFile();
}
wrappedSet.localizePromise = deferred;
return wrappedSet;
Expand Down
6 changes: 3 additions & 3 deletions dist/jquery.localize.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 19 additions & 3 deletions src/jquery.localize.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,27 @@ do ($ = jQuery) ->
else
string_or_regex_or_array

lang = normaliseLang(if options.language then options.language else $.defaultLanguage)
if (options.skipLanguage && lang.match(regexify(options.skipLanguage)))
# Retrieve translations from an external file based on required language
translateFromFile = () ->
lang = normaliseLang(if options.language then options.language else $.defaultLanguage)
if (options.skipLanguage && lang.match(regexify(options.skipLanguage)))
deferred.resolve()
else
loadLanguage(pkg, lang, 1)

# Retrieve translations from an object
translateFromObject = (object) ->
# We stringify and parse the received object to ensure the object is a valid json
# Any functions defined within the object will be removed during this process
data = JSON.parse(JSON.stringify(object))
defaultCallback(data)
deferred.resolve()

# If 'pkg' is an object, use it as the source for translations
if typeof(pkg) == "object"
translateFromObject(pkg)
else
loadLanguage(pkg, lang, 1)
translateFromFile()

wrappedSet.localizePromise = deferred

Expand Down