Fix js l10n registration to also work with more than one bundle
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
parent
bc276cdd83
commit
409acf1131
10 changed files with 112 additions and 66 deletions
33
build/package-lock.json
generated
33
build/package-lock.json
generated
|
@ -1493,10 +1493,6 @@
|
|||
"concat-map": "0.0.1"
|
||||
}
|
||||
},
|
||||
"chownr": {
|
||||
"version": "1.0.1",
|
||||
"bundled": true
|
||||
},
|
||||
"code-point-at": {
|
||||
"version": "1.1.0",
|
||||
"bundled": true,
|
||||
|
@ -1548,13 +1544,6 @@
|
|||
"dev": true,
|
||||
"optional": true
|
||||
},
|
||||
"fs-minipass": {
|
||||
"version": "1.2.5",
|
||||
"bundled": true,
|
||||
"requires": {
|
||||
"minipass": "^2.2.1"
|
||||
}
|
||||
},
|
||||
"fs.realpath": {
|
||||
"version": "1.0.0",
|
||||
"bundled": true,
|
||||
|
@ -1667,22 +1656,6 @@
|
|||
"dev": true,
|
||||
"optional": true
|
||||
},
|
||||
"minipass": {
|
||||
"version": "2.2.4",
|
||||
"bundled": true,
|
||||
"optional": true,
|
||||
"requires": {
|
||||
"safe-buffer": "^5.1.1",
|
||||
"yallist": "^3.0.0"
|
||||
}
|
||||
},
|
||||
"minizlib": {
|
||||
"version": "1.1.0",
|
||||
"bundled": true,
|
||||
"requires": {
|
||||
"minipass": "^2.2.1"
|
||||
}
|
||||
},
|
||||
"mkdirp": {
|
||||
"version": "0.5.1",
|
||||
"bundled": true,
|
||||
|
@ -1867,6 +1840,7 @@
|
|||
"safe-buffer": {
|
||||
"version": "5.1.1",
|
||||
"bundled": true,
|
||||
"dev": true,
|
||||
"optional": true
|
||||
},
|
||||
"safer-buffer": {
|
||||
|
@ -1954,11 +1928,6 @@
|
|||
"bundled": true,
|
||||
"dev": true,
|
||||
"optional": true
|
||||
},
|
||||
"yallist": {
|
||||
"version": "3.0.2",
|
||||
"bundled": true,
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
BIN
core/js/dist/login.js
vendored
BIN
core/js/dist/login.js
vendored
Binary file not shown.
BIN
core/js/dist/login.js.map
vendored
BIN
core/js/dist/login.js.map
vendored
Binary file not shown.
BIN
core/js/dist/main.js
vendored
BIN
core/js/dist/main.js
vendored
Binary file not shown.
BIN
core/js/dist/main.js.map
vendored
BIN
core/js/dist/main.js.map
vendored
Binary file not shown.
BIN
core/js/dist/maintenance.js
vendored
BIN
core/js/dist/maintenance.js
vendored
Binary file not shown.
BIN
core/js/dist/maintenance.js.map
vendored
BIN
core/js/dist/maintenance.js.map
vendored
Binary file not shown.
|
@ -15,7 +15,7 @@ describe('OC.L10N tests', function() {
|
|||
OC.appswebroots[TEST_APP] = OC.getRootPath() + '/apps3/jsunittestapp';
|
||||
});
|
||||
afterEach(function() {
|
||||
delete OC.L10N._bundles[TEST_APP];
|
||||
OC.L10N._unregister(TEST_APP);
|
||||
delete OC.appswebroots[TEST_APP];
|
||||
});
|
||||
|
||||
|
@ -29,7 +29,7 @@ describe('OC.L10N tests', function() {
|
|||
});
|
||||
});
|
||||
it('returns untranslated text when no bundle exists', function() {
|
||||
delete OC.L10N._bundles[TEST_APP];
|
||||
OC.L10N._unregister(TEST_APP);
|
||||
expect(t(TEST_APP, 'unknown text')).toEqual('unknown text');
|
||||
});
|
||||
it('returns untranslated text when no key exists', function() {
|
||||
|
|
88
core/src/OC/l10n-registry.js
Normal file
88
core/src/OC/l10n-registry.js
Normal file
|
@ -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],
|
||||
}
|
||||
}
|
|
@ -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,21 +64,16 @@ 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
|
||||
|
@ -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);
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue