includes/clientside/static/l10n.js
changeset 1227 bdac73ed481e
parent 1196 704ba4b9819b
equal deleted inserted replaced
1226:de56132c008d 1227:bdac73ed481e
     2  * Enano client-side localization library
     2  * Enano client-side localization library
     3  */
     3  */
     4 
     4 
     5 var Language = function(lang_id)
     5 var Language = function(lang_id)
     6 {
     6 {
     7   var have_lang = false;
     7 	var have_lang = false;
     8   
     8 	
     9   if ( typeof(enano_lang) == 'object' )
     9 	if ( typeof(enano_lang) == 'object' )
    10   {
    10 	{
    11     if ( typeof(enano_lang[lang_id]) == 'object' )
    11 		if ( typeof(enano_lang[lang_id]) == 'object' )
    12     {
    12 		{
    13       have_lang = true;
    13 			have_lang = true;
    14     }
    14 		}
    15   }
    15 	}
    16   if ( !have_lang )
    16 	if ( !have_lang )
    17   {
    17 	{
    18     // load the language file
    18 		// load the language file
    19     load_show_win('strings');
    19 		load_show_win('strings');
    20     console.info('Loading language ' + lang_id + ' via AJAX synchronous request');
    20 		console.info('Loading language ' + lang_id + ' via AJAX synchronous request');
    21     var ajax = ajaxMakeXHR();
    21 		var ajax = ajaxMakeXHR();
    22     var uri = makeUrlNS('Special', 'LangExportJSON/' + lang_id, enano_version);
    22 		var uri = makeUrlNS('Special', 'LangExportJSON/' + lang_id, enano_version);
    23     ajax.open('GET', uri, false);
    23 		ajax.open('GET', uri, false);
    24     ajax.send(null);
    24 		ajax.send(null);
    25     if ( ajax.readyState == 4 && ajax.status == 200 )
    25 		if ( ajax.readyState == 4 && ajax.status == 200 )
    26     {
    26 		{
    27       eval_global(ajax.responseText);
    27 			eval_global(ajax.responseText);
    28     }
    28 		}
    29     load_hide_win();
    29 		load_hide_win();
    30   }
    30 	}
    31   
    31 	
    32   if ( typeof(enano_lang) != 'object' )
    32 	if ( typeof(enano_lang) != 'object' )
    33     return false;
    33 		return false;
    34   if ( typeof(enano_lang[lang_id]) != 'object' )
    34 	if ( typeof(enano_lang[lang_id]) != 'object' )
    35     return false;
    35 		return false;
    36   this.strings = enano_lang[lang_id];
    36 	this.strings = enano_lang[lang_id];
    37   this.lang_id = lang_id;
    37 	this.lang_id = lang_id;
    38   this.placeholder = false;
    38 	this.placeholder = false;
    39   
    39 	
    40   this.get = function(string_id, subst)
    40 	this.get = function(string_id, subst)
    41   {
    41 	{
    42     var catname = string_id.substr(0, string_id.indexOf('_'));
    42 		var catname = string_id.substr(0, string_id.indexOf('_'));
    43     var string_name = string_id.substr(string_id.indexOf('_') + 1);
    43 		var string_name = string_id.substr(string_id.indexOf('_') + 1);
    44     if ( typeof(this.strings[catname]) != 'object' )
    44 		if ( typeof(this.strings[catname]) != 'object' )
    45       return string_id;
    45 			return string_id;
    46     if ( typeof(this.strings[catname][string_name]) != 'string' )
    46 		if ( typeof(this.strings[catname][string_name]) != 'string' )
    47       return string_id;
    47 			return string_id;
    48     return this.perform_subst(this.strings[catname][string_name], subst);
    48 		return this.perform_subst(this.strings[catname][string_name], subst);
    49   }
    49 	}
    50   
    50 	
    51   this.perform_subst = function(str, subst)
    51 	this.perform_subst = function(str, subst)
    52   {
    52 	{
    53     var this_regex = /%this\.([a-z0-9_]+)%/;
    53 		var this_regex = /%this\.([a-z0-9_]+)%/;
    54     var match;
    54 		var match;
    55     while ( str.match(this_regex) )
    55 		while ( str.match(this_regex) )
    56     {
    56 		{
    57       match = str.match(this_regex);
    57 			match = str.match(this_regex);
    58       str = str.replace(match[0], this.get(match[1]));
    58 			str = str.replace(match[0], this.get(match[1]));
    59     }
    59 		}
    60     // hackish workaround for %config.*%
    60 		// hackish workaround for %config.*%
    61     str = str.replace(/%config\.([a-z0-9_]+)%/g, '%$1%');
    61 		str = str.replace(/%config\.([a-z0-9_]+)%/g, '%$1%');
    62     if ( typeof(subst) == 'object' )
    62 		if ( typeof(subst) == 'object' )
    63     {
    63 		{
    64       for ( var i in subst )
    64 			for ( var i in subst )
    65       {
    65 			{
    66         if ( !i.match(/^([a-z0-9_]+)$/) )
    66 				if ( !i.match(/^([a-z0-9_]+)$/) )
    67           continue;
    67 					continue;
    68         var regex = new RegExp('%' + i + '%', 'g');
    68 				var regex = new RegExp('%' + i + '%', 'g');
    69         str = str.replace(regex, subst[i]);
    69 				str = str.replace(regex, subst[i]);
    70       }
    70 			}
    71     }
    71 		}
    72     return str;
    72 		return str;
    73   }
    73 	}
    74   
    74 	
    75 }
    75 }
    76 
    76 
    77 var language_onload = function()
    77 var language_onload = function()
    78 {
    78 {
    79   $lang = new Language(ENANO_LANG_ID);
    79 	$lang = new Language(ENANO_LANG_ID);
    80 }
    80 }
    81 
    81 
    82 var $lang = {
    82 var $lang = {
    83   get: function(t, s) {
    83 	get: function(t, s) {
    84     language_onload();
    84 		language_onload();
    85     return $lang.get(t, s);
    85 		return $lang.get(t, s);
    86   },
    86 	},
    87   placeholder: true
    87 	placeholder: true
    88 };
    88 };