aboutsummaryrefslogtreecommitdiffstats
path: root/core/src
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@winzerhof-wurst.at>2019-05-31 13:27:29 +0200
committerRoeland Jago Douma <roeland@famdouma.nl>2019-06-26 10:07:21 +0200
commit409acf113129cc3d1acb550387016f0b30318018 (patch)
tree187b0a2a3a151aa621420e6679ccb7adf742ffa9 /core/src
parentbc276cdd83aeb40e70ccca4573b4318ca7dceb81 (diff)
downloadnextcloud-server-409acf113129cc3d1acb550387016f0b30318018.tar.gz
nextcloud-server-409acf113129cc3d1acb550387016f0b30318018.zip
Fix js l10n registration to also work with more than one bundle
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'core/src')
-rw-r--r--core/src/OC/l10n-registry.js88
-rw-r--r--core/src/OC/l10n.js53
2 files changed, 109 insertions, 32 deletions
diff --git a/core/src/OC/l10n-registry.js b/core/src/OC/l10n-registry.js
new file mode 100644
index 00000000000..6f537c86c00
--- /dev/null
+++ b/core/src/OC/l10n-registry.js
@@ -0,0 +1,88 @@
+/*
+ * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// This var is global because it's shared across webpack bundles
+window._oc_l10n_registry_translations = window._oc_l10n_registry_translations || {}
+window._oc_l10n_registry_plural_functions = window._oc_l10n_registry_plural_functions || {}
+
+/**
+ * @param {String} appId
+ * @param {Object} translations
+ * @param {Function} pluralFunction
+ */
+const register = (appId, translations, pluralFunction) => {
+ window._oc_l10n_registry_translations[appId] = translations
+ window._oc_l10n_registry_plural_functions[appId] = pluralFunction
+}
+
+/**
+ * @param {String} appId
+ * @param {Object} translations
+ * @param {Function} pluralFunction
+ */
+const extend = (appId, translations, pluralFunction) => {
+ window._oc_l10n_registry_translations[appId] = Object.assign(
+ window._oc_l10n_registry_translations[appId],
+ translations
+ )
+ window._oc_l10n_registry_plural_functions[appId] = pluralFunction
+}
+
+/**
+ * @param {String} appId
+ * @param {Object} translations
+ * @param {Function} pluralFunction
+ */
+export const registerAppTranslations = (appId, translations, pluralFunction) => {
+ if (!hasAppTranslations(appId)) {
+ register(appId, translations, pluralFunction)
+ } else {
+ extend(appId, translations, pluralFunction)
+ }
+}
+
+/**
+ * @param {String} appId
+ */
+export const unregisterAppTranslations = appId => {
+ delete window._oc_l10n_registry_translations[appId]
+ delete window._oc_l10n_registry_plural_functions[appId]
+}
+
+/**
+ * @param {String} appId
+ * @return {Boolean}
+ */
+export const hasAppTranslations = appId => {
+ return window._oc_l10n_registry_translations[appId] !== undefined
+ && window._oc_l10n_registry_plural_functions[appId] !== undefined
+}
+
+/**
+ * @param {String} appId
+ * @return {Object}
+ */
+export const getAppTranslations = appId => {
+ return {
+ translations: window._oc_l10n_registry_translations[appId] || {},
+ pluralFunction: window._oc_l10n_registry_plural_functions[appId],
+ }
+}
diff --git a/core/src/OC/l10n.js b/core/src/OC/l10n.js
index 30089b3427a..a4f92b4487a 100644
--- a/core/src/OC/l10n.js
+++ b/core/src/OC/l10n.js
@@ -13,6 +13,12 @@ import $ from 'jquery'
import Handlebars from 'handlebars'
import OC from './index'
+import {
+ getAppTranslations,
+ hasAppTranslations,
+ registerAppTranslations,
+ unregisterAppTranslations
+} from './l10n-registry'
/**
* L10N namespace with localization functions.
@@ -20,17 +26,6 @@ import OC from './index'
* @namespace OC.L10n
*/
const L10n = {
- /**
- * String bundles with app name as key.
- * @type {Object.<String,String>}
- */
- _bundles: {},
-
- /**
- * Plural functions, key is app name and value is function.
- * @type {Object.<String,Function>}
- */
- _pluralFunctions: {},
/**
* Load an app's translation bundle if not loaded already.
@@ -42,7 +37,7 @@ const L10n = {
*/
load: function(appName, callback) {
// already available ?
- if (this._bundles[appName] || OC.getLocale() === 'en') {
+ if (hasAppTranslations(appName) || OC.getLocale() === 'en') {
var deferred = $.Deferred();
var promise = deferred.promise();
promise.then(callback);
@@ -69,22 +64,17 @@ const L10n = {
*
* @param {String} appName name of the app
* @param {Object<String,String>} bundle
- * @param {Function|String} [pluralForm] optional plural function or plural string
*/
register: function(appName, bundle, pluralForm) {
- var self = this;
- if (_.isUndefined(this._bundles[appName])) {
- this._bundles[appName] = bundle || {};
-
- // generate plural function based on form
- this._pluralFunctions[appName] = this._getPlural;
- } else {
- // Theme overwriting the default language
- _.extend(self._bundles[appName], bundle);
- }
+ registerAppTranslations(appName, bundle, this._getPlural)
},
/**
+ * @private do not use this
+ */
+ _unregister: unregisterAppTranslations,
+
+ /**
* Translate a string
* @param {string} app the id of the app for which to translate the string
* @param {string} text the string to translate
@@ -121,8 +111,8 @@ const L10n = {
);
};
var translation = text;
- var bundle = this._bundles[app] || {};
- var value = bundle[text];
+ var bundle = getAppTranslations(app);
+ var value = bundle.translations[text];
if( typeof(value) !== 'undefined' ){
translation = value;
}
@@ -146,21 +136,20 @@ const L10n = {
* @return {string} Translated string
*/
translatePlural: function(app, textSingular, textPlural, count, vars, options) {
- var identifier = '_' + textSingular + '_::_' + textPlural + '_';
- var bundle = this._bundles[app] || {};
- var value = bundle[identifier];
+ const identifier = '_' + textSingular + '_::_' + textPlural + '_';
+ const bundle = getAppTranslations(app);
+ const value = bundle.translations[identifier];
if( typeof(value) !== 'undefined' ){
var translation = value;
if ($.isArray(translation)) {
- var plural = this._pluralFunctions[app](count);
+ var plural = bundle.pluralFunction(count);
return this.translate(app, translation[plural], vars, count, options);
}
}
- if(count === 1) {
+ if (count === 1) {
return this.translate(app, textSingular, vars, count, options);
- }
- else{
+ } else {
return this.translate(app, textPlural, vars, count, options);
}
},