Lang

The lang service allows you to get language variables and control the translation of modules.

Usage #

The lang setting on the initialization of Kube, allows to choose the application and modules language.

$K.init({
    lang: 'en'
});

Set and get the language variables in your module.

(function($K)
{
    $K.add('module', 'mymodule', {
        translations: {
            en: { 'title': 'Title' },
            de: { 'title': 'Titel' }
        },
        init: function(app, context)
        {
            this.app = app;

            // define service
            this.lang = app.lang;
        },
        getLangVariable: function()
        {
            var value = this.lang.get('title');
        }
    });
})(Kube);

Add Language #

The standard way is to add a translations object to your modules like in the example above. But you can create the global language object with $K.addLang method. So all modules can use the language variables from this object.

$K.addLang('en', {
    "title": "Title"
});

$K.addLang('de', {
    "title": "Titel"
});

Methods #

get

Arguments
name String the name of language variable

Returns: String Boolean

Get the language variable. Returns false if the variable does not exists.

(function($K)
{
    $K.add('module', 'mymodule', {
        translations: {
            en: { 'title': 'Title' },
            de: { 'title': 'Titel' }
        },
        init: function(app, context)
        {
            this.app = app;

            // define service
            this.lang = app.lang;
        },
        getLangVariable: function()
        {
            var value = this.lang.get('title');
        }
    });
})(Kube);

parse

Arguments
str String language template

Returns: String

Replace templates like ## title ## in the string with the values of the language variable.

(function($K)
{
    $K.add('module', 'mymodule', {
        translations: {
            en: { 'title': 'Title' },
            de: { 'title': 'Titel' }
        },
        init: function(app, context)
        {
            this.app = app;

            // define service
            this.lang = app.lang;
        },
        parseLangTemplate: function()
        {
            var str = this.lang.parse('<span class="title">## title ##</span>');
        }
    });
})(Kube);

rebuild

Arguments
lang String two-letter index of language

Change the language on the fly.

(function($K)
{
    $K.add('module', 'mymodule', {
        translations: {
            en: { 'title': 'Title' },
            de: { 'title': 'Titel' }
        },
        init: function(app, context)
        {
            this.app = app;

            // define service
            this.lang = app.lang;
        },
        changeLang: function()
        {
            this.lang.rebuild('de');
        }
    });
})(Kube);