how to re-use a language file in multiple languages without doubling of files with Titanium

438 views Asked by At

So I'm using a language file in Titanium to serve TSS properties I want to re-use throughout the entire app at different locations. These language file variables should be used in the themes folder (or any other TSS file for that matter).

Currently it works with a single language, but my app has multiple languages. But I don't want to duplicate the language file for all languages. Can I re-use the same file in multiple languages without having to copy the file somewhere?

3

There are 3 answers

0
Rene Pot On BEST ANSWER

It appears it is not possible to reuse a language file without copying it to all languages. However, the best solution to create a global go-to for parameters to be used in TSS files is to add a section to the config.json file.

A proper way to do this is:

"global": {
    "design": {
        "primaryColor": "red"
    }
},

This can then be used by accessing Alloy.CFG.design.primaryColor.

The benefit for using the config.json file is that you can also theme the files, as described by Fokke Zandbergen.

This way, it is even better than using language files, because those couldn't be themed.

3
Alco On

No, but you could use default strings like:

L('my_string','my default for this string');

In this example 'my_string' is a string withing your language file. If you only provide a file for English, you'll get the default setting for all other languages.

R

4
Alejandro F. Carrera On

Use i18n files at ISO 639-1 representation.

That files allow you have any languages and use each "labels" with Ti.Locale.getString().

Also, you can use a require of file at app.js and put this variable like global.

language.js (for example):

var language = (function() {

    var self = {
        currentLanguage: 'en' // by default
    };

    var labels = {
        msgHello: {
            en: 'Hello World',
            es: 'Hola Mundo'
        }
    };

    self.changeLanguage = function changeLanguage(newLanguage){
        self.currentLanguage = newLanguage;
    };

    self.getLabel = function getLabel(key, language){
         if(typeof language !== 'undefined') {
             return labels[key][language];
         }
         else return labels[key][self.currentLanguage];
    };

    return self;

}());

module.exports = language;

app.js (for example):

var appLanguage = require('language.js');

(function() {

    Ti.API.info("Language: "+appLanguage.currentLanguage);
    Ti.API.info("MSG Hello World (English): "+appLanguage.getLabel(msgHello));
    Ti.API.info("MSG Hello World (Spanish): "+appLanguage.getLabel(msgHello, es));

}());

You can use appLanguage variable directly on any file.