Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
This commit is contained in:
parent
93e3a48004
commit
b38dea0597
25901 changed files with 9982925 additions and 369 deletions
93
gaia/apps/sms/views/shared/js/localization_helper.js
Executable file
93
gaia/apps/sms/views/shared/js/localization_helper.js
Executable file
|
|
@ -0,0 +1,93 @@
|
|||
/* exported LocalizationHelper */
|
||||
(function(exports) {
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Look for attachment iframes and localize them - l20n.js doesn't do this
|
||||
* XXX: remove once bug 1020130 is fixed
|
||||
*/
|
||||
function localizeAttachmentIFrames() {
|
||||
var attachmentContainers = Array.from(document.querySelectorAll(
|
||||
'iframe.attachment-container'
|
||||
));
|
||||
|
||||
return Promise.all(
|
||||
attachmentContainers.map(localizeAttachmentIFrame)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Localize an attachment iframe
|
||||
*/
|
||||
function localizeAttachmentIFrame(attachmentContainer) {
|
||||
var doc = attachmentContainer.contentDocument;
|
||||
doc.documentElement.lang = document.documentElement.getAttribute('lang');
|
||||
doc.documentElement.dir = document.documentElement.getAttribute('dir');
|
||||
return document.l10n.translateFragment(doc.body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Look for time elements with format and re-translate the date message.
|
||||
* More complex because the argument to the l10n string is itself a formatted
|
||||
* date using l10n.
|
||||
* @param {boolean} isTimeFormatChanged Indicates that time format has been
|
||||
* changed and we need to update only elements with time aware formats.
|
||||
*/
|
||||
function localizeDateTime(isTimeFormatChanged) {
|
||||
var dateTimeElements = document.querySelectorAll('.l10n-contains-date');
|
||||
|
||||
Array.forEach(dateTimeElements, function(element) {
|
||||
if (!element.dataset.l10nDate) {
|
||||
return;
|
||||
}
|
||||
|
||||
var formatL10nId = element.dataset.l10nDateFormat;
|
||||
|
||||
if (!formatL10nId) {
|
||||
return;
|
||||
}
|
||||
|
||||
formatL10nId = JSON.parse(formatL10nId);
|
||||
|
||||
if (isTimeFormatChanged && !formatL10nId.hour) {
|
||||
return;
|
||||
}
|
||||
formatL10nId.hour12 = navigator.mozHour12;
|
||||
|
||||
var formatter = new Intl.DateTimeFormat(
|
||||
navigator.languages, formatL10nId
|
||||
);
|
||||
var localeData = formatter.format(new Date(+element.dataset.l10nDate));
|
||||
|
||||
if (element.hasAttribute('data-l10n-id') &&
|
||||
element.hasAttribute('data-l10n-args')) {
|
||||
var l10nAttrs = document.l10n.getAttributes(element);
|
||||
l10nAttrs.args.date = localeData;
|
||||
document.l10n.setAttributes(element, l10nAttrs.id, l10nAttrs.args);
|
||||
} else {
|
||||
element.textContent = localeData;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
exports.LocalizationHelper = {
|
||||
init: function () {
|
||||
function localize() {
|
||||
localizeDateTime();
|
||||
return localizeAttachmentIFrames();
|
||||
}
|
||||
|
||||
// After startup, call localize every time the language changes
|
||||
document.addEventListener('DOMRetranslated', localize);
|
||||
|
||||
// This event is fired when time format (12h/24h) is changed
|
||||
window.addEventListener(
|
||||
'timeformatchange',
|
||||
localizeDateTime.bind(null, /* isTimeFormatChanged */ true)
|
||||
);
|
||||
|
||||
// Call localize during startup
|
||||
return document.l10n.ready.then(localize);
|
||||
}
|
||||
};
|
||||
})(window);
|
||||
Loading…
Reference in a new issue