Compare commits
1 Commits
develop
...
update_htm
Author | SHA1 | Date |
---|---|---|
Luiza Pagliari | 84f9e55da4 |
|
@ -180,7 +180,7 @@ window.html10n = (function(window, document, undefined) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// dat alng ain't here, man!
|
// dat alng ain't here, man!
|
||||||
if (!data[lang]) {
|
if (!data) {
|
||||||
var msg = 'Couldn\'t find translations for '+lang
|
var msg = 'Couldn\'t find translations for '+lang
|
||||||
, l
|
, l
|
||||||
if(~lang.indexOf('-')) lang = lang.split('-')[0] // then let's try related langs
|
if(~lang.indexOf('-')) lang = lang.split('-')[0] // then let's try related langs
|
||||||
|
@ -208,12 +208,12 @@ window.html10n = (function(window, document, undefined) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('object' != typeof data[lang]) {
|
if ('object' != typeof data) {
|
||||||
cb(new Error('Translations should be specified as JSON objects!'))
|
cb(new Error('Translations should be specified as JSON objects!'))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
this.langs[lang] = data[lang]
|
this.langs[lang] = data
|
||||||
// TODO: Also store accompanying langs
|
// TODO: Also store accompanying langs
|
||||||
cb()
|
cb()
|
||||||
}
|
}
|
||||||
|
@ -231,6 +231,17 @@ window.html10n = (function(window, document, undefined) {
|
||||||
|
|
||||||
html10n.rtl = ["ar","dv","fa","ha","he","ks","ku","ps","ur","yi"]
|
html10n.rtl = ["ar","dv","fa","ha","he","ks","ku","ps","ur","yi"]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Language-Script fallbacks for Language-Region language tags, for languages that
|
||||||
|
* varies heavily on writing form and two-part locale expansion is not feasible.
|
||||||
|
* See also: https://tools.ietf.org/html/rfc4646 (RFC 4646)
|
||||||
|
*/
|
||||||
|
html10n.scripts = {
|
||||||
|
'zh-tw': 'zh-hant',
|
||||||
|
'zh-hk': 'zh-hant',
|
||||||
|
'zh-cn': 'zh-hans'
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get rules for plural forms (shared with JetPack), see:
|
* Get rules for plural forms (shared with JetPack), see:
|
||||||
* http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
|
* http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
|
||||||
|
@ -669,24 +680,43 @@ window.html10n = (function(window, document, undefined) {
|
||||||
return str;
|
return str;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/** Prepare localization context:
|
||||||
* Localize a document
|
*
|
||||||
* @param langs An array of lang codes defining fallbacks
|
* - Populate translations with strings for the indicated languages
|
||||||
|
* - adding in non-qualified versions of language codes immediately
|
||||||
|
* after any qualified (e.g., "en" for "en-GB")
|
||||||
|
* - Trigger "localized" event.
|
||||||
|
*
|
||||||
|
* @param {array} langs: diminishing-precedence lang codes, or one string.
|
||||||
*/
|
*/
|
||||||
html10n.localize = function(langs) {
|
html10n.localize = function(langs) {
|
||||||
var that = this
|
var that = this,
|
||||||
// if only one string => create an array
|
candidates = [];
|
||||||
if ('string' == typeof langs) langs = [langs]
|
// if a single string, bundle it as an array:
|
||||||
|
if ('string' == typeof langs) {
|
||||||
|
langs = [langs];
|
||||||
|
}
|
||||||
|
|
||||||
// Expand two-part locale specs
|
// Determine candidates from langs:
|
||||||
var i=0
|
// - Omitting empty strings
|
||||||
|
// - Adding in non-qualified versions of country-qualified codes.
|
||||||
langs.forEach(function(lang) {
|
langs.forEach(function(lang) {
|
||||||
if(!lang) return;
|
var splat;
|
||||||
langs[i++] = lang;
|
if(!lang) { return; }
|
||||||
if(~lang.indexOf('-')) langs[i++] = lang.substr(0, lang.indexOf('-'));
|
(candidates.indexOf(lang) == -1) && candidates.push(lang);
|
||||||
})
|
splat = lang.split('-');
|
||||||
|
if (splat[1]) {
|
||||||
|
(candidates.indexOf(splat[0]) == -1) && candidates.push(splat[0]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
this.build(langs, function(er, translations) {
|
// Append script fallbacks for region-specific locales if applicable
|
||||||
|
for (var lang in html10n.scripts) {
|
||||||
|
i = candidates.indexOf(lang);
|
||||||
|
if (~i) candidates.splice(i, 0, html10n.scripts[lang])
|
||||||
|
}
|
||||||
|
|
||||||
|
this.build(candidates, function(er, translations) {
|
||||||
html10n.translations = translations
|
html10n.translations = translations
|
||||||
html10n.translateElement(translations)
|
html10n.translateElement(translations)
|
||||||
that.trigger('localized')
|
that.trigger('localized')
|
||||||
|
@ -715,7 +745,7 @@ window.html10n = (function(window, document, undefined) {
|
||||||
var i = 0
|
var i = 0
|
||||||
, n = list.length
|
, n = list.length
|
||||||
iterator(list[i], i, function each(err) {
|
iterator(list[i], i, function each(err) {
|
||||||
if(err) console.error(err)
|
if(err) console.log(err)
|
||||||
i++
|
i++
|
||||||
if (i < n) return iterator(list[i],i, each);
|
if (i < n) return iterator(list[i],i, each);
|
||||||
cb()
|
cb()
|
||||||
|
@ -738,7 +768,12 @@ window.html10n = (function(window, document, undefined) {
|
||||||
|
|
||||||
html10n.get = function(id, args) {
|
html10n.get = function(id, args) {
|
||||||
var translations = html10n.translations
|
var translations = html10n.translations
|
||||||
if(!translations) return console.warn('No translations available (yet)')
|
if(!translations) {
|
||||||
|
if (! html10n.quiet) {
|
||||||
|
console.warn('No translations available (yet)');
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
if(!translations[id]) return console.warn('Could not find string '+id)
|
if(!translations[id]) return console.warn('Could not find string '+id)
|
||||||
|
|
||||||
// apply macros
|
// apply macros
|
||||||
|
@ -755,8 +790,9 @@ window.html10n = (function(window, document, undefined) {
|
||||||
// replace {{arguments}} with their values or the
|
// replace {{arguments}} with their values or the
|
||||||
// associated translation string (based on its key)
|
// associated translation string (based on its key)
|
||||||
function substArguments(str, args) {
|
function substArguments(str, args) {
|
||||||
var reArgs = /\{\{\s*([a-zA-Z\.]+)\s*\}\}/
|
var reArgs = /\{\{\s*([a-zA-Z_\-\.]+)\s*\}\}/,
|
||||||
, match
|
translations = html10n.translations,
|
||||||
|
match;
|
||||||
|
|
||||||
while (match = reArgs.exec(str)) {
|
while (match = reArgs.exec(str)) {
|
||||||
if (!match || match.length < 2)
|
if (!match || match.length < 2)
|
||||||
|
@ -764,12 +800,13 @@ window.html10n = (function(window, document, undefined) {
|
||||||
|
|
||||||
var arg = match[1]
|
var arg = match[1]
|
||||||
, sub = ''
|
, sub = ''
|
||||||
if (arg in args) {
|
if (args && (arg in args)) {
|
||||||
sub = args[arg]
|
sub = args[arg]
|
||||||
} else if (arg in translations) {
|
} else if (arg in translations) {
|
||||||
sub = translations[arg]
|
sub = translations[arg]
|
||||||
} else {
|
} else {
|
||||||
console.warn('Could not find argument {{' + arg + '}}')
|
console.warn('Could not satisfy argument {{' + arg + '}}' +
|
||||||
|
' for string "' + str + '"');
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -781,7 +818,7 @@ window.html10n = (function(window, document, undefined) {
|
||||||
|
|
||||||
// replace {[macros]} with their values
|
// replace {[macros]} with their values
|
||||||
function substMacros(key, str, args) {
|
function substMacros(key, str, args) {
|
||||||
var regex = /\{\[\s*([a-zA-Z]+)\(([a-zA-Z]+)\)((\s*([a-zA-Z]+)\: ?([ a-zA-Z{}]+),?)+)*\s*\]\}/ //.exec('{[ plural(n) other: are {{n}}, one: is ]}')
|
var regex = /\{\[\s*([a-zA-Z]+)\(([a-zA-Z]+)\)((\s*([a-zA-Z]+)\: ?([^,]+?),?)+)*\s*\]\}/ //.exec('{{n}} {[plural(n) one: Bomba, other: Bombe]} ]}')
|
||||||
, match
|
, match
|
||||||
|
|
||||||
while(match = regex.exec(str)) {
|
while(match = regex.exec(str)) {
|
||||||
|
@ -795,7 +832,7 @@ window.html10n = (function(window, document, undefined) {
|
||||||
if (!(macroName in html10n.macros)) continue
|
if (!(macroName in html10n.macros)) continue
|
||||||
|
|
||||||
if(optv) {
|
if(optv) {
|
||||||
optv.match(/(?=\s*)([a-zA-Z]+)\: ?([ a-zA-Z{}]+)(?=,?)/g).forEach(function(arg) {
|
optv.match(/(?=\s*)([a-zA-Z]+)\: ?([^,\]]+)(?=,?)/g).forEach(function(arg) {
|
||||||
var parts = arg.split(':')
|
var parts = arg.split(':')
|
||||||
, name = parts[0]
|
, name = parts[0]
|
||||||
, value = parts[1].trim()
|
, value = parts[1].trim()
|
||||||
|
@ -832,7 +869,7 @@ window.html10n = (function(window, document, undefined) {
|
||||||
|
|
||||||
// get args
|
// get args
|
||||||
if(window.JSON) {
|
if(window.JSON) {
|
||||||
str.args = JSON.parse(node.getAttribute('data-l10n-args'))
|
str.args = node.getAttribute('data-l10n-args') ? JSON.parse(node.getAttribute('data-l10n-args')) : [];
|
||||||
}else{
|
}else{
|
||||||
try{
|
try{
|
||||||
str.args = eval(node.getAttribute('data-l10n-args'))
|
str.args = eval(node.getAttribute('data-l10n-args'))
|
||||||
|
@ -851,6 +888,7 @@ window.html10n = (function(window, document, undefined) {
|
||||||
, "innerHTML": 1
|
, "innerHTML": 1
|
||||||
, "alt": 1
|
, "alt": 1
|
||||||
, "textContent": 1
|
, "textContent": 1
|
||||||
|
, "placeholder": 1
|
||||||
, "value": 1
|
, "value": 1
|
||||||
}
|
}
|
||||||
if (index > 0 && str.id.substr(index + 1) in attrList) { // an attribute has been specified
|
if (index > 0 && str.id.substr(index + 1) in attrList) { // an attribute has been specified
|
||||||
|
@ -861,7 +899,7 @@ window.html10n = (function(window, document, undefined) {
|
||||||
|
|
||||||
// Apply translation
|
// Apply translation
|
||||||
if (node.children.length === 0 || prop != 'textContent') {
|
if (node.children.length === 0 || prop != 'textContent') {
|
||||||
node[prop] = str.str
|
node[prop] = str.str;
|
||||||
node.setAttribute("aria-label", str.str); // Sets the aria-label
|
node.setAttribute("aria-label", str.str); // Sets the aria-label
|
||||||
// The idea of the above is that we always have an aria value
|
// The idea of the above is that we always have an aria value
|
||||||
// This might be a bit of an abrupt solution but let's see how it goes
|
// This might be a bit of an abrupt solution but let's see how it goes
|
||||||
|
|
Loading…
Reference in New Issue