i18n.js 6.5 KB
Newer Older
1 2
/**
 *  ABook Viewer for WEB
Takumi Imai committed
3
 *  Common processing for internationalization (language switching)
4
 *
Takumi Imai committed
5
 *  For language resource files, modify the following files according to the language you specify
Takumi Imai committed
6 7 8
 *   - ja: lang-ja.json
 *   - ko: lang-ko.json
 *   - en : lang-en.json
9 10 11 12
 *
 *  Copyright (C) Agentec Co, Ltd. All rights reserved.
 */

Takumi Imai committed
13
//Prepare objects for the global namespace
14 15
var I18N = {};

16
/**
Takumi Imai committed
17
 * Constant: Language file location
18
 */
19 20 21 22
I18N.avwsys_location = '';
I18N.avwsys_dir = '';
I18N.avwsys_storagekey = '';
I18N.avwsys_currLang = '';
23

Takumi Imai committed
24
/* Language initialization */
25 26
$(function () {
    I18N.initi18n();
Masaru Abe committed
27 28
});

Takumi Imai committed
29
/** Initialization of language resource settings */
30 31 32 33 34 35
I18N.initi18n = function () {
    I18N.avwsys_location = '/common/json/lang';
    I18N.avwsys_dir = '/abweb';
    I18N.avwsys_storagekey = 'AVWUS_Lang';
    I18N.avwsys_currLang = 'AVW_CurrLang';

Takumi Imai committed
36
    // Login screen/direct access measures
37 38
    var location = window.location.toString().toLowerCase();
    if (location.indexOf(I18N.avwsys_dir) < 0) {
Takumi Imai committed
39
        // If not under I18N.avwsys_dir directory, add I18N.avwsys_dir directory
40 41
        I18N.avwsys_location = '..' + I18N.avwsys_dir + I18N.avwsys_location;
    } else {
Takumi Imai committed
42
        // If under I18N.avwsys_dir directory, convert to relative path
43 44 45 46 47 48 49 50 51 52 53
        I18N.avwsys_location = '..' + I18N.avwsys_location;
    }

    var lang = 'en';
    var storage = window.localStorage;
    if (storage) {
        var lang = storage.getItem(I18N.avwsys_storagekey);
        if (!lang) {
            lang = I18N.getNavigatorLanguage();
        }
    }
Takumi Imai committed
54
    // Initialize language files
55
    I18N.loadLanguage(lang);
56
};
Masaru Abe committed
57

Takumi Imai committed
58
/* Retrieve browser language settings */
59 60
I18N.getNavigatorLanguage = function () {
    var lang = navigator.browserLanguage || navigator.language || navigator.userLanguage;
Takumi Imai committed
61 62
    /* Languages Supported */
    var languages = ['ja', 'ko', 'en']; // Change here if you want to add more supported languages
63 64 65 66 67 68 69 70 71
    if (lang.match(/ja|ko|en/g)) {
        for (var i = 0; i < languages.length; i++) {
            var index = lang.indexOf(languages[i]);
            if (index >= 0) {
                lang = lang.substring(index, 2);
                break;
            }
        }
    } else {
Takumi Imai committed
72
        lang = 'en'; // If there is no supported language, English is the default.
73 74
    }
    return lang;
75 76
};

Takumi Imai committed
77 78 79 80
/**
 * Language resource file loading
 * @param {*} lang
 */
81
I18N.loadLanguage = function (lang) {
Takumi Imai committed
82
    // Select language file from arguments
83 84
    var langfile = 'lang-' + lang + '.json';

Takumi Imai committed
85
    // Read the language file
86 87 88 89 90 91
    $.ajax({
        url: I18N.avwsys_location + '/' + langfile,
        async: false,
        dataType: 'json',
        cache: false,
        success: function (data) {
Takumi Imai committed
92
            // Rewriting the lang attribute
93 94
            document.documentElement.lang = lang;

Takumi Imai committed
95
            // Rewrite html language data
96 97 98
            var jsonLangData = data;
            I18N.replaceText(jsonLangData);

Takumi Imai committed
99
            // Cache language settings and language data in storage
100 101 102 103 104 105 106 107
            I18N.storeCurrentLanguage(lang, jsonLangData);
        },
        error: function (xhr, txtStatus, errorThrown) {
            var error = 'Could not load a language file ' + langfile + '. please check it.';
            error += '\n' + xhr.status + ' ' + txtStatus + ' ' + errorThrown + ' : ' + langfile;
            alert(error);
        },
    });
108 109
};

Takumi Imai committed
110 111 112 113
/**
 * Replace all text on the page with the language
 * @param {*} jsonLangData
 */
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
I18N.replaceText = function (jsonLangData) {
    var itemCount = $('.lang').length;
    if (itemCount > 0) {
        for (var i = 0; i < itemCount; i++) {
            var obj = $('.lang:eq(' + i + ')');
            var langId = obj.attr('lang');
            if (langId) {
                var langText = I18N.getLangText(jsonLangData, langId);
                var tn = obj.get()[0].localName;
                if (tn == 'input') {
                    if (obj.attr('type') == 'button' || obj.attr('type') == 'submit') {
                        obj.val(langText);
                    } else {
                        obj.text(langText);
                    }
Kang Donghun committed
129 130 131 132
                    const attr = obj.attr('placeholder');
                    if (typeof attr !== 'undefined' && attr !== false) {
                        obj.attr('placeholder', langText);
                    }
133 134 135 136 137 138 139 140 141 142 143
                } else {
                    if (obj.attr('type') == 'image') {
                        obj.attr('alt', langText);
                        obj.attr('title', langText);
                    } else {
                        obj.text(langText);
                    }
                }
            }
        }
    }
144
};
145

Takumi Imai committed
146
/* Replace HTML text with the currently set language */
147 148 149 150 151 152 153 154 155
I18N.i18nReplaceText = function () {
    var storage = window.sessionStorage;
    if (storage) {
        var value = storage.getItem(I18N.avwsys_storagekey);
        if (value) {
            var json = JSON.parse(value);
            I18N.replaceText(json);
        }
    }
156
};
157

Takumi Imai committed
158 159 160 161 162
/**
 * Get string from key
 * @param {*} key
 * @returns
 */
163 164 165 166 167 168 169 170 171 172
I18N.i18nText = function (key) {
    var storage = window.sessionStorage;
    if (storage) {
        var value = storage.getItem(I18N.avwsys_storagekey);
        if (value) {
            var json = JSON.parse(value);
            return I18N.getLangText(json, key);
        }
    }
    return 'undefined';
173 174
};

Takumi Imai committed
175 176 177 178 179 180
/**
 * Obtain strings from key values of language data
 * @param {*} jsonLangData
 * @param {*} key
 * @returns
 */
181 182 183 184 185 186
I18N.getLangText = function (jsonLangData, key) {
    if (jsonLangData) {
        var text = jsonLangData[key];
        return text;
    }
    return 'undefined.';
187 188
};

Takumi Imai committed
189 190 191 192
/**
 * Switching Language Data
 * @param {*} lang
 */
193
I18N.changeLanguage = function (lang) {
Takumi Imai committed
194
    // Store the selected language only when switching languages
195 196 197 198 199
    var storage = window.localStorage;
    if (storage) {
        storage.setItem(I18N.avwsys_storagekey, lang);
    }

Takumi Imai committed
200
    // Reads language files and converts text strings
201
    I18N.loadLanguage(lang);
202 203
};

Takumi Imai committed
204 205 206 207 208
/**
 * Save language settings
 * @param {*} lang
 * @param {*} langData
 */
209 210 211 212 213 214 215 216
I18N.storeCurrentLanguage = function (lang, langData) {
    var ss = window.sessionStorage;
    if (ss) {
        // language data
        ss.setItem(I18N.avwsys_storagekey, JSON.stringify(langData));
        // current language
        ss.setItem(I18N.avwsys_currLang, lang);
    }
217
};
Takumi Imai committed
218
/* Retrieving the setting language */
219 220 221 222 223 224 225 226 227 228
I18N.getCurrentLanguage = function () {
    var lang;
    var storage = window.sessionStorage;
    if (storage) {
        lang = storage.getItem(I18N.avwsys_currLang);
    }
    if (!lang) {
        lang = I18N.getNavigatorLanguage();
    }
    return lang;
229
};