Class Languages
version: 1.2.4, (*1)
date: 2017/10/17, (*2)
Author: Marcos Menezes <mmqfilho@gmail.com>, (*3)
Directory structure
| your_project_root
|-# class (class folder)
| |-# Mmqfilho
| |-# Languages
| |- Language.php (class file)
|
|-# languages (language folder)
| |-# en (english language folder)
| | |- your english language files
| |
| |-# pt-br (brazilian portuguese language folder)
| | |- your brazilian portuguese language files
| |
| |-# xxxx (other languages folder)
| |- others language files
How to use
include the file and set the namespace, (*4)
include_once 'class/Mmqfilho/Languages/Language.php';
or use autoload composer
include_once YOUR_VENDOR_DIR . '/autoload.php' ;
use mmqfilho\Languages\Language as Lang;
Create object, (*5)
$objLang = new Lang();
Or create object and set language to use, (*6)
$objLang = new Lang('en');
Or use setter, (*7)
$objLang->__set('default_language', 'en');
(optional) Setting the language folder if necessary (without slashes in the end), (*8)
$objLang->__set('directory', 'path/to/your/language/folder');
(optional) Show or not a message if not found the text, (*9)
$objLang->__set('show_message_not_found', true);
(optional) The message to show if not found the text, (*10)
$objLang->__set('message_not_found', 'I dont like default message');
(optional) number of recursive attempts in directories to find the language files (3 is the default), (*11)
$objLang->__set('recursiveDirCount', 3);
(optional) set the file type to load (json or xml) (json is the default file type), (*12)
$objLang->setXml();
or
$objLang->setJson();
Show the text
* Param 1 is the name of xml file without the '.xml'
* Param 2 is the name of xml tag
* Param 3 (optional) is a variables array, (*13)
echo $objLang->load('index', 'welcome');
or with an array with one or many variables
echo $objLang->load('index', 'yourName', array('Marcos'));
echo $objLang->load('index', 'yourFullName', array('Marcos', 'Menezes'));
The XML files
In the tag <languages> put your own translation tags, (*14)
If you use a html tag or special caracteres put into CDATA tag., (*15)
If you use a variable to send a parameter, put the '%s' in the tag, (*16)
<languages>
<welcome>Welcome</welcome>
<welcomeCData><![CDATA[<strong>Warning:</strong> Welcome %s.]]></welcomeCData>
<yourName><![CDATA[Your name is: %s.]]></yourName>
<yourFullName><![CDATA[Your full name is: %s %s.]]></yourFullName>
</languages>
The JSON files
If you use a variable to send a parameter, put the '%s' in the tag, (*17)
{
"welcome": "Welcome to json version",
"welcomeTag": "<strong>Warning:</strong> Welcome json.",
"version": "Version: %s"
}