Module parse_bif_locale

Source
Expand description

§{:locale; … :}

Includes a language json file:

{:locale; filename :}

{:locale; file.json :}

The language files must have a structure similar to this one:

{
    "trans": {
        "es": {
            "Hello": "Hola",
            "ref:greeting": "Hola",
        },
        "es-ES": {
            "Hello": "Hola",
            "ref:greeting": "Hola",
        }
    }
}

A “trans” key and then the key for each language, any other key will produce an error or be ignored.

§Modifiers:

{:!locale; ... :}
{:^locale; ... :}

§Modifier: ! (not)

The “not” modifier prevents the file from being reloaded if it has already been parsed. Generally, language files should only be included once; it will increase performance if “not” is used:

{:!locale; file.json :}

§Modifier: ^ (upline)

Eliminates previous whitespaces.

§Flags

{:locale; {:flg; require noparse :} >> ... :}

§Flag: require

By default, no error will occur if the locale file does not exist, unless the “require” flag is set.

{:locale; {:flg; require :} >> file.json :}

§Flag: noparse

The “noparse” flag prevents the file to be included from being parsed.

{:locale; {:flg; noparse :} >> file.json :}

§Return value

“locale” shows no output but returns “{:;:}” (unprintable) if successful, so this is possible:

{:locale; file.{:lang;:}.json :}
{:else; file.en.json :}

§Dynamic evaluation

The following will produce an error:

{:locale; {:;varname:} :}

For safety reasons, when evaluating the complete variable it is necessary to use “allow”:

{:locale; {:allow; allowed-words-list >> {:;varname:} :} :}
{:locale; {:!allow; traversal >> {:;varname:} :} :}

In any case, you must use “allow” on any variable that comes from the context. See the “allow” and “declare” bifs for more details.

§Translation evaluation

Translations may contain variables:

{
    "trans": {
        "es": {
            "Welcome to {:;web-site-name:}": "Bienvenido a {:;web-site-name:}"
        }
    }
}

Variables are resolved at the time the file is included.

(*) Translations included in the “schema” are not evaluated, only those loaded with “locale”.

§Relative to current file path

We can use relative paths to the currently localed file with the “#” symbol:

{:locale; #/file.json :}

It will be relative to the file loaded with “include”:

{:*
    file: /path/to/tpl/snippets.ntpl
    Here #/file.json is: /path/to/tpl/file.json
*:}

{:locale; #/file.json :}