Pentru a putea vota, modifica pagini sau abona la modificările unei pagini trebuie să fiți autentificați. Cu această ocazie scăpați și de acest mesaj deranjant.

Localizarea în PHP

De la l10n.ro
Salt la: navigare, căutare


Localizarea in PHP
// This function return the preffered language based on browser settings and user choise saved in a cookie
function loc($iso = FALSE) {
  $accepted = Array('ro', 'en');
  $ISO = Array('ro' => 'ro_RO', 'en' => 'en_US');
  if (in_array($_COOKIE['language'], $accepted)) $x = $_COOKIE['language'];
  else {
    $temp = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
    foreach($temp as $val) if (in_array(substr($val, 0, 2), $accepted)) $x = substr($val, 0, 2);  
  }
  if (in_array($x, $accepted)) $x = $accepted[0];
  if ($iso) return $ISO[$x];
  else return $x;
}



/**
* Function: __x (String, Hash)
* Returns: String
*
* A kind of sprintf function using locale that allows the translator to change the order of parameters
* 
* Example :
* 
* $array = Array(
*   'cols' => 10,
*   'rows' => 12,
* );
* echo __x('In this table we have [_COLS] columns and [_ROWS] rows', $array); //-> In this tab;e we have 10 columns and 12 rows
* 
*/     

function __x($str, $arr) {
  return preg_replace("/\[\_(.+?)\]/e","\$arr{strtolower(\"\$1\")}", _($str));
}




/**
* Function: __nx (String, String, Int)
* Returns: String
*
* Plural locale function. It receives 2 strings and returns the one appropriate for the value of integer
* 
* Example :
* 
* echo __nx("I have one apple", "I have [X] apples", 1); //-> I have one apple
* echo __nx("I have one apple", "I have [X] apples", 6); //-> I have 6 apples
* 
*/                                                                          

function __nx($str1, $str2, $nr) {
  if ($nr == 1) return str_replace('[X]', $nr, _($str1));
  else return str_replace('[X]', $nr, _($str2));
}




setlocale(LC_ALL, loc(TRUE));      // setlocale requires ISO format (en_US, fr_FR) - it doesn't work with simple codes

bindtextdomain('orgdomain', 'locale');
textdomain('orgdomain');
bind_textdomain_codeset("orgdomain","UTF-8");  // don't forget it !!!

# localized script

string _( string msgid1);              // alias for gettext
string gettext( string msgid1); 
string ngettext ( string msgid1, string msgid2, int n)