i18n.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. Photo - localization
  3. Shared by index.html and embedded.html. The strings live in
  4. locale/photo.json; static markup is translated by ../script/applocale.js
  5. (locale="key" attributes, titles and placeholders), script strings by T().
  6. */
  7. // The translation of a key, or the English fallback. {name} placeholders are
  8. // filled from vars.
  9. function T(key, fallback, vars) {
  10. var text = fallback;
  11. if (typeof applocale !== "undefined" && applocale && applocale.localData && applocale.localData.keys) {
  12. text = applocale.getString(key, fallback);
  13. }
  14. if (vars) {
  15. text = text.replace(/\{(\w+)\}/g, function (match, name) {
  16. return vars[name] !== undefined ? vars[name] : match;
  17. });
  18. }
  19. return text;
  20. }
  21. // Language to format dates and times with; undefined keeps the browser's own
  22. function localeTag() {
  23. if (typeof applocale !== "undefined" && applocale && applocale.localData &&
  24. applocale.localData.keys && applocale.localData.keys[applocale.lang]) {
  25. return applocale.lang;
  26. }
  27. return undefined;
  28. }
  29. // Load the language file, translate the static markup, then run callback. The
  30. // page starts in English when the file cannot be loaded.
  31. function loadAppLocale(callback) {
  32. if (typeof applocale === "undefined" || !applocale) {
  33. callback();
  34. return;
  35. }
  36. applocale.init("locale/photo.json", function () {
  37. applocale.translate();
  38. callback();
  39. }, callback);
  40. }