Skip to content

The localization keys generator from JSONC file. It generates a typescript class with static fields that have values is path to JSON value.

License

Notifications You must be signed in to change notification settings

ripenko/localizationkeys-webpack

Repository files navigation

localizationkeys-webpack

The localization keys generator from JSONC file. It generates a typescript class with static fields that have values is path to JSON value. The main advantage is that when your jsonc has been changed then typescipt class will be changed too. So. If you remove localization from default language json file then you have an compile error.

Installation

npm install --save localizationkeys-webpack

Setup

className. Optional

Default is LocalizationKeys. The name of class

output. Optional

Default is ./LocalizationKeys.ts. The path of generated typescript file

Usage

Simple usage

Your localization default.jsonc is in localization folder.

webpack.config.js file:

...

module.exports = () => {
    return [{
        ...
        module: {
            ...
            rules: [
                ...
                {
                    test: /\.jsonc$/,
                    include: /localization/,
                    use: [
                        "json-loader"
                    ],
                    type: "javascript/auto"
                },
                {
                    test: /default\.jsonc$/,
                    type: "javascript/auto",
                    use: [{
                        loader: "localizationkeys-webpack?className=LocalizationKeys&output=./LocalizationKeys.ts"
                    }]
                }
                ...
            ]
            ...
        }
        ...
    }];
    ...
}

json-loader is used to load exact localization values as an object. Somewhere in your app:

const defaultLanguage = required("localization/default.jsonc");

default.jsonc file:

{
    "Hello": "{0}! Hello World!", // {0} - user full name
    "Boolean": {
        "true": "Ja",
        "nein": "Nein"
    }
}

JSONC supports comments. So you can leave some comment if you localization value has replacement token(s) to share knowledge with someone who will make translation.

The generated LocalizationKeys will be:

export default class LocalizationKeys
{
    Hello= "Hello";
    public static Boolean = 
    {
        AllKeys: "Boolean",
        true: "Boolean.true",
        nein: "Boolean.nein",
    }
}

Credits

Alexey Ripenko, GitHub

License

MIT

About

The localization keys generator from JSONC file. It generates a typescript class with static fields that have values is path to JSON value.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published