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
|
@ -1493,10 +1493,6 @@
|
||||||
"concat-map": "0.0.1"
|
"concat-map": "0.0.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"chownr": {
|
|
||||||
"version": "1.0.1",
|
|
||||||
"bundled": true
|
|
||||||
},
|
|
||||||
"code-point-at": {
|
"code-point-at": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
|
@ -1548,13 +1544,6 @@
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"optional": true
|
"optional": true
|
||||||
},
|
},
|
||||||
"fs-minipass": {
|
|
||||||
"version": "1.2.5",
|
|
||||||
"bundled": true,
|
|
||||||
"requires": {
|
|
||||||
"minipass": "^2.2.1"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"fs.realpath": {
|
"fs.realpath": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
|
@ -1667,22 +1656,6 @@
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"optional": 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": {
|
"mkdirp": {
|
||||||
"version": "0.5.1",
|
"version": "0.5.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
|
@ -1867,6 +1840,7 @@
|
||||||
"safe-buffer": {
|
"safe-buffer": {
|
||||||
"version": "5.1.1",
|
"version": "5.1.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
|
"dev": true,
|
||||||
"optional": true
|
"optional": true
|
||||||
},
|
},
|
||||||
"safer-buffer": {
|
"safer-buffer": {
|
||||||
|
@ -1954,11 +1928,6 @@
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"optional": true
|
"optional": true
|
||||||
},
|
|
||||||
"yallist": {
|
|
||||||
"version": "3.0.2",
|
|
||||||
"bundled": true,
|
|
||||||
"optional": true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -15,7 +15,7 @@ describe('OC.L10N tests', function() {
|
||||||
OC.appswebroots[TEST_APP] = OC.getRootPath() + '/apps3/jsunittestapp';
|
OC.appswebroots[TEST_APP] = OC.getRootPath() + '/apps3/jsunittestapp';
|
||||||
});
|
});
|
||||||
afterEach(function() {
|
afterEach(function() {
|
||||||
delete OC.L10N._bundles[TEST_APP];
|
OC.L10N._unregister(TEST_APP);
|
||||||
delete OC.appswebroots[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() {
|
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');
|
expect(t(TEST_APP, 'unknown text')).toEqual('unknown text');
|
||||||
});
|
});
|
||||||
it('returns untranslated text when no key exists', function() {
|
it('returns untranslated text when no key exists', function() {
|
||||||
|
|
|
@ -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 Handlebars from 'handlebars'
|
||||||
|
|
||||||
import OC from './index'
|
import OC from './index'
|
||||||
|
import {
|
||||||
|
getAppTranslations,
|
||||||
|
hasAppTranslations,
|
||||||
|
registerAppTranslations,
|
||||||
|
unregisterAppTranslations
|
||||||
|
} from './l10n-registry'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* L10N namespace with localization functions.
|
* L10N namespace with localization functions.
|
||||||
|
@ -20,17 +26,6 @@ import OC from './index'
|
||||||
* @namespace OC.L10n
|
* @namespace OC.L10n
|
||||||
*/
|
*/
|
||||||
const 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.
|
* Load an app's translation bundle if not loaded already.
|
||||||
|
@ -42,7 +37,7 @@ const L10n = {
|
||||||
*/
|
*/
|
||||||
load: function(appName, callback) {
|
load: function(appName, callback) {
|
||||||
// already available ?
|
// already available ?
|
||||||
if (this._bundles[appName] || OC.getLocale() === 'en') {
|
if (hasAppTranslations(appName) || OC.getLocale() === 'en') {
|
||||||
var deferred = $.Deferred();
|
var deferred = $.Deferred();
|
||||||
var promise = deferred.promise();
|
var promise = deferred.promise();
|
||||||
promise.then(callback);
|
promise.then(callback);
|
||||||
|
@ -69,21 +64,16 @@ const L10n = {
|
||||||
*
|
*
|
||||||
* @param {String} appName name of the app
|
* @param {String} appName name of the app
|
||||||
* @param {Object<String,String>} bundle
|
* @param {Object<String,String>} bundle
|
||||||
* @param {Function|String} [pluralForm] optional plural function or plural string
|
|
||||||
*/
|
*/
|
||||||
register: function(appName, bundle, pluralForm) {
|
register: function(appName, bundle, pluralForm) {
|
||||||
var self = this;
|
registerAppTranslations(appName, bundle, this._getPlural)
|
||||||
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);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private do not use this
|
||||||
|
*/
|
||||||
|
_unregister: unregisterAppTranslations,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Translate a string
|
* Translate a string
|
||||||
* @param {string} app the id of the app for which to translate the 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 translation = text;
|
||||||
var bundle = this._bundles[app] || {};
|
var bundle = getAppTranslations(app);
|
||||||
var value = bundle[text];
|
var value = bundle.translations[text];
|
||||||
if( typeof(value) !== 'undefined' ){
|
if( typeof(value) !== 'undefined' ){
|
||||||
translation = value;
|
translation = value;
|
||||||
}
|
}
|
||||||
|
@ -146,21 +136,20 @@ const L10n = {
|
||||||
* @return {string} Translated string
|
* @return {string} Translated string
|
||||||
*/
|
*/
|
||||||
translatePlural: function(app, textSingular, textPlural, count, vars, options) {
|
translatePlural: function(app, textSingular, textPlural, count, vars, options) {
|
||||||
var identifier = '_' + textSingular + '_::_' + textPlural + '_';
|
const identifier = '_' + textSingular + '_::_' + textPlural + '_';
|
||||||
var bundle = this._bundles[app] || {};
|
const bundle = getAppTranslations(app);
|
||||||
var value = bundle[identifier];
|
const value = bundle.translations[identifier];
|
||||||
if( typeof(value) !== 'undefined' ){
|
if( typeof(value) !== 'undefined' ){
|
||||||
var translation = value;
|
var translation = value;
|
||||||
if ($.isArray(translation)) {
|
if ($.isArray(translation)) {
|
||||||
var plural = this._pluralFunctions[app](count);
|
var plural = bundle.pluralFunction(count);
|
||||||
return this.translate(app, translation[plural], vars, count, options);
|
return this.translate(app, translation[plural], vars, count, options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(count === 1) {
|
if (count === 1) {
|
||||||
return this.translate(app, textSingular, vars, count, options);
|
return this.translate(app, textSingular, vars, count, options);
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
return this.translate(app, textPlural, vars, count, options);
|
return this.translate(app, textPlural, vars, count, options);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue