From 7e42aca65b63706d7ad67df3c86c52b551acdd41 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 13 Aug 2018 18:34:07 +0200 Subject: [PATCH] fix #2170 @2 --- js/libs/fluidbook/fluidbook.l10n.js | 76 +++++++++++++++-------------- js/libs/fluidbook/fluidbook.nav.js | 4 +- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/js/libs/fluidbook/fluidbook.l10n.js b/js/libs/fluidbook/fluidbook.l10n.js index de956fe2..ed9e8a22 100644 --- a/js/libs/fluidbook/fluidbook.l10n.js +++ b/js/libs/fluidbook/fluidbook.l10n.js @@ -3,18 +3,20 @@ function FluidbookL10N(fluidbook, lang) { this.multilang = []; this.multilangEnabled = false; this.fluidbook = fluidbook; - - this.load(lang); + this.init(lang); this.initMultilang(); + this.load(); } FluidbookL10N.prototype = { - load: function (lang) { - if (lang == undefined || lang == null || lang == '') { + init: function (lang) { + if (lang === undefined || lang === null || lang === '') { lang = 'default'; } this.lang = lang; + }, + load: function () { this.dir = this.getLanguageDirection(this.getActiveLang()); this.ltr = (this.dir == 'ltr'); this.rtl = !this.ltr; @@ -25,20 +27,26 @@ FluidbookL10N.prototype = { $("html").removeClass('rtl').addClass('ltr'); } - if (lang == 'default') { + if (this.lang === 'default') { this.translations = this.fluidbook.datas.l10n[this.fluidbook.datas.defaultLang]; - $.extend(this.translations, this.fluidbook.datas.l10n[lang]); + $.extend(this.translations, this.fluidbook.datas.l10n[this.lang]); } else { - this.translations = this.fluidbook.datas.l10n[lang]; + this.translations = this.fluidbook.datas.l10n[this.lang]; } this.updateTranslations(); }, getActiveLang: function () { - if (this.lang != 'default') { - return this.lang; + var res = this.lang; + if (this.lang === 'default') { + res = this.fluidbook.datas.defaultLang; } - return this.fluidbook.datas.defaultLang; + + // Append country when lang is present more than one time in the list + if (this._langsCount[res] > 1) { + res += '_' + this.fluidbook.datas.country; + } + return res; }, initMultilang: function () { var forceLocales; @@ -73,6 +81,8 @@ FluidbookL10N.prototype = { var ml = this.fluidbook.datas.multilang.replace(/\r/g, "\n").replace(/\n+/g, "\n"); var e = ml.split("\n"); + this._langsCount = {}; + var versions = []; $.each(e, function (k, v) { if (v == '') { return; @@ -96,35 +106,24 @@ FluidbookL10N.prototype = { var o = {lang: l[0], flag: l[1], url: url, langName: l[3], countryName: l[4]}; - // $this.multilang[$this.multilang.length] = o; - var languageID = l[0] + '_' + l[1]; - $this.multilang[languageID] = o; // Indexed by language code + flag to allow easier lookups later and allow multiple variations of the same language - }); - }, - getActiveFlag: function () { - var active = this.getActiveLang(); - var res = []; - for (var i = 0; i < this.multilang.length; i++) { - var l = this.multilang[i]; - if (l.lang == active) { - res.push(l); + if ($this._langsCount[o.lang] === undefined) { + $this._langsCount[o.lang] = 0; } - } + $this._langsCount[o.lang]++; + versions.push(o); + }); - if (res.length == 1) { - return res[0].flag; - } else if (res.length == 0) { - return false; - } else { - if (this.fluidbook.datas.country != '') { - for (var i in res) { - if (res[i].flag == this.fluidbook.datas.country) { - return res[i].flag; - } - } - return res[0].flag; + + $.each(versions, function (k, version) { + // $this.multilang[$this.multilang.length] = o; + var languageId = version.lang; + if ($this._langsCount[version.lang] > 1) { + languageId += '_' + version.flag; } - } + version.id = languageId; + $this.multilang[languageId] = version; // Indexed by language code + flag to allow easier lookups later and allow multiple variations of the same language + }); + }, getCurrentLanguageName: function () { @@ -161,6 +160,9 @@ FluidbookL10N.prototype = { // Determine if a language is LTR or RTL based on its code getLanguageDirection: function (languageCode) { + var e = languageCode.split('_'); + languageCode = e[0]; + // Arabic and Hebrew are the only RTL languages defined for now. // ToDo: update this with a full list of RTL languages return (languageCode == 'ar' || languageCode == 'he') ? 'rtl' : 'ltr' @@ -184,7 +186,7 @@ FluidbookL10N.prototype = { if (str.substr(0, 1) == '!') { return str.substr(1); } - + if (this.translations[str] == undefined || this.translations[str] == null || this.translations[str] == '') { if (markupIfNonAvailable == undefined || !markupIfNonAvailable) { return str; diff --git a/js/libs/fluidbook/fluidbook.nav.js b/js/libs/fluidbook/fluidbook.nav.js index 7303ec1e..41fef794 100644 --- a/js/libs/fluidbook/fluidbook.nav.js +++ b/js/libs/fluidbook/fluidbook.nav.js @@ -437,9 +437,7 @@ FluidbookNav.prototype = { } } else if (icon == 'lang' && this.fluidbook.l10n.multilangEnabled) { // Note: the "!" at the beginning of the title/help parameters means that we don't want these strings translated - //link = this.addLink(navType, 'nav-locales', '#/locales', 'locales', '!' + this.fluidbook.l10n.getCurrentLanguageName(), '!Select Language'); - // Todo: remove this temporary link and re-instate original link above when ticket #2173 is done so we can display proper language name - link = this.addLink(navType, 'nav-locales', '#/locales', 'locales', '!Select Language', '!Select Language'); // Label says "Select Language" until we can have the country code recorded for the Fluidbook + link = this.addLink(navType, 'nav-locales', '#/locales', 'locales', '!' + this.fluidbook.l10n.getCurrentLanguageName(), '!Select Language'); } else if (icon == 'archives') { if (this.fluidbook.datas.archivesLink != '') { link = this.addLink(navType, 'nav-archives', this.fluidbook.datas.archivesLink, 'archives', '!' + this.fluidbook.datas.archivesLabel, '!' + this.fluidbook.datas.archivesLabel); -- 2.39.5