<commit_before>var Backbone = require('backbone'),
config = require("../config"),
api = require("../api"),
template = require("../templates/language-toggle.hbs"),
i18n = require('i18next-client');

// To change the app's language, call config.set('forever.language', lang)
// where lang is e.g. 'en', 'fr', or 'ar'.
// The config change handler below will handle the magic.

module.exports = Backbone.View.extend({
    initialize: function(){
        var language = config.get('forever.language'),
            lt = this;
        this.render();
        this.setLanguage(language);
        if (!config.isset('forever.language')) {
            this.show(true);
        };
        // Get called when forever.language is changed in the config.
        // This will (1) update the app and (2) if the user is logged in,
        // update their preferred language.
        config.change('forever.language', function (key, change, value) {
            // If user is logged in, save their language preference
            var token,
                headers = {};
            if (change === 'set') {
                // Update the app
                lt.setLanguage(value);
                if (value) {
                    // make sure language toggle is hidden
                    lt.hide(true);
                }
                token = config.get('forever.authToken');
                if (token) {
                    // user is logged in
                    // remember user's preference in the backend
                    headers.ServiceInfoAuthorization = 'Token ' + token;
                    $.ajax(api.getAPIPrefix() + 'api/language/', {
                        type: 'POST',
                        headers: headers,
                        data: {
                            'language': value
                        },
                        error: function (data) {
                            console.error(data);
                        }
                    });
                }
            }
        });
    },

    render: function() {
        var $el = this.$el;
        this.$el.html(template({

        }));
        i18n.init(function(t){

        })
    },

    setLanguage: function(lang) {
        i18n.init({
            fallbackLng: 'en',
            lng: lang
        }, function(t){
            $("body").i18n();
        });
        if (lang === 'ar') {
            /* RIGHT to LEFT */
            $('body').attr('dir', 'rtl');
            $('body').removeClass('left-to-right');
            $('body').addClass('right-to-left');
            $('link.load-style').attr('href', "styles/site-rtl.css");
        } else {
            $('body').attr('dir', 'ltr');
            $('body').removeClass('right-to-left');
            $('body').addClass('left-to-right');
            $('link.load-style').attr('href', "styles/site-ltr.css");
        }
    },

    hide: function(immediate) {
        var target;
        if (window.innerHeight > window.innerWidth){
            target = $('#menu-toggle').position();
        } else {
            target = $('#menu-container').find('.menu-item-language').position();
        }
        var anim = {
            top: target.top - $('.language-toggle').height()/2,
            left: target.left,
        };
        this.$el.css(this.$el.position());<commit_msg>17 Stop i18n trying to load non-existent language files<commit_after>var Backbone = require('backbone'),
config = require("../config"),
api = require("../api"),
template = require("../templates/language-toggle.hbs"),
i18n = require('i18next-client');

// To change the app's language, call config.set('forever.language', lang)
// where lang is e.g. 'en', 'fr', or 'ar'.
// The config change handler below will handle the magic.

module.exports = Backbone.View.extend({
    initialize: function(){
        var language = config.get('forever.language'),
            lt = this;
        this.render();
        this.setLanguage(language);
        if (!config.isset('forever.language')) {
            this.show(true);
        };
        // Get called when forever.language is changed in the config.
        // This will (1) update the app and (2) if the user is logged in,
        // update their preferred language.
        config.change('forever.language', function (key, change, value) {
            // If user is logged in, save their language preference
            var token,
                headers = {};
            if (change === 'set') {
                // Update the app
                lt.setLanguage(value);
                if (value) {
                    // make sure language toggle is hidden
                    lt.hide(true);
                }
                token = config.get('forever.authToken');
                if (token) {
                    // user is logged in
                    // remember user's preference in the backend
                    headers.ServiceInfoAuthorization = 'Token ' + token;
                    $.ajax(api.getAPIPrefix() + 'api/language/', {
                        type: 'POST',
                        headers: headers,
                        data: {
                            'language': value
                        },
                        error: function (data) {
                            console.error(data);
                        }
                    });
                }
            }
        });
    },

    render: function() {
        var $el = this.$el;
        this.$el.html(template({

        }));
         /* We have to be firm with i18next to stop it trying to load all sorts
           of non-existent language files like 'en-US' and 'dev' */
        i18n.init(
            {
                fallbackLng: ['en', 'ar', 'fr'],
                lngWhitelist: ['en', 'ar', 'fr'],
                preload: ['en', 'ar', 'fr'],
                useCookie: false
            },
            function () {}
        );
    },

    setLanguage: function(lang) {
        i18n.init({
            lng: lang
        }, function(t){
            $("body").i18n();
        });
        if (lang === 'ar') {
            /* RIGHT to LEFT */
            $('body').attr('dir', 'rtl');
            $('body').removeClass('left-to-right');
            $('body').addClass('right-to-left');
            $('link.load-style').attr('href', "styles/site-rtl.css");
        } else {
            $('body').attr('dir', 'ltr');
            $('body').removeClass('right-to-left');
            $('body').addClass('left-to-right');
            $('link.load-style').attr('href', "styles/site-ltr.css");
        }
    },

    hide: function(immediate) {
        var target;
        if (window.innerHeight > window.innerWidth){
            target = $('#menu-toggle').position();
        } else {
            target = $('#menu-container').find('.menu-item-language').position();
        }
        var anim = {
            top: target.top - $('.language-toggle').height()/2,
            left: target.left,
        };
        this.$el.css(this.$el.position());
{'fraction_non_alphanumeric': 0.15312098271675476, 'fraction_numerical': 0.004570775603485217, 'mean_word_length': 1.4004113815563937, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4311884', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1800, 'n_words': 433}