非常教程

Webpack参考手册

装载 | Loaders

i18n-loader

用法

./colors.json

{
    "red": "red",
    "green": "green",
    "blue": "blue"
}

./de-de.colors.json

{
    "red": "rot",
    "green": "gr�n"
}

call it

// 假如我们的所在区域是“de-de-berlin”
var locale = require("i18n!./colors.json");
// 等待准备就绪,在一个web项目中所有地区只需要一次
// 因为所有区域的语言被合并到一个chunk里
locale(function() {
    console.log(locale.red); // prints rot
    console.log(locale.blue); // prints blue
});

选项

如果想要一次加载然后可以同步地使用, 你应该告诉 loader 所有要使用的地区。

{
    "i18n": {
        "locales": [
            "de",
            "de-de",
            "fr"
        ],
        // "bundleTogether": false
        // this can disable the bundling of locales
    }
}

可选的调用方法

require("i18n/choose!./file.js"); // 根据地区选择正确的文件
                    // 但是不会合并到对象中

require("i18n/concat!./file.js"); // 拼接所有合适的地区
require("i18n/merge!./file.js");  // 合并到对象中
                    // ./file.js 在编译时会被排除掉
require("i18n!./file.json") == require("i18n/merge!json!./file.json")

如果需要在 node 中使用,不要忘记填补(polyfill)require。 可以参考 webpack 文档。