PageSourceSearch

https://portal.owncloud.com/core/js/l10n.js?v=dd2774748f47720d9deea7a9f29fea34

js owncloud.com collected 2026-09-24 08:49:00 UTC 7,553 bytes, 238 lines download raw bytes

1/**
2 * Copyright (c) 2014 Vincent Petry <[email protected]>
3 *
4 * This file is licensed under the Affero General Public License version 3
5 * or later.
6 *
7 * See the COPYING-README file.
8 *
9 */
10
11/**
12 * L10N namespace with localization functions.
13 *
14 * @namespace
15 */
16OC.L10N = {
17	/**
18	 * String bundles with app name as key.
19	 * @type {Object.<String,String>}
20	 */
21	_bundles: {},
22
23	/**
24	 * Plural functions, key is app name and value is function.
25	 * @type {Object.<String,Function>}
26	 */
27	_pluralFunctions: {},
28
29	/**
30	 * Load an app's translation bundle if not loaded already.
31	 *
32	 * @param {String} appName name of the app
33	 * @param {Function} callback callback to be called when
34	 * the translations are loaded
35	 * @return {Promise} promise
36	 */
37	load: function(appName, callback) {
38		// already available ?
39		if (this._bundles[appName] || OC.getLocale() === 'en') {
40			var deferred = $.Deferred();
41			var promise = deferred.promise();
42			promise.then(callback);
43			deferred.resolve();
44			return promise;
45		}
46
47		var self = this;
48		var url = OC.filePath(appName, 'l10n', OC.getLocale() + '.json');
49
50		// load JSON translation bundle per AJAX
51		return $.get(url)
52			.then(
53				function(result) {
54					if (result.translations) {
55						self.register(appName, result.translations, result.pluralForm);
56					}
57				})
58			.then(callback);
59	},
60
61	/**
62	 * Register an app's translation bundle.
63	 *
64	 * @param {String} appName name of the app
65	 * @param {Object<String,String>} bundle
66	 * @param {Function|String} [pluralForm] optional plural function or plural string
67	 */
68	register: function(appName, bundle, pluralForm) {
69		var self = this;
70		if (_.isUndefined(this._bundles[appName])) {
71			this._bundles[appName] = bundle || {};
72
73			if (_.isFunction(pluralForm)) {
74				this._pluralFunctions[appName] = pluralForm;
75			} else {
76				// generate plural function based on form
77				this._pluralFunctions[appName] = this._generatePluralFunction(pluralForm);
78			}
79		} else {
80			// Theme overwriting the default language
81			_.extend(self._bundles[appName], bundle);
82		}
83	},
84
85	/**
86	 * Generates a plural function based on the given plural form.
87	 * If an invalid form has been given, returns a default function.
88	 *
89	 * @param {String} pluralForm plural form
90	 */
91	_generatePluralFunction: function(pluralForm) {
92		// default func
93		var func = function (n) {
94			var p = (n !== 1) ? 1 : 0;
95			return { 'nplural' : 2, 'plural' : p };
96		};
97
98		if (!pluralForm) {
99			console.warn('Missing plural form in language file');
100			return func;
101		}
102
103		/**
104		 * code below has been taken from jsgettext - which is LGPL licensed
105		 * https://developer.berlios.de/projects/jsgettext/
106		 * http://cvs.berlios.de/cgi-bin/viewcvs.cgi/jsgettext/jsgettext/lib/Gettext.js
107		 */
108		var pf_re = new RegExp('^(\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;a-zA-Z0-9_\\(\\)])+)', 'm');
109		if (pf_re.test(pluralForm)) {
110			//ex english: "Plural-Forms: nplurals=2; plural=(n != 1);\n"
111			//pf = "nplurals=2; plural=(n != 1);";
112			//ex russian: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10< =4 && (n%100<10 or n%100>=20) ? 1 : 2)
113			//pf = "nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)";
114			var pf = pluralForm;
115			if (! /;\s*$/.test(pf)) {
116				pf = pf.concat(';');
117			}
118			/* We used to use eval, but it seems IE has issues with it.
119			 * We now use "new Function", though it carries a slightly
120			 * bigger performance hit.
121			var code = 'function (n) { var plural; var nplurals; '+pf+' return { "nplural" : nplurals, "plural" : (plural === true ? 1 : plural ? plural : 0) }; };';
122			Gettext._locale_data[domain].head.plural_func = eval("("+code+")");
123			 */
124			var code = 'var plural; var nplurals; '+pf+' return { "nplural" : nplurals, "plural" : (plural === true ? 1 : plural ? plural : 0) };';
125			func = new Function("n", code);
126		} else {
127			console.warn('Invalid plural form in language file: "' + pluralForm + '"');
128		}
129		return func;
130	},
131
132	/**
133	 * Translate a string
134	 * @param {string} app the id of the app for which to translate the string
135	 * @param {string} text the string to translate
136	 * @param [vars] map of placeholder key to value
137	 * @param {number} [count] number to replace %n with
138	 * @param {array} [options] options array
139	 * @param {bool} [options.escape=true] enable/disable auto escape of placeholders (by default enabled)
140	 * @return {string}
141	 */
142	translate: function(app, text, vars, count, options) {
143		var defaultOptions = {
144				escape: true
145			},
146			allOptions = options || {};
147		_.defaults(allOptions, defaultOptions);
148
149		// TODO: cache this function to avoid inline recreation
150		// of the same function over and over again in case
151		// translate() is used in a loop
152		var _build = function (text, vars, count) {
153			return text.replace(/%n/g, count).replace(/{([^{}]*)}/g,
154				function (a, b) {
155					var r = vars[b];
156					if(typeof r === 'string' || typeof r === 'number') {
157						if(allOptions.escape) {
158							return escapeHTML(r);
159						} else {
160							return r;
161						}
162					} else {
163						return a;
164					}
165				}
166			);
167		};
168		var translation = text;
169		var bundle = this._bundles[app] || {};
170		var value = bundle[text];
171		if( typeof(value) !== 'undefined' ){
172			translation = value;
173		}
174
175		if(typeof vars === 'object' || count !== undefined ) {
176			return _build(translation, vars, count);
177		} else {
178			return translation;
179		}
180	},
181
182	/**
183	 * Translate a plural string
184	 * @param {string} app the id of the app for which to translate the string
185	 * @param {string} textSingular the string to translate for exactly one object
186	 * @param {string} textPlural the string to translate for n objects
187	 * @param {number} count number to determine whether to use singular or plural
188	 * @param [vars] map of placeholder key to value
189	 * @param {array} [options] options array
190	 * @param {bool} [options.escape=true] enable/disable auto escape of placeholders (by default enabled)
191	 * @return {string} Translated string
192	 */
193	translatePlural: function(app, textSingular, textPlural, count, vars, options) {
194		var identifier = '_' + textSingular + '_::_' + textPlural + '_';
195		var bundle = this._bundles[app] || {};
196		var value = bundle[identifier];
197		if( typeof(value) !== 'undefined' ){
198			var translation = value;
199			if ($.isArray(translation)) {
200				var plural = this._pluralFunctions[app](count);
201				return this.translate(app, translation[plural.plural], vars, count, options);
202			}
203		}
204
205		if(count === 1) {
206			return this.translate(app, textSingular, vars, count, options);
207		}
208		else{
209			return this.translate(app, textPlural, vars, count, options);
210		}
211	}
212};
213
214/**
215 * translate a string
216 * @param {string} app the id of the app for which to translate the string
217 * @param {string} text the string to translate
218 * @param [vars] map of placeholder key to value
219 * @param {number} [count] number to replace %n with
220 * @return {string}
221 */
222window.t = _.bind(OC.L10N.translate, OC.L10N);
223
224/**
225 * translate a string
226 * @param {string} app the id of the app for which to translate the string
227 * @param {string} text_singular the string to translate for exactly one object
228 * @param {string} text_plural the string to translate for n objects
229 * @param {number} count number to determine whether to use singular or plural
230 * @param [vars] map of placeholder key to value
231 * @return {string} Translated string
232 */
233window.n = _.bind(OC.L10N.translatePlural, OC.L10N);
234
235Handlebars.registerHelper('t', function(app, text) {
236	return OC.L10N.translate(app, text);
237});
238

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.