PHP Classes

PHP i81n: Extract text to translate from application code

Recommend this page to a friend!
  Info   View files Example   View files View files (30)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2024-06-15 (Yesterday) RSS 2.0 feedNot enough user ratingsTotal: 369 All time: 6,824 This week: 79Up
Version License PHP version Categories
i81n 1.0.11GNU General Publi...5Localization, PHP 5, Tools, Parsers
Description 

Author

This class can extract text to translate from application code.

It can traverse the PHP code files of a project to extract strings that need to be translated to internationalize an application.

The class generates a configuration file in the .INI format that should be translated to other languages to be supported.

The application can read the translation configuration files to return application texts for different languages.

Innovation Award
PHP Programming Innovation award nominee
September 2014
Number 9


Prize: One year Codenvy Developer hosted cloud IDE premium plan
Adapting an application to a new languages may take a significant effort, especially if the application was not planned to be adapted to multiple languages from the start.

This class implements an automated approach to generate translation configuration files to internationalize PHP applications.

It scans the PHP code of an existing application to extract the text strings that need to be translated and generates .INI files for the translators to work on.

Manuel Lemos
Picture of Everton da Rosa
  Performance   Level  
Name: Everton da Rosa <contact>
Classes: 17 packages by
Country: Brazil Brazil
Innovation award
Innovation award
Nominee: 3x

Example

<!DOCTYPE html>
<!--
Copyright (C) 2014 Everton

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title>i81n :: Example of use</title>
    </head>
    <body>
        <h1>Example of use of the library i81n internationalization</h1>
        <?php
       
//$lang = 'pt_BR';//Sets the language to use
       
$lang = 'en_US';
        require
'i81n.php';//loads the i81n library
       
       
$i81n = new i81n($lang, '*');//creates an instance of translation for all domains available
       
       
printf("<p>%s.</p>", $i81n->translate('A biblioteca i81n possibilita a internacionalização de aplicações através do PHP sem a necessidade de gettext ou outra extenção do PHP'));
       
       
printf("<p>%s %s %s %s</p>", $i81n->translate('O idioma atual é '), $i81n->getLang(), $i81n->translate('Os arquivos estão armazenados no seguinte diretório: '), $i81n->getLocaleDir());
       
       
printf("<p>%s.</p>", $i81n->translate('i81n suporta domínios para que você possa dividir suas traduções em módulos.'));
       
?>
</body>
</html>


Details

i81n

<h1>i81n :: Gettext translation without gettext</h1>

    <p>i81n (whose name was inspired by the term i18n) is written in PHP for translation of pa'ginas and systems without the reliance on PHP extensions like gettext, eg class.</p>
    <h2>Installation</h2>
    <p>The installation for use only with the translation (without the parser) depends only on the inclusion of i81n.php file and setting the directory where translations are (default is "./locale").</p>
    <p>The "locale" directory (or other you want to specify) stores the translation files. For each language, there should be a subdirectory "locale" with the abbreviation of the language (en, en_US, for example). Within these subdirectories are the translation files generated by the parser.</p>
    <p>To change the directory "locale" to another, in the instance of i81n class that is created, use the setLocaleDir () method.</p>
    <h2>Use</h2>
    <p>The use is based on the creation of an instance of the class and i81n language setting to be used by setlang () method.</p>
    <p>Use the translate () method for the translation of messages (see sample files).</p>
    <p>You can also create alias for the method i81n :: translate (), is extending the i81n class or creating a function (you can even create a gettext function, for example).</p>
    <h2>Creating translations for your PHP files</h2>
    <p>i81n has a parser to extract the texts to be translated (which we call messages) straight from the pages of your application.</p>
    <p>Basically, the process follows these steps:</p>
    <ol>
        <li>Configuration INI file with the parameters of the parser (see parser_config_example.ini);</li>
        <li>Implementation of parser.bat file as argument stating the path to the INI configuration file;</li>
        <li>parser.bat runs parser.php file, which actually only serves to run the i81n :: parseDir () static method. You can create other ways of running the parser proque all the work is done by :: parseDir () method i81n.</li>
        <li>The parseDir :: i81n () method reads the parameters of the INI file and search the files of your application for the messages to be translated and placed them all in INI files in subdirectory "locale" for the language of your application;</li>
        <li>After that, just make copies of the translation INI files, one for each language (in separate subdirectories) and perform the translation of messages saved them.</li>
    </ol>
    <h2>The translation files</h2>
    <p>INI files translation are separated into sections where each section corresponds to the sha1 hash of the original message found in the files of your application. It is for this that the hash method i81n :: translate () finds the message to use.</p>
    <p>In each section, there are two parameters:</p>
    <ul>
        <li>omsg: the original message, used only as a reference to the time of the translation of messages in INI files.</li>
        <li>tmsg: the translated message that will be returned by the method i81n :: translate ().</li>
    </ul>
    <h2>Important Tips</h2>
    <p>Always use static, independent variables to messages that the translation is done correctly.</p>
    <p>Remember that any change in the messages, however small, will require a new implementation of the parser, and new manual translations.</p>
    <p>Before running the parser, backup INI files for translation.</p>
    <p>The examples are in example.php subdir_test files and directory.</p>

  Files folder image Files  
File Role Description
Files folder imagedoc (10 files, 1 directory)
Files folder imagelocale (1 directory)
Files folder imagesubdir_test (1 file)
Accessible without login Plain text file example.php Example Example script
Plain text file i81n.php Class Class source
Accessible without login Plain text file index.php Aux. Example script
Accessible without login Plain text file parser.bat Data Auxiliary data
Accessible without login Plain text file parser.php Example Example script
Accessible without login Plain text file parser_config_example.ini Data Auxiliary data
Accessible without login Plain text file README.md Data Documentation

  Files folder image Files  /  doc  
File Role Description
Files folder imageresources (11 files)
  Accessible without login Plain text file .zip Data Auxiliary data
  Accessible without login HTML file 404.html Doc. Documentation
  Accessible without login HTML file class-Exception.html Doc. Documentation
  Accessible without login HTML file class-i81n.html Doc. Documentation
  Accessible without login Plain text file elementlist.js Data Auxiliary data
  Accessible without login HTML file index.html Doc. Documentation
  Accessible without login HTML file namespace-None.html Doc. Documentation
  Accessible without login HTML file namespace-PHP.html Doc. Documentation
  Accessible without login HTML file source-class-i81n.html Doc. Documentation
  Accessible without login HTML file tree.html Doc. Documentation

  Files folder image Files  /  doc  /  resources  
File Role Description
  Accessible without login Image file collapsed.png Icon Icon image
  Accessible without login Plain text file combined.js Data Auxiliary data
  Accessible without login Image file footer.png Data Auxiliary data
  Accessible without login Image file inherit.png Icon Icon image
  Accessible without login Image file resize.png Icon Icon image
  Accessible without login Image file sort.png Icon Icon image
  Accessible without login Plain text file style.css Data Auxiliary data
  Accessible without login Image file tree-cleaner.png Icon Icon image
  Accessible without login Image file tree-hasnext.png Icon Icon image
  Accessible without login Image file tree-last.png Data Auxiliary data
  Accessible without login Image file tree-vertical.png Icon Icon image

  Files folder image Files  /  locale  
File Role Description
Files folder imagept_BR (1 file)

  Files folder image Files  /  locale  /  pt_BR  
File Role Description
  Accessible without login Plain text file main.ini Data Auxiliary data

  Files folder image Files  /  subdir_test  
File Role Description
  Accessible without login Plain text file test.php Example Auxiliary script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:369
This week:0
All time:6,824
This week:79Up