<commit_before>/**
 * I18n Service Provider
 * Load your translations and update $rootScope
 * It gives you access to your translation.
 */
module.exports = function() {

    var i18n = {
        current: "",
        data: {},
        available: [],
        currentState: "",
        active: false
    }

    /**
     * Default configuration for the module
     * @type {Object}
     */
    var config = {
        state: "home",
        lang: "en-EN",
        url: "/i18n/languages.json",
        namespace: "",
        lazy: false,
        urls: [
            {
                lang: "",
                url: ""
            }
        ],
        current: ""
    };

    /**
     * Load your url from lazy mode
     * @return {String} url
     */
    function loadLazyDefaultUrl() {

        var url = config.url;
        if(config.lazy) {
            url = config.urls.filter(function (o) {
                return o.lang === config.lang;
            })[0].url;
        }
        return url;
    }

    /**
     * Build i18n.data.
     * @param  {Object} data Data from the translation file
     */
    function buildI18n(data) {

        if(!config.lazy) {
            i18n.data = data;
            return;
        }

        i18n.data[i18n.current] = data;
    }

    /**
     * Configure the service with a provider from the config of your module
     * @param  {Object} params Configuration object
     * @return {void}
     */
    this.languages = function languagesConfig(params) {
        angular.extend(config, params);
    }

    /**
     * Babelfish service
     */
    this.$get = ['$rootScope', '$http', function($rootScope, $http) {

        /**
         * Load a translation to the $scope
         * - doc BCP 47 {@link http://tools.ietf.org/html/bcp47}
         * - doc Value of HTML5 lang attr {@link http://webmasters.stackexchange.com/questions/28307/value-of-the-html5-lang-attribute}
         * @param {String} lang Your language cf BCP 47
         */
        function setTranslation(page) {

            page = page || config.state;
            i18n.currentState = page;
            lang = i18n.current;
            i18n.active = true;

            if(i18n.data[lang]) {
                angular.extend(i18n.data[lang]['_common'], {languages: i18n.available});
                angular.extend(i18n.data[lang][page], i18n.data[lang]['_common']);

                if(config.namespace) {
                    $rootScope[config.namespace] = i18n.data[lang][page];
                }else {
                    angular.extend($rootScope, i18n.data[lang][page]);
                }
            }
        }

        /**
         * Load a translation to the $scope for a language
         * - doc BCP 47 {@link http://tools.ietf.org/html/bcp47}
         * - doc Value of HTML5 lang attr {@link http://webmasters.stackexchange.com/questions/28307/value-of-the-html5-lang-attribute}
         * @trigger {Event} i18n:babelfish:changed {previous:XXX,value:XXX2}
         * @param {String} lang Your language cf BCP 47
         */
        function loadLanguage(lang) {

            var old = document.documentElement.lang;
            if(!lang) {
                lang =  (old + '-' + old.toUpperCase()) || 'en-EN';
            }else {
                document.documentElement.lang = lang.split('-')[0];
            }

            config.lang = i18n.current = lang;

            $rootScope.$emit('i18n:babelfish:changed', {
                previous: (old + '-' + old.toUpperCase()),<commit_msg>Init #5 - Patch the error<commit_after>/**
 * I18n Service Provider
 * Load your translations and update $rootScope
 * It gives you access to your translation.
 */
module.exports = function() {

    var i18n = {
        current: "",
        data: {},
        available: [],
        currentState: "",
        active: false
    }

    /**
     * Default configuration for the module
     * @type {Object}
     */
    var config = {
        state: "home",
        lang: "en-EN",
        url: "/i18n/languages.json",
        namespace: "",
        lazy: false,
        urls: [
            {
                lang: "",
                url: ""
            }
        ],
        current: ""
    };

    /**
     * Load your url from lazy mode
     * @return {String} url
     */
    function loadLazyDefaultUrl() {

        var url = config.url;
        if(config.lazy) {
            url = config.urls.filter(function (o) {
                return o.lang === config.lang;
            })[0].url;
        }
        return url;
    }

    /**
     * Build i18n.data.
     * @param  {Object} data Data from the translation file
     */
    function buildI18n(data) {

        if(!config.lazy) {
            i18n.data = data;
            return;
        }

        i18n.data[i18n.current] = data;
    }

    /**
     * Configure the service with a provider from the config of your module
     * @param  {Object} params Configuration object
     * @return {void}
     */
    this.languages = function languagesConfig(params) {
        angular.extend(config, params);
    }

    /**
     * Babelfish service
     */
    this.$get = ['$rootScope', '$http', function($rootScope, $http) {

        /**
         * Load a translation to the $scope
         * - doc BCP 47 {@link http://tools.ietf.org/html/bcp47}
         * - doc Value of HTML5 lang attr {@link http://webmasters.stackexchange.com/questions/28307/value-of-the-html5-lang-attribute}
         * @param {String} lang Your language cf BCP 47
         */
        function setTranslation(page) {

            page = page || config.state;
            i18n.currentState = page;
            lang = i18n.current;
            i18n.active = true;

            if(i18n.data[lang]) {

                /**
                 * Prevent the error
                 *     > TypeError: Cannot read property '$$hashKey' of undefined
                 * cf {@link https://github.com/dhoko/serval-i18n/issues/5}
                 */
                if(!i18n.data[lang][page]) {
                    i18n.data[lang][page] = {};
                    console.warn('[serval-i18n-babelfish@setTranslation] No translation available for the page %s for the lang %s',page, lang);
                }

                angular.extend(i18n.data[lang]['_common'], {languages: i18n.available});
                angular.extend(i18n.data[lang][page], i18n.data[lang]['_common']);

                if(config.namespace) {
                    $rootScope[config.namespace] = i18n.data[lang][page];
                }else {
                    angular.extend($rootScope, i18n.data[lang][page]);
                }
            }
        }

        /**
         * Load a translation to the $scope for a language
         * - doc BCP 47 {@link http://tools.ietf.org/html/bcp47}
         * - doc Value of HTML5 lang attr {@link http://webmasters.stackexchange.com/questions/28307/value-of-the-html5-lang-attribute}
         * @trigger {Event} i18n:babelfish:changed {previous:XXX,value:XXX2}
         * @param {String} lang Your language cf BCP 47
         */
        function loadLanguage(lang) {

            var old = document.documentElement.lang;
            if(!lang) {
                lang =  (old + '-' + old.toUpperCase()) || 'en-EN';
            }else {
                document.documentElement.lang = lang.split('-')[0];
            }

            config.lang = i18n.current = lang;

            $rootScope.$emit('i18n:babelfish:changed', {
                previous: (old + '-' + old.toUpperCase()),
{'fraction_non_alphanumeric': 0.13187399893219434, 'fraction_numerical': 0.0202883075280299, 'mean_word_length': 1.5731456043956045, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 1, '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': '5908982', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2167, 'n_words': 484}