# all files / red/runtime/ i18n.js

61.82% Statements 34/55
27.27% Branches 6/22
69.23% Functions 9/13
61.82% Lines 34/55

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131

    1×
    1×
    1×
    1×

    1×

    1×
    1×

    1×

    1×
    5×
    5×
    5×
    5×

    1×

    1×

    10×
    10×

    10×
    10×
    5×

    5×
    5×
    5×

    5×

    5×

    1×
    5×
    5×
    5×

    5×

    1×

    1×

    1×

    109×

     /**
     * Copyright JS Foundation and other contributors, http://js.foundation
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     **/

    var i18n = require("i18next");
    var when = require("when");
    var path = require("path");
    var fs = require("fs");

    var defaultLang = "en-US";

    var resourceMap = {};
    var resourceCache = {};

    function registerMessageCatalogs(catalogs) {
        var promises = catalogs.map(function(catalog) {
            return registerMessageCatalog(catalog.namespace,catalog.dir,catalog.file);
        });
        return when.settle(promises);
    }

    function registerMessageCatalog(namespace,dir,file) {
        return when.promise(function(resolve,reject) {
            resourceMap[namespace] = { basedir:dir, file:file};
            i18n.loadNamespace(namespace,function() {
                resolve();
            });
        });
    }

    function mergeCatalog(fallback,catalog) {
        for (var k in fallback) {
            if (fallback.hasOwnProperty(k)) {
                if (!catalog[k]) {
                    catalog[k] = fallback[k];
                } else if (typeof fallback[k] === 'object') {
                    mergeCatalog(fallback[k],catalog[k]);
                }
            }
        }
    }

    var MessageFileLoader = {
        fetchOne: function(lng, ns, callback) {
            Eif (resourceMap[ns]) {
                var file = path.join(resourceMap[ns].basedir,lng,resourceMap[ns].file);
                //console.log(file);
                fs.readFile(file,"utf8",function(err,content) {
                    if (err) {
                        callback(err);
                    } else {
                        try {
                            resourceCache[ns] = resourceCache[ns]||{};
                            resourceCache[ns][lng] = JSON.parse(content.replace(/^\uFEFF/, ''));
                            //console.log(resourceCache[ns][lng]);
                            Iif (lng !== defaultLang) {
                                mergeCatalog(resourceCache[ns][defaultLang],resourceCache[ns][lng]);
                            }
                            callback(null, resourceCache[ns][lng]);
                        } catch(e) {
                            callback(e);
                        }
                    }
                });
            } else {
                callback(new Error("Unrecognised namespace"));
            }
        }

    }

    function init() {
        return when.promise(function(resolve,reject) {
            i18n.backend(MessageFileLoader);
            i18n.init({
                ns: {
                    namespaces: [],
                    defaultNs: "runtime"
                },
                fallbackLng: [defaultLang]
            },function() {
                resolve();
            });
        });
    }

    function getCatalog(namespace,lang) {
        var result = null;
        lang = lang || defaultLang;
        if (resourceCache.hasOwnProperty(namespace)) {
            result = resourceCache[namespace][lang];
            if (!result) {
                var langParts = lang.split("-");
                if (langParts.length == 2) {
                    result = resourceCache[namespace][langParts[0]];
                }
            }
        }
        return result;
    }

    var obj = module.exports = {
        init: init,
        registerMessageCatalog: registerMessageCatalog,
        registerMessageCatalogs: registerMessageCatalogs,
        catalog: getCatalog,
        i: i18n,
        defaultLang: defaultLang
    }

    obj['_'] = function() {
        //var opts = {};
        //if (def) {
        //    opts.defaultValue = def;
        //}
        //console.log(arguments);
        return i18n.t.apply(null,arguments);
    }