Version 2
What is new? Migration Guide From V1
Getting Started
Understanding WinnetouJs Setting Up
Constructos
Creating Constructos Advanced Options Lazy Loading
Mutables
Constructos State Management Case: Custom State Management System Winnetou FX
Addons
Winnetou Selectors Winnetou Router App Translations CSS Sass Built-in Transpiler SVG Icons Color Themes
Extras
Useful JsDoc Syntax
Plugins
Creating Plugins SlideScreen Official Plugin Animate Official Plugin
Compiling to Production
WBR
API Change Log Git Hub Pull Requests Guidelines Get in touch
Addons

Translations

One of WinnetouJs' most powerful tools is translate. With this method it is possible to have all the phrases, titles, texts and labels in an json file separate from the code and with just one method we can change which language file the application will feed on. For that we have two methods, the updateTranslations and the changeLang.

To begin you must define the defaultLang in win.config.json. The defaultLang name is the name of the default json file for your application's text. Remember that the WinnetouJs translations only work in applications that have a backend.

All translations files must be inside folder ./translations at Public folder of project.

Settings

Set the default language file in win.config.json: defaultLang: "en-us",. In folder ./translations you need to have a file called en-us.json.

Creating a json translation file

Inside en-us.json, create keys and values that will be your keys.

en-us.json

        
{
    "welcome": "Welcome to new Winnetou Translations.",
    "title": "This is your title"
}
       
      

Create a new file for each translation.

es.json

        
{
    "welcome": "Bienvenido a las nuevas Traducciones de WinnetouJs.",
    "title": "Este es tu titulo"
}
       
      
chinese.json

          
{
    "welcome": "欢迎来到新的 Winnetou 翻译。",
    "title": "这是你的标题"
}
         
        

When you run node wbr --bundleRelease, the translations will be transpiled to an js file _strings.js which allow you to import strings in your native code.

How to use

You need to import strings to your code, call updateTranslations and then start your app. See code below.

appTranslations.js

                
import { Winnetou } from "winnetoujs";
import _strings from "./_strings.js";
import {simpleDiv} from './constructos/components.html'

Winnetou.updateTranslations(_strings).then(() => startMyApp());

async function startMyApp(){
    simpleDiv({
        text: _strings.title
    }).create('#app')
}
               
              

Switching languages

In order to switch languages, call changeLang method.

appTranslations.js

                  
...

const changeToChinese = () =>{
    Winnetou.changeLang('chinese')
}
                 
                

The param of changeLang needs to be the name of translation file without .json.