From 686403af3843693558533311a6523abb41725457 Mon Sep 17 00:00:00 2001 From: Deathamns Date: Tue, 21 Oct 2014 16:45:54 +0200 Subject: [PATCH] Avoid using Chrome's @@bidi_* type i18n messages ... for the sake of portability. When including vapi-common.js in an HTML file, then the body element there will have a "dir" attribute filled with the current locale's direction (ltr or rtl). The following languages are considered right-to-left: ar, he, fa, ps, ur. Everything else is left-to-right. After the "dir" attribute is set, we can decide in CSS which elements should have different styling for rtl languages (e.g., body[dir=rtl] #id). --- src/js/vapi-common.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/js/vapi-common.js b/src/js/vapi-common.js index 752cb2b..3ece2ad 100644 --- a/src/js/vapi-common.js +++ b/src/js/vapi-common.js @@ -5,6 +5,14 @@ window.vAPI = window.vAPI || {}; +// http://www.w3.org/International/questions/qa-scripts#directions +var setScriptDirection = function(langugae) { + document.body.setAttribute( + 'dir', + ~['ar', 'he', 'fa', 'ps', 'ur'].indexOf(langugae) ? 'rtl' : 'ltr' + ); +}; + vAPI.download = function(details) { if (!details.url) { return; @@ -40,6 +48,8 @@ if (window.chrome) { vAPI.i18n = function(s) { return chrome.i18n.getMessage(s) || s; }; + + setScriptDirection(vAPI.i18n('@@ui_locale')); } else if (window.safari) { vAPI.getURL = function(path) { return safari.extension.baseURI + path; @@ -53,14 +63,14 @@ if (window.chrome) { if (vAPI.i18nData[vAPI.i18n = navigator.language.replace('-', '_')] || vAPI.i18nData[vAPI.i18n = vAPI.i18n.slice(0, 2)]) { - vAPI.i18nAlpha2 = vAPI.i18n; + vAPI.i18nLocale = vAPI.i18n; } else { - vAPI.i18nAlpha2 = vAPI.i18nData._; + vAPI.i18nLocale = vAPI.i18nData._; } xhr = new XMLHttpRequest; xhr.overrideMimeType('application/json;charset=utf-8'); - xhr.open('GET', './_locales/' + vAPI.i18nAlpha2 + '/messages.json', false); + xhr.open('GET', './_locales/' + vAPI.i18nLocale + '/messages.json', false); xhr.send(); vAPI.i18nData = JSON.parse(xhr.responseText); @@ -72,6 +82,8 @@ if (window.chrome) { return this.i18nData[s] || s; }; + setScriptDirection(vAPI.i18nLocale); + // update popover size to its content if (safari.self.identifier === 'popover' && safari.self) { window.addEventListener('load', function() {