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
Prev Previous commit
Next Next commit
The method wrapping default behaviour now receives the filename as a …
…parameter
  • Loading branch information
LonelyPrincess committed Jul 9, 2017
commit e138c72c280f237c305f0a5f64d264073f82ae17
12 changes: 6 additions & 6 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, loadLanguage, localizeElement, localizeForSpecialKeys, localizeImageElement, localizeInputElement, localizeOptgroupElement, notifyDelegateLanguageLoaded, regexify, setAttrFromValueForKey, setTextFromValueForKey, translateFromFile, translateFromObject, valueForKey, wrappedSet;
var defaultCallback, deferred, fileExtension, intermediateLangData, jsonCall, loadLanguage, localizeElement, localizeForSpecialKeys, localizeImageElement, localizeInputElement, localizeOptgroupElement, notifyDelegateLanguageLoaded, regexify, setAttrFromValueForKey, setTextFromValueForKey, useFileAsDataSource, useObjectAsDataSource, valueForKey, wrappedSet;
if (options == null) {
options = {};
}
Expand Down Expand Up @@ -180,25 +180,25 @@ http://keith-wood.name/localisation.html
return string_or_regex_or_array;
}
};
translateFromFile = function() {
useFileAsDataSource = function(filename) {
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);
return loadLanguage(filename, lang, 1);
}
};
translateFromObject = function(object) {
useObjectAsDataSource = function(object) {
var data;
data = JSON.parse(JSON.stringify(object));
defaultCallback(data);
return deferred.resolve();
};
if (typeof pkg === "object") {
translateFromObject(pkg);
useObjectAsDataSource(pkg);
} else {
translateFromFile();
useFileAsDataSource(pkg);
}
wrappedSet.localizePromise = deferred;
return wrappedSet;
Expand Down
4 changes: 2 additions & 2 deletions dist/jquery.localize.min.js

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

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

# Retrieve translations from an external file based on required language
translateFromFile = () ->
# Retrieve translations from an external file depending on required language
useFileAsDataSource = (filename) ->
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)
loadLanguage(filename, lang, 1)

# Retrieve translations from an object
translateFromObject = (object) ->
useObjectAsDataSource = (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))
Expand All @@ -154,9 +154,9 @@ do ($ = jQuery) ->

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

wrappedSet.localizePromise = deferred

Expand Down