elgg-gitbot / test2

0 stars 0 forks source link

js elgg_echo doesn't fall back to default translation (Trac #4390) #7

Open elgg-gitbot opened 11 years ago

elgg-gitbot commented 11 years ago

Original ticket http://trac.elgg.org/ticket/4390 on 2012-02-29 by trac user coldtrick, assigned to unknown.

Elgg version: 1.8.3

Only the current user language is js loaded in js/lib/languages. Also the elgg.elgg_echo is not falling back to site language when user language key is not found.

This situation is very irritating when you are using a lot of elgg_echo's in js and have your site in a different language than "en"

I provide a quick fix here, but maybe cash can consider this in his translations caching system.

------- THE FIX -------

elgg.config.translations.init = function() {
    elgg.reload_all_translations();
    if(elgg.get_language() !== elgg.config.language){
        elgg.reload_all_translations(elgg.config.language);
    }
};

and

elgg.echo = function(key, argv, language) {
    //elgg.echo('str', 'en')
    if (elgg.isString(argv)) {
        language = argv;
        argv = [];
    }

    //elgg.echo('str', [...], 'en')
    var translations = elgg.config.translations,
        dlang = elgg.get_language(),
        map;

    language = language || dlang;
    argv = argv || [];

    map = translations[language] || translations[dlang];
    if (map && map[key]) {
        return vsprintf(map[key], argv);
    }
    if(dlang !== elgg.config.language){
        map = translations[elgg.config.language];
        if (map && map[key]) {
            return vsprintf(map[key], argv);
        }
    }

    return key;
};
elgg-gitbot commented 11 years ago

trac user coldtrick wrote on 2012-02-29

Also a fix in the reload_all_translation function is needed to prevent a duplicate init.

elgg.reload_all_translations = function(language) {
    var lang = language || elgg.get_language();

    var url, options;
    if (elgg.config.simplecache_enabled) {
        url = 'cache/js/default/languages/' + lang + '.' + elgg.config.lastcache + '.js';
        options = {};
    } else {
        url = 'ajax/view/js/languages';
        options = {data: {language: lang}};
    }

    options['success'] = function(json) {
        elgg.add_translation(lang, json);
        if(elgg.config.languageReady !== true){
            elgg.config.languageReady = true;
            elgg.initWhenReady();
        }
    };

    elgg.getJSON(url, options);
};
elgg-gitbot commented 11 years ago

trac user coldtrick wrote on 2012-02-29

Better to pull the initWhenReady out of the reload function...

elgg-gitbot commented 11 years ago

ewinslow wrote on 2012-05-03

requirejs would make all of this so much easier...

elgg-gitbot commented 11 years ago

Milestone changed to Near Term Future Release by ewinslow on 2012-05-03