PHP Classes

Intl like gettext: Translate texts from arrays, PO files or JSON

Recommend this page to a friend!
  Info   View files Example   Demos   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 69%Total: 170 All time: 8,834 This week: 455Up
Version License PHP version Categories
intl 1.5.4The PHP License5Localization, PHP 5, Templates
Description 

Author

This class can translate application texts from arrays, PO gettext or JSON.

It can auto detect the user preferred languages and load translation files in JSON format, PHP scripts with arrays of strings, or using gettext extension to load PO translation files.

It can also format strings with placeholders %named_value to translated string 'named_value'.

The class can also plurals from gettext in po file or array.

Picture of Lukasz Józwiak
  Performance   Level  
Name: Lukasz Józwiak <contact>
Classes: 4 packages by
Country: Poland Poland
Age: 45
All time rank: 265362 in Poland Poland
Week rank: 354 Up8 in Poland Poland Up

Example

<?php
if(!defined('DS')) {
define('DS',DIRECTORY_SEPARATOR);

require_once
dirname(__FILE__).DS.'Intl.php';
}

        echo
"<h3>Simple</h3>\n";

$strings = array(
   
'text' => 'tekst',
   
'write' => 'zapisz',
   
'read' => 'czytaj',
   
'forget it' => 'zapomnij',
   
'put' => 'w?ó?',
   
'up' => 'góra',
   
'down' => 'dó?',
   
'true' => 'prawda',
   
'text to you'=>'tekst dla ciebie',
 );

       
Intl::set_strings($strings); // can use with Intl::set_strings($strings,'string');

       
echo '<p>'.Intl::_('text to you')."</p>\n"; // will return 'text dla ciebie'

       
echo '<p>'.Intl::_('you')."</p>\n"; // will return 'you' because not exist or not translated
       
       
Intl::set_strings($strings,'string'); // read up

       
echo '<p>'.Intl::_('text to you','string')."</p>\n"; // will return 'text dla ciebie'

       
echo '<p>'.Intl::_('you','string')."</p>\n"; // will return 'you' because not exist or not translated

       
echo '<p>'.Intl::get_browser_lang()."</p>\n"; // without arguments return first supported browser language

       
$array = array('en','ru','ar','en-us');

        echo
'<p>'.Intl::get_browser_lang($array)."</p>\n"; // with arguments return first supported browser language existing in array

       
Intl::$strings = NULL; // for next examples

        // Simple translate

       
echo "<h3>Simple with loaders</h3>\n";

       
Intl::set_path(dirname(__FILE__).DS.'languages'); // set search path with files
       
$langs = Intl::available_locales(Intl::PHP); // return available files from dir, Intl::PHP[JSON,PO] const mode set
       
Intl::set_default_lang('pl'); // set default lang
       
var_dump(Intl::get_browser_lang($langs)); // return available lang default(if set else en if exists), first code found in $langs if exists (en > en-US/GB... > en_GB...)
       
      
echo "<pre>\n";
       
Intl::load_locale_simple(Intl::get_browser_lang($langs)); // Load simple translate file can be full path, set domain for store strings optional
       
echo "</pre>\n";
        echo
"<p>".Intl::_('text to you')."</p>\n"; // return translated string not work with plurals here
      
echo "<p>".Intl::_('up')."</p>\n";
       echo
"<p>".Intl::_('read')."</p>\n";
      
// var_dump(Intl::$strings);
       // Plurals PO

       
echo "<h3>Multiple auto PO</h3>\n";

       
$langs = Intl::available_locales(Intl::PO);
       
$lang = Intl::get_browser_lang($langs);
       
//Intl::po_locale_plural(fullpath,'plurals');
       
Intl::po_locale_plural($lang,'plurals'); // Load translate file can be full path this function have a plural support, set domain for store strings optional

       
$searchone = "choice: "; // one
       
$searchplural = "choices: "; // many

       
$a = 0;
        while (
$a <= 40) {
          echo
"<p>".$a." ".Intl::_n($searchone,$searchplural,$a,'plurals')."</p>\n"; // return transladed string with given number of choises
         
$a += 3;
        }

       
$n = 30030; // number of choises

       
echo "<p>".$n." ".Intl::_n($searchone,$searchplural,$n,'plurals')."</p>\n"; // return transladed string with given number of choises

       
echo "<h3>Multiple custom PO</h3>\n";
      
       
$plural = 1; // for manual logic , many langs is added like cz,sk,pl,csb,ar,en,gb,fr... but not all
       
$nplurals = 3; // like up
       
echo "<p> _n ".$n." ".Intl::_n($searchone,$searchplural,$n,'plurals',array($nplurals,$plural))."</p>\n"; // in this way you can use you own logic
       
echo "<p>".$n." ".Intl::_p($searchplural,'plurals',$plural)."</p>\n"; // return transladed string with manually given plural number
       
echo "<p>".$n." ".Intl::_p($searchplural,'plurals')."</p>\n"; // return transladed string but always from 0 plural entry
       
      
echo "<p> 1. ".Intl::_p($searchone,'plurals')."</p>\n"; // // return transladed string, 1 is optional [Intl::_p($searchplural,'plurals',2) return [2] choises ]
       
       
echo "<p>".Intl::_('text to you','plurals')."</p>\n"; // yeap now work
       
echo "<p>".$n." ".Intl::_($searchplural,'plurals',2)."</p>\n"; // yeap now work with plurals too but strings with them must be loaded
        // Plural with not po file? yes look in dump, you need that scheme in php or json file
        //Intl::tojson(dirname(__FILE__).DS.'languages'.DS.'pl.json',Intl::$strings['plurals']);
        //var_dump(Intl::$strings['plurals']);

       
echo "<h3>Multiple auto JSON</h3>\n";

       
$langs = Intl::available_locales(Intl::JSON);
       
$lang = Intl::get_browser_lang($langs);
       
//Intl::po_locale_plural(fullpath,'plurals');
       
Intl::json_locale($lang,'json'); // Load translate file can be full path this function have a plural support, set domain for store strings optional

       
$searchone = "choice: "; // one
       
$searchplural = "choices: "; // many

       
$a = 1;
        while (
$a <= 40) {
          echo
"<p>".$a." ".Intl::_n($searchone,$searchplural,$a,'json')."</p>\n"; // return transladed string with given number of choises
         
$a += 3;
        }

       
$n = 30030; // number of choises

       
echo "<p>".$n." ".Intl::_n($searchone,$searchplural,$n,'json')."</p>\n"; // return transladed string with given number of choises

?>


  Intl on php 5.2External page  

Open in a separate window

  Files folder image Files  
File Role Description
Files folder imagelanguages (4 files)
Plain text file Intl.php Class Simple strings translation class like gettext with plural
Accessible without login Plain text file test.php Example usage example

  Files folder image Files  /  languages  
File Role Description
  Accessible without login Plain text file pl.json Data json plural strings
  Accessible without login Plain text file pl.php Conf. polish strings in php
  Accessible without login Plain text file pl.po Data Polish po getext file
  Accessible without login Plain text file ru.json Data rusian strings in json

 Version Control Unique User Downloads Download Rankings  
 0%
Total:170
This week:0
All time:8,834
This week:455Up
User Ratings User Comments (1)
 All time
Utility:100%StarStarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:-
Examples:93%StarStarStarStarStar
Tests:-
Videos:-
Overall:69%StarStarStarStar
Rank:340
 
nice
7 years ago (muabshir)
70%StarStarStarStar