# PHP i18n
This is a simple i18n class for PHP. Nothing fancy, but fast, because it uses caching and it is easy to use. Try it out!
Some of its features:
* Translations in `.ini`, `.json` or `.yaml` format
* File cache
* Simple API (`L::category_stringname`)
* Built-in support for [vprintf](http://php.net/manual/en/function.vprintf.php) formatting (`L::name($par1)`)
* Automatic finding out what language to use
* Simplicity ;)
## Requirements
* Write permissions in cache directory
* PHP 5.2 (only tested with this version, it maybe runs with other versions too)
* PHP SPL extension (installed by default)
## Setup
To use the i18n class, look at the example.php. You will find there a simple tutorial for this class in the file. Otherwise follow these easy five steps:
### 1. Create language files
To use this class, you have to use ini files for the translated strings. This could look like this:
`lang_en.ini` (English)
```ini
greeting = "Hello World!"
[category]
somethingother = "Something other..."
```
`lang_de.ini` (German)
```ini
greeting = "Hallo Welt!"
[category]
somethingother = "Etwas anderes..."
```
Save both files in the directory you will set in step 4.
The files must be named like the filePath setting, where '{LANGUAGE}' will be replaced by the chosen language, e.g. 'en' or 'de'.
### 2. Include the class
```php
```
### 3. Initialize the class
```php
```
### 4. Set some settings if necessary
The possible settings are:
* Language file path (the ini files) (default: `./lang/lang_{LANGUAGE}.ini`)
* Cache file path (default: `./langcache/`)
* The fallback language, if no one of the user languages is available (default: `en`)
* A 'prefix', the compiled class name (default `L`)
* A forced language, if you want to force a language (default: none)
* The section seperator: this is used to seperate the sections in the language class. If you set the seperator to `_abc_` you could access your localized strings via `L::category_abc_stringname` if you use categories in your ini. (default: `_`)
```php
```
#### Shorthand
There is also a shorthand for that: you can set all settings in the constructor!
```php
```
The (optional) parameters are:
1. the language file path (the ini files)
2. the language cache path
3. fallback language
4. the prefix/compiled class name
### 5. Call the `init()` method to load all files and translations
Call the `init()` file to instruct the class to load the needed language file, to load the cache file or generate it if it is not available and make the `L` class available so you can access your localizations.
```php
```
### 6. Use the localizations
To call your localizations, simple use the `L` class and a class constant for the string.
In this example, we use the translation string seen in step 1.
```php
```
As you can see, you can also call the constant as a function. It will be formatted with [vprintf](http://php.net/manual/en/function.vprintf.php).
Thats it!
## How it finds out the user language
This class tries to find out the user language by generating a queue of the following things:
1. Forced language (if set)
2. GET parameter 'lang' (`$_GET['lang']`)
3. SESSION parameter 'lang' (`$_SESSION['lang']`)
4. HTTP_ACCEPT_LANGUAGE (can be multiple languages) (`$_SERVER['HTTP_ACCEPT_LANGUAGE']`)
5. Fallback language
First it will remove duplicate elements and then it will replace all characters that are not A-Z, a-z or 0-9.
After that it searches for the language files. For example, if you set the GET parameter 'lang' to 'en' without a forced language set, the class would try to find the file `lang/lang_en.ini` (if the setting `langFilePath` was set to default (`lang/lang_{LANGUAGE}.ini`)).
If this file was not there, it would try to find the language file for the language defined in the session and so on.
### How to change this implementation
You can change this 'algorithm' by extending the i18n class. You could do it like that:
```php
```
This very basic extension of the i18n class replaces the default implementation of the `getUserLangs()`-method and only uses the GET parameter 'language' and the session parameter 'userlanguage'.
You see that this method must return an array.
**Note that this example function is insecure**: `getUserLangs()` also has to escape the results or else i18n will load every file!
## Fork it!
Contributions are always welcome.
# PHP i18nThis is a simple i18n class for PHP. Nothing fancy, but fast, because it uses caching and it is easy to use. Try it out!Some of its features:* Translations in `.ini`, `.json` or `.yaml` format* File cache* Simple API (`L::category_stringname`)* Built-in support for [vprintf](http://php.net/manual/en/function.vprintf.php) formatting (`L::name($par1)`)* Automatic finding out what language to use* Simplicity ;)## Requirements* Write permissions in cache directory* PHP 5.2 (only tested with this version, it maybe runs with other versions too)* PHP SPL extension (installed by default)## SetupTo use the i18n class, look at the example.php. You will find there a simple tutorial for this class in the file. Otherwise follow these easy five steps:### 1. Create language filesTo use this class, you have to use ini files for the translated strings. This could look like this:`lang_en.ini` (English)```inigreeting = "Hello World!"[category]somethingother = "Something other..."````lang_de.ini` (German)```inigreeting = "Hallo Welt!"[category]somethingother = "Etwas anderes..."```Save both files in the directory you will set in step 4.The files must be named like the filePath setting, where '{LANGUAGE}' will be replaced by the chosen language, e.g. 'en' or 'de'.### 2. Include the class```php require_once 'i18n.class.php';?>```### 3. Initialize the class```php $i18n = new i18n();?>```### 4. Set some settings if necessaryการตั้งค่าที่เป็นไปได้คือ:* เส้นทางของแฟ้มภาษา (แฟ้ม ini) (เริ่มต้น: ' . ลัง/.ini lang_ {ภาษา}')* พาธของไฟล์ที่แค (ค่าเริ่มต้น: ' . langcache /')* ย้อนกลับภาษา ถ้าไม่มีผู้ใช้ภาษามี (ค่าเริ่มต้น: 'น้ำ')* การ 'นำหน้า' ชื่อคอมไพล์คลา (ค่าเริ่มต้น 'L')* บังคับภาษา ถ้าคุณต้องการใช้ภาษา (ค่าเริ่มต้น: ไม่มี)* ส่วน seperator: ใช้แยกส่วนในชั้นเรียนภาษา ถ้าคุณตั้ง seperator เพื่อ '_abc_' คุณสามารถเข้าถึงสายอักขระภาษาท้องถิ่นของคุณผ่าน 'L::category_abc_stringname' ถ้าคุณใช้ประเภทของ ini (ค่าเริ่มต้น: '_')'' " php $i18n -> setCachePath('./tmp/cache') $i18n -> setFilePath('./langfiles/lang/lang_{LANGUAGE}.ini') เส้นทางของแฟ้มภาษา $i18n -> setFallbackLang('en') $i18n -> setPrefix('I') $i18n -> setForcedLang('en') / / กองทัพอังกฤษ แม้ว่าผู้ใช้ภาษาอื่นได้ $i18n -> setSectionSeperator('_')?>```### ย่อมีตัวย่อว่า: คุณสามารถตั้งค่าทั้งหมดในตัวสร้าง'' " php $i18n = i18n ใหม่ ('lang_ ลัง {ภาษา} .ini', ' langcache /', 'น้ำ');?>```มีพารามิเตอร์ (ถ้ามี):1.เส้นทางแฟ้มภาษา (แฟ้ม ini)2.เส้นทางแคภาษา3. ใช้แทนภาษา4. รวบรวมคำนำหน้า/ชื่อคลา### 5. เรียกวิธีการ 'init()' ที่จะโหลดแฟ้มทั้งหมดและการแปลเรียกแฟ้ม 'init()' สอนคลาโหลดแฟ้มภาษาที่จำเป็น โหลดไฟล์แคช หรือสร้างถ้าไม่มี และทำให้ชั้น 'L' พร้อมให้คุณ localizations ของคุณ'' " php $i18n -> init()?>```### 6. ใช้ที่ localizationsโทร localizations ของคุณ ใช้คลาส 'L' และคงระดับสำหรับข้อความในตัวอย่างนี้ เราใช้แปลสายอักขระที่เห็นในขั้นตอนที่ 1'' " php echo L::greeting ถ้าใช้ 'น้ำ': 'ประเทศ' echo L::category_somethingother ถ้าใช้ 'น้ำ': ' สิ่งอื่น ๆ...' echo L::last_modified("today") อาจเป็น: "ล่าสุด: วันนี้ '?>```คุณสามารถดู คุณยังสามารถเรียกค่าคงที่เป็นฟังก์ชัน มันจะถูกฟอร์แมต ด้วย [vprintf] (ส่วน http://php.net/manual/en/function.vprintf.php)Thats มัน# วิธีมันพบภาษาของผู้ใช้เรียนพยายามค้นหาภาษาของผู้ใช้ โดยการสร้างคิวในสิ่งต่อไปนี้:1. บังคับภาษา (ถ้ากำหนด)2. รับพารามิเตอร์ 'ทองหลาง' ('$_GET['lang']')3. ช่วงพารามิเตอร์ 'ทองหลาง' ('$_SESSION['lang']')4. HTTP_ACCEPT_LANGUAGE (ได้หลายภาษา) ('$_SERVER['HTTP_ACCEPT_LANGUAGE']')5. ใช้แทนภาษาครั้งแรกมันจะเอาองค์ประกอบที่ซ้ำกัน และมันจะแทนอักขระทั้งหมดที่ไม่ใช่ A-Z, a-z หรือ 0-9หลังจากนั้น มันค้นหาแฟ้มภาษา ตัวอย่าง ถ้าคุณกำหนดพารามิเตอร์รับ 'ทองหลาง' กับ 'น้ำ' โดยไม่มีชุดภาษาที่บังคับ ชั้นจะลองเพื่อค้นหาแฟ้ม 'lang/lang_en.ini' (ถ้าตั้งค่า 'langFilePath' ถูกตั้งค่าเริ่มต้น ('lang_ ลัง {ภาษา} .ini'))ถ้าแฟ้มนี้ไม่มี มันจะพยายามหาไฟล์ภาษาสำหรับภาษาที่กำหนดในงานและ### วิธีการเปลี่ยนใช้งานนี้คุณสามารถเปลี่ยนแปลงนี้ 'อัลกอริทึม' ขยายคลาส i18n คุณสามารถทำเช่นนั้น:'' " php require_once 'i18n.class.php' คลา My_i18n ขยาย i18n { {getUserLangs() ฟังก์ชันสาธารณะ $userLangs = array() ใหม่ [$userLangs] = $_GET ['ภาษา']; [$userLangs] = $_SESSION ['userlanguage']; $userLangs กลับ } } $i18n = My_i18n() ใหม่ // [...]?>```ส่วนขยายนี้มากของคลา i18n แทนใช้งานเริ่มต้นของ '() getUserLangs'-วิธี และใช้เฉพาะ รับพารามิเตอร์ 'ภาษา' และพารามิเตอร์ของเซสชัน 'userlanguage'คุณดูว่า วิธีการนี้ต้องส่งกลับอาร์เรย์** สังเกตว่า ฟังก์ชันนี้อย่างไม่ปลอดภัย **: 'getUserLangs()' มีหนีผลหรือ อื่น ๆ i18n จะโหลดทุกไฟล์# fork มันผลงานต้อนรับท่านเสมอ
การแปล กรุณารอสักครู่..
