Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: safari/ext/common.js

Issue 6326608939974656: Issue 1502 - Cache translation catalogs on Safari (Closed)
Patch Set: Renamed "source" variable to avoid confusion Created Oct. 29, 2014, 7:58 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: safari/ext/common.js
===================================================================
--- a/safari/ext/common.js
+++ b/safari/ext/common.js
@@ -77,9 +77,6 @@
/* I18n */
- var localeCandidates = null;
- var uiLocale;
-
var getLocaleCandidates = function()
{
var candidates = [];
@@ -100,82 +97,81 @@
return candidates;
};
- var getCatalog = function(locale)
+ var locales = getLocaleCandidates();
+ var catalog = {__proto__: null, "@@ui_locale": [locales[0], []]};
+
+ var replacePlaceholder = function(text, placeholder, content)
+ {
+ return text.split("$" + placeholder + "$").join(content || "");
Wladimir Palant 2014/10/30 23:00:44 Why not keep the more obvious text.replace(...)? A
Sebastian Noack 2014/10/30 23:53:01 Because .replace() with a string as first argument
+ };
+
+ var parseMessage = function(rawMessage)
+ {
+ var text = rawMessage.message;
+ var placeholders = [];
+
+ for (var placeholder in rawMessage.placeholders)
Wladimir Palant 2014/10/30 23:00:44 I didn't expect it but apparently one can iterate
Sebastian Noack 2014/10/30 23:53:01 You can. Iterating over null (or undefined) has th
+ {
+ var content = rawMessage.placeholders[placeholder].content;
+
+ if (/^\$\d+$/.test(content))
+ placeholders[parseInt(content.substr(1)) - 1] = placeholder;
Wladimir Palant 2014/10/30 23:00:44 Doing both regular expressions and string manipula
Sebastian Noack 2014/10/30 23:53:01 Neither the old code, nor most other calls to pars
+ else
+ text = replacePlaceholder(text, placeholder, content);
+ }
+
+ return [text, placeholders];
+ };
+
+ var readCatalog = function(locale)
{
var xhr = new XMLHttpRequest();
-
xhr.open("GET", safari.extension.baseURI + "_locales/" + locale + "/messages.json", false);
- try {
+ try
+ {
xhr.send();
}
catch (e)
{
- return null;
+ return;
}
if (xhr.status != 200 && xhr.status != 0)
- return null;
+ return;
- return JSON.parse(xhr.responseText);
+ var rawCatalog = JSON.parse(xhr.responseText);
+ for (var msgId in rawCatalog)
+ {
+ if (!(msgId in catalog))
+ catalog[msgId] = parseMessage(rawCatalog[msgId]);
+ }
};
ext.i18n = {
getMessage: function(msgId, substitutions)
{
- if (!localeCandidates)
+ while (true)
{
- localeCandidates = getLocaleCandidates();
- uiLocale = localeCandidates[0];
- }
+ var message = catalog[msgId];
+ if (message)
+ {
+ var [text, placeholders] = message;
- if (msgId == "@@ui_locale")
- return uiLocale;
+ if (!(substitutions instanceof Array))
+ substitutions = [substitutions];
- for (var i = 0; i < localeCandidates.length; i++)
- {
- var catalog = getCatalog(localeCandidates[i]);
- if (!catalog)
- {
- // if there is no catalog for this locale
- // candidate, don't try to load it again
- localeCandidates.splice(i--, 1);
- continue;
+ for (var i = 0; i < placeholders.length; i++)
+ text = replacePlaceholder(text, placeholders[i], substitutions[i]);
+
+ return text;
}
- var msg = catalog[msgId];
- if (!msg)
- continue;
+ if (locales.length == 0)
+ return "";
- var msgstr = msg.message;
- if (!msgstr)
- continue;
-
- for (var placeholder in msg.placeholders)
- {
- var placeholderDetails = msg.placeholders[placeholder];
- if (!placeholderDetails || !placeholderDetails.content)
- continue;
- if (placeholderDetails.content.indexOf("$") != 0)
- continue;
-
- var placeholderIdx = parseInt(placeholderDetails.content.substr(1));
- if (isNaN(placeholderIdx) || placeholderIdx < 1)
- continue;
-
- var placeholderValue;
- if (typeof substitutions != "string")
- placeholderValue = substitutions[placeholderIdx - 1];
- else if (placeholderIdx == 1)
- placeholderValue = substitutions;
-
- msgstr = msgstr.replace("$" + placeholder + "$", placeholderValue || "");
- }
-
- return msgstr;
+ readCatalog(locales.shift());
}
-
- return "";
}
};
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld