‚̧Niche Portobello Mushroom
    Wondering what‚Äôs next for npm?Check out our public roadmap! ¬Ľ

    json-translation

    1.1.0¬†‚Äʬ†Public¬†‚Äʬ†Published

    json-translation

    npm GitHub issues GitHub license GitHub stars

    Tool to make translations "offline" from json documents, dont having to wait for an API call or async function. Can be used with NodeJS without problem.

    Instalation

    To install it just run: npm i json-translation -s

    How to use it

    First, you need to create a new Translation object.

    const Translator = require("json-translation")
    const ts = new Translator()

    Then, specify where the json translation files are, like this: ts.translationPath = "./data/" or like this: ts.setPath("./data")

    After declaring the path, you load them to memory with ts.init(), so all the calls are as fast as they can.

    Then, you can ask for the translation text like this: ts.translate(<lang>, <string>[, <fallbackString>]) You need to have the json files with the desired names where you specify the translationPath.

    The json structure can be like this:

    {
        "header_section": {
            "title": "My awesome page",
            "subtitle": "Hello world"
        },
        "footer_section": {
            "example": "you can put more text right here!"
        },
        "my_email": "personal@email.com"
    }

    Considering that the file name is en.json you can access it information like this:

    ts.translate("en", "header_section.title")

    and will output My awesome page. You can change the dot separator to any other one with: ts.separator = "/" and access the information like this: ts.translate("en", "header_section/title")

    Full example

    index.js

        var Translator = require("json-translation")
        let ts = new Translator()
     
        ts.setPath("./data")
        ts.init()
        ts.separator = ";"
        console.log(ts.translate("es", "example_section;translation_2"))
        console.log(ts.translate("en", "example_section;translation_2"))

    data/es.json

    {
        "example_section": {
            "translation_1": "Texto de prueba",
            "translation_2": "Esté es un texto de prueba para mostrar la funcionalidad del paquete."
        }
    }

    data/en.json

    {
        "example_section": {
            "translation_1": "Testing text",
            "translation_2": "This is a testing text to show the functionality of the package."
        }
    }

    Install

    npm i json-translation

    DownloadsWeekly Downloads

    1

    Version

    1.1.0

    License

    MIT

    Unpacked Size

    7.81 kB

    Total Files

    9

    Last publish

    Collaborators

    • avatar