Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-13966

TiAPI: Select Locale for L() at runtime

    Details

    • Story Points:
      8
    • Sprint:
      2018 Sprint 08 SDK, 2018 Sprint 09 SDK, 2018 Sprint 10 SDK, 2018 Sprint 11 SDK, 2018 Sprint 12 SDK, 2018 Sprint 13 SDK, 2018 Sprint 14 SDK, 2018 Sprint 15 SDK, 2018 Sprint 16 SDK

      Description

      Problem
      It would be great if it was possible to let the user select which language to use at runtime. So instead the L() would only look at device language, it could also check if a Ti.App.LanguageXMLSetting property has been defined. That way we do not need to create homemade localization systems anymore.

      The use case is included below. The problem is that L is undefined in style.tss because I redefine it in the controller.

      alloy.js

      // Language helper
      Alloy.LanguageHelper = require('languageHelper');
      Alloy.L = Alloy.LanguageHelper.fetchString;
      

      Controller

      //top of the controller
      var L = require("languageHelper");
      

      Style

       
      "#menuBtn":{
      	title: L("MenuButton")
      }
      

      View

      <Alloy>
      	<Window>
      		<Button id="menuBtn"></Button>
      	</Window>
      </Alloy>
      

      Custom languageHelper

      /**
       * Language Helper
       * Makes it possible to change language at runtime 
       * @version 1.0
       */
       
      var lookup = null;
      exports.locale = Ti.App.Properties.getString('SETTING_LANGUAGE', Ti.Locale.getCurrentLanguage() );
       
       
      /**
       * Private function
       * Loads the language xml file.
       * It has fallback for english if file does not exist.
       */
      function loadFile() {
      	lookup = {};
      	
      	// LOAD FILE
      	var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'i18n/' + exports.locale + '/strings.xml');
      	if (!file.exists()) {
      		file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'i18n/en/strings.xml');
      		if (!file.exists()) {
      			Ti.API.warn("Language file for both '" + exports.locale + "' and 'en' fallback do not exist");
      			return;	
      		}
      	}
      	
      	// PARSE XML
      	var xml_string = file.read().text;
        	var doc = Ti.XML.parseString(xml_string);
      	var strings = doc.getElementsByTagName("string");
      	for (var i = 0; i < strings.length; i++) {
      		var node = strings.item(i);
      		var value = node.text;
      		if (node.hasAttributes()) {
      			for (var att_index = 0; att_index < node.attributes.length; att_index++) {
      				var att = node.attributes.item(att_index);
      				if (att.nodeName === "name") {
      					lookup[att.nodeValue] = value;
      				}
      			}
      		}
      	}
      	
      }
       
      /**
       * Set the current language of the app. 
       * @param {String} language
       */
      exports.setLanguage = function(language){
      	if(language === null){
      		return;
      	}
      	// clean old lookup
      	exports.clear();
      	
      	// save language
      	Ti.App.Properties.setString('SETTING_LANGUAGE', language);
      	exports.locale = language;
      	
      	
      	// parse new lookup
      	loadFile();
      	
      	
      	return lookup;
      }
       
      /**
       * Clear the parsed xml translations
       */
      exports.clear = function() {
      	lookup = null;
      };
       
      /**
       * Lookup function. require this at each controller
       * Example usage:
       * var L = require('languageHelper').fetchString;
       * @param {Object} string
       * @param {Object} hint
       */
      exports.fetchString = function(string, hint) {
      	if (lookup === null) {
      		loadFile();
      	}
      	return lookup[string] !== undefined ? lookup[string].replace(/\\n/g, "\n") : (hint || string);
      }; 
      

        Attachments

          Activity

            People

            • Assignee:
              jquick Joshua Quick
              Reporter:
              viezel Mads Moller
              Reviewer:
              Gary Mathews
              Tester:
              Josh Longton
            • Watchers:
              26 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Backbone Issue Sync

                • Backbone Issue Sync is enabled for your project, but we do not have any synchronization info for this issue.

                  Git Source Code