4

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?

Rene Pot
  • 24,681
  • 7
  • 68
  • 92

3 Answers3

0

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.

0

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.

Rene Pot
  • 24,681
  • 7
  • 68
  • 92
-1

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

Rene Pot
  • 24,681
  • 7
  • 68
  • 92
Alco
  • 149
  • 5
  • Thanks, but that defeats the whole purpose of the language files, If I have to define the default everywhere what I specifically want dynamic – Rene Pot Aug 25 '14 at 20:54
  • The right way of doing it is by creating files in the language folder you wish to support. If you have a language file for that language, then Titanium will use it. If you don't support the language selected by your user, the it'll display the default text. – Alco Aug 26 '14 at 21:28
  • If you properly read my question, you'd see my question isn't about language strings,but about properties for TSS file. The 'default' should be the one coming from the file. Providing a default in the `L()` function defeats the purpose of the functionality I'm trying to make. Therefore it is easier to copy files than using defaults. And my question is how to avoid having to copy files, and have a fallback file instead – Rene Pot Aug 27 '14 at 12:24