<commit_before>/**
 * Copyright (c) 2014 <NAME> <<EMAIL>>
 *
 * This file is licensed under the Affero General Public License version 3
 * or later.
 *
 * See the COPYING-README file.
 *
 */

/**
 * L10N namespace with localization functions.
 *
 * @namespace
 */
OC.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.
	 *
	 * @param {String} appName name of the app
	 * @param {Function} callback callback to be called when
	 * the translations are loaded
	 * @return {Promise} promise
	 */
	load: function(appName, callback) {
		// already available ?
		if (this._bundles[appName] || OC.getLocale() === 'en') {
			if (callback) {
				callback();
			}
			return;
		}

		var self = this;
		var deferred = $.Deferred();
		var url = OC.filePath(appName, 'l10n', OC.getLocale() + '.json');

		// load JSON translation bundle per AJAX
		$.get(url,
			function(result) {
				if (result.translations) {
					self.register(appName, result.translations, result.pluralForm);
				}
				if (callback) {
					callback();
					deferred.resolve();
				}
			}
		);
		return deferred.promise();
	},

	/**
	 * Register an app's translation bundle.
	 *
	 * @param {String} appName name of the app
	 * @param {Object<String,String>} strings bundle
	 * @param {{Function|String}} [pluralForm] optional plural function or plural string
	 */
	register: function(appName, bundle, pluralForm) {
		this._bundles[appName] = bundle || {};

		if (_.isFunction(pluralForm)) {
			this._pluralFunctions[appName] = pluralForm;
		} else {
			// generate plural function based on form
			this._pluralFunctions[appName] = this._generatePluralFunction(pluralForm);
		}
	},

	/**
	 * Generates a plural function based on the given plural form.
	 * If an invalid form has been given, returns a default function.
	 *
	 * @param {String} pluralForm plural form
	 */
	_generatePluralFunction: function(pluralForm) {<commit_msg>Better use of promise in OC.L10N.load()<commit_after>/**
 * Copyright (c) 2014 <NAME> <<EMAIL>>
 *
 * This file is licensed under the Affero General Public License version 3
 * or later.
 *
 * See the COPYING-README file.
 *
 */

/**
 * L10N namespace with localization functions.
 *
 * @namespace
 */
OC.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.
	 *
	 * @param {String} appName name of the app
	 * @param {Function} callback callback to be called when
	 * the translations are loaded
	 * @return {Promise} promise
	 */
	load: function(appName, callback) {
		// already available ?
		if (this._bundles[appName] || OC.getLocale() === 'en') {
			if (callback) {
				callback();
			}
			return;
		}

		var self = this;
		var url = OC.filePath(appName, 'l10n', OC.getLocale() + '.json');

		// load JSON translation bundle per AJAX
		return $.get(url)
			.then(
				function(result) {
					if (result.translations) {
						self.register(appName, result.translations, result.pluralForm);
					}
				})
			.then(callback);
	},

	/**
	 * Register an app's translation bundle.
	 *
	 * @param {String} appName name of the app
	 * @param {Object<String,String>} strings bundle
	 * @param {{Function|String}} [pluralForm] optional plural function or plural string
	 */
	register: function(appName, bundle, pluralForm) {
		this._bundles[appName] = bundle || {};

		if (_.isFunction(pluralForm)) {
			this._pluralFunctions[appName] = pluralForm;
		} else {
			// generate plural function based on form
			this._pluralFunctions[appName] = this._generatePluralFunction(pluralForm);
		}
	},

	/**
	 * Generates a plural function based on the given plural form.
	 * If an invalid form has been given, returns a default function.
	 *
	 * @param {String} pluralForm plural form
	 */
	_generatePluralFunction: function(pluralForm) {
{'fraction_non_alphanumeric': 0.13364928909952606, 'fraction_numerical': 0.005687203791469194, 'mean_word_length': 3.2983706720977595, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3572976', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1284, 'n_words': 401}