includes/clientside/static/l10n.js
changeset 210 2b283402e4e4
child 334 c72b545f1304
equal deleted inserted replaced
209:8a00247d1dee 210:2b283402e4e4
       
     1 /*
       
     2  * Enano client-side localization library
       
     3  */
       
     4 
       
     5 var Language = function(lang_id)
       
     6 {
       
     7   if ( typeof(enano_lang) != 'object' )
       
     8     return false;
       
     9   if ( typeof(enano_lang[lang_id]) != 'object' )
       
    10     return false;
       
    11   this.strings = enano_lang[lang_id];
       
    12   
       
    13   this.get = function(string_id, subst)
       
    14   {
       
    15     var catname = string_id.substr(0, string_id.indexOf('_'));
       
    16     var string_name = string_id.substr(string_id.indexOf('_') + 1);
       
    17     if ( typeof(this.strings[catname]) != 'object' )
       
    18       return string_id;
       
    19     if ( typeof(this.strings[catname][string_name]) != 'string' )
       
    20       return string_id;
       
    21     return '[LJS] ' + this.perform_subst(this.strings[catname][string_name], subst);
       
    22   }
       
    23   
       
    24   this.perform_subst = function(str, subst)
       
    25   {
       
    26     // var this_regex = /%this\.([a-z0-9_]+)%/;
       
    27     // var match;
       
    28     // while ( str.match(this_regex) )
       
    29     // {
       
    30     //   match = str.match(this_regex);
       
    31     //   alert(match);
       
    32     // }
       
    33     // hackish workaround for %config.*%
       
    34     str = str.replace(/%config\.([a-z0-9_]+)%/g, '%$1%');
       
    35     if ( typeof(subst) == 'object' )
       
    36     {
       
    37       for ( var i in subst )
       
    38       {
       
    39         if ( !i.match(/^([a-z0-9_]+)$/) )
       
    40           continue;
       
    41         var regex = new RegExp('%' + i + '%', 'g');
       
    42         str = str.replace(regex, subst[i]);
       
    43       }
       
    44     }
       
    45     return str;
       
    46   }
       
    47   
       
    48 }
       
    49 
       
    50 var $lang;
       
    51 
       
    52 var language_onload = function()
       
    53 {
       
    54   $lang = new Language(ENANO_LANG_ID);
       
    55   // for debugging :-)
       
    56   // alert( $lang.get('user_err_invalid_credentials_lockout_captcha', { lockout_fails: '3', lockout_threshold: '5', lockout_duration: '15' }) );
       
    57 }
       
    58 
       
    59 addOnloadHook(language_onload);
       
    60