Skip to content

Commit

Permalink
ES6: migrate "localization"
Browse files Browse the repository at this point in the history
  • Loading branch information
lipis committed Apr 7, 2017
1 parent a8353dc commit f75b0ae
Show file tree
Hide file tree
Showing 42 changed files with 9,965 additions and 11,253 deletions.
36 changes: 18 additions & 18 deletions app/page/template/_dist/app.htm
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,24 @@
<script src="/script/auth/URLParameter.js"></script>
<!-- Localization -->
<script src="/script/localization/strings-init.js"></script>
<script src="/script/localization/strings-cs.js"></script>
<script src="/script/localization/strings-da.js"></script>
<script src="/script/localization/strings-de.js"></script>
<script src="/script/localization/strings-es.js"></script>
<script src="/script/localization/strings-fi.js"></script>
<script src="/script/localization/strings-fr.js"></script>
<script src="/script/localization/strings-hr.js"></script>
<script src="/script/localization/strings-hu.js"></script>
<script src="/script/localization/strings-it.js"></script>
<script src="/script/localization/strings-lt.js"></script>
<script src="/script/localization/strings-pt.js"></script>
<script src="/script/localization/strings-ro.js"></script>
<script src="/script/localization/strings-ru.js"></script>
<script src="/script/localization/strings-sk.js"></script>
<script src="/script/localization/strings-sl.js"></script>
<script src="/script/localization/strings-tr.js"></script>
<script src="/script/localization/strings-uk.js"></script>
<script src="/script/localization/strings.js"></script>
<script src="/script/localization/webapp-cs.js"></script>
<script src="/script/localization/webapp-da.js"></script>
<script src="/script/localization/webapp-de.js"></script>
<script src="/script/localization/webapp-es.js"></script>
<script src="/script/localization/webapp-fi.js"></script>
<script src="/script/localization/webapp-fr.js"></script>
<script src="/script/localization/webapp-hr.js"></script>
<script src="/script/localization/webapp-hu.js"></script>
<script src="/script/localization/webapp-it.js"></script>
<script src="/script/localization/webapp-lt.js"></script>
<script src="/script/localization/webapp-pt.js"></script>
<script src="/script/localization/webapp-ro.js"></script>
<script src="/script/localization/webapp-ru.js"></script>
<script src="/script/localization/webapp-sk.js"></script>
<script src="/script/localization/webapp-sl.js"></script>
<script src="/script/localization/webapp-tr.js"></script>
<script src="/script/localization/webapp-uk.js"></script>
<script src="/script/localization/webapp.js"></script>
<script src="/script/localization/Localizer.js"></script>
<!-- Media -->
<script src="/script/media/MediaDevicesHandler.js"></script>
Expand Down
106 changes: 0 additions & 106 deletions app/script/localization/Localizer.coffee

This file was deleted.

116 changes: 116 additions & 0 deletions app/script/localization/Localizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Wire
* Copyright (C) 2017 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see http://www.gnu.org/licenses/.
*
*/

'use strict';

window.z = window.z || {};
z.localization = z.localization || {};

class Localizer {
constructor() {
let param = z.util.get_url_parameter(z.auth.URLParameter.LOCALE);
if (param) {
z.util.StorageUtil.set_value(z.storage.StorageKey.LOCALIZATION.LOCALE, param);
}
this.locale = z.util.StorageUtil.get_value(z.storage.StorageKey.LOCALIZATION.LOCALE) || navigator.language.substr(0, 2) || 'en';
moment.locale([this.locale, 'en']);
if (z.string[this.locale]) {
$.extend(z.string, z.string[this.locale]);
}
}

/*
Pulls the localized string from the resources and replaces placeholders.
@note Takes the id of the string for look up from z.string is directly for simple use. Else pass it in as the id
parameter in conjunction with a single or multiple (it supports but does not require an array) replace rules that
consist of a placeholder and the content that it should be replace with.
@param id [String] Localization string ID
@param replace [Object | Array<Object>] Placeholders that should be replaced
@option replace [String] placeholder Content to be replaced
@option replace [String] content replacing content
*/

get_text(valueAccessor) {
if (valueAccessor == null) return;

let args = [];
let s = valueAccessor;
if (valueAccessor.id != null) {
s = valueAccessor.id;
if (_.isArray(valueAccessor.replace)) {
args = valueAccessor.replace;
} else {
args.push(valueAccessor.replace);
}
}
if (args.length !== 0) {
for (let arg of args) {
const reg = new RegExp(arg.placeholder, 'gm');
s = s.replace(reg, arg.content);
}
}
return s;
}
}

z.localization.Localizer = new Localizer();

ko.bindingHandlers.l10n_href = {
update(element, valueAccessor) {
element.setAttribute('href', z.localization.Localizer.get_text(valueAccessor()));
},
};

ko.bindingHandlers.l10n_input = {
update(element, valueAccessor) {
element.setAttribute('value', z.localization.Localizer.get_text(valueAccessor()));
},
};

ko.bindingHandlers.l10n_placeholder = {
update(element, valueAccessor) {
element.setAttribute('placeholder', z.localization.Localizer.get_text(valueAccessor()));
},
};

ko.bindingHandlers.l10n_text = {
update(element, valueAccessor) {
ko.utils.setTextContent(element, z.localization.Localizer.get_text(valueAccessor()));
},
};

ko.bindingHandlers.l10n_html = {
update(element, valueAccessor) {
ko.utils.setHtml(element, z.localization.Localizer.get_text(valueAccessor()));
},
};

ko.bindingHandlers.l10n_tooltip = {
update(element, valueAccessor) {
element.setAttribute('title', z.localization.Localizer.get_text(valueAccessor()));
},
};

ko.bindingHandlers.l10n_aria_label = {
update(element, valueAccessor) {
element.setAttribute('aria-label', z.localization.Localizer.get_text(valueAccessor()));
},
};
Loading

0 comments on commit f75b0ae

Please sign in to comment.