summaryrefslogtreecommitdiffstats
path: root/src/lib/i18n.js
blob: 11ef67c6e4ae4c74fdf40a5e034d54d7f9b2a609 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * This file is part of Adblock Plus <http://adblockplus.org/>,
 * Copyright (C) 2006-2013 Eyeo GmbH
 *
 * Adblock Plus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * Adblock Plus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Adblock Plus.  If not, see <http://www.gnu.org/licenses/>.
 */

(function () {

const LOCALE = chrome.i18n.getMessage('@@ui_locale'),
  ON_POPUP = (document.location.pathname == "/skin/popup.html");

function localizeFaqLink() {
  const LOCALIZED_HOMEPAGE_LOCALES = ['es'];
  if (ON_POPUP && LOCALIZED_HOMEPAGE_LOCALES.includes(LOCALE)) {
    // update FAQ link to point to localized version
    $('#help').prop('href', `https://privacybadger.org/${LOCALE}/#faq`);
  }
}

function setTextDirection() {
  function toggle_css_value(selector, property, from, to) {
    let $els = $(selector);
    $els.each(i => {
      let $el = $($els[i]);
      if ($el.css(property) === from) {
        $el.css(property, to);
      }
    });
  }

  // https://www.w3.org/International/questions/qa-scripts#examples
  // https://developer.chrome.com/webstore/i18n?csw=1#localeTable
  // TODO duplicated in src/js/webrequest.js
  const RTL_LOCALES = ['ar', 'he', 'fa'];
  if (!RTL_LOCALES.includes(LOCALE)) {
    return;
  }

  // set body text direction
  document.body.setAttribute("dir", "rtl");

  // popup page
  if (ON_POPUP) {
    // fix floats
    ['#privacyBadgerHeader img', '#header-image-stack', '#version'].forEach((selector) => {
      toggle_css_value(selector, "float", "left", "right");
    });
    ['#fittslaw', '#options', '#help', '#share', '.overlay_close'].forEach((selector) => {
      toggle_css_value(selector, "float", "right", "left");
    });

  // options page
  } else if (document.location.pathname == "/skin/options.html") {
    // apply RTL workaround for jQuery UI tabs
    // https://zoomicon.wordpress.com/2009/10/15/how-to-use-jqueryui-tabs-in-right-to-left-layout/
    let css = document.createElement("style");
    css.type = "text/css";
    css.textContent = `
.ui-tabs { direction: rtl; }
.ui-tabs .ui-tabs-nav li.ui-tabs-selected,
  .ui-tabs .ui-tabs-nav li.ui-state-default { float: right; }
.ui-tabs .ui-tabs-nav li a { float: right; }
`;
    document.body.appendChild(css);

    // fix floats
    ['.btn-silo', '.btn-silo div', '#allowlist-form > div > div > div'].forEach((selector) => {
      toggle_css_value(selector, "float", "left", "right");
    });
  }
}

/**
 * Loads and inserts i18n strings into matching elements.
 */
function loadI18nStrings() {
  let els = document.querySelectorAll("[class^='i18n_']");

  // replace element contents by their class names
  for (let el of els) {
    const key = el.className.split(/\s/)[0].slice(5),
      prop = ("innerHTML" in el ? "innerHTML" : "textContent");

    // get chrome.i18n placeholders, if any
    let placeholders = el.dataset.i18n_contents_placeholders;
    placeholders = (placeholders ? placeholders.split("@@") : []);

    // replace contents
    el[prop] = chrome.i18n.getMessage(key, placeholders);
  }

  // also replace alt, placeholder and title attributes
  const ATTRS = [
    'alt',
    'placeholder',
    'title',
  ];

  // get all the elements that contain one or more of these attributes
  els = document.querySelectorAll(
    // for example: "[placeholder^='i18n_'], [title^='i18n_']"
    "[" + ATTRS.join("^='i18n_'], [") + "^='i18n_']"
  );

  // for each element
  for (let el of els) {
    // for each attribute
    for (let attr_type of ATTRS) {
      // get the translation message key
      let key = el.getAttribute(attr_type);

      // attribute exists
      if (key) {
        // remove the i18n_ prefix
        key = key.startsWith("i18n_") && key.slice(5);
      }

      if (!key) {
        continue;
      }

      // get chrome.i18n placeholders, if any
      // TODO multiple attributes are not supported
      let placeholders = el.dataset.i18n_attribute_placeholders;
      placeholders = (placeholders ? placeholders.split("@@") : []);

      // update the attribute with the result of a translation lookup by KEY
      el.setAttribute(attr_type, chrome.i18n.getMessage(key, placeholders));
    }
  }
}

// Fill in the strings as soon as possible
window.addEventListener("DOMContentLoaded", function () {
  localizeFaqLink();
  setTextDirection();
  loadI18nStrings();
}, true);

}());