Gettext
=======
[![Build Status](https://travis-ci.org/oscarotero/Gettext.png?branch=master)](https://travis-ci.org/oscarotero/Gettext)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/oscarotero/Gettext/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/oscarotero/Gettext/?branch=master)
[![Latest Stable Version](https://poser.pugx.org/gettext/gettext/v/stable.svg)](https://packagist.org/packages/gettext/gettext)
[![Total Downloads](https://poser.pugx.org/gettext/gettext/downloads.svg)](https://packagist.org/packages/gettext/gettext)
[![Monthly Downloads](https://poser.pugx.org/gettext/gettext/d/monthly.png)](https://packagist.org/packages/gettext/gettext)
[![License](https://poser.pugx.org/gettext/gettext/license.svg)](https://packagist.org/packages/gettext/gettext)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/496dc2a6-43be-4046-a283-f8370239dd47/big.png)](https://insight.sensiolabs.com/projects/496dc2a6-43be-4046-a283-f8370239dd47)
Created by Oscar Otero <http://oscarotero.com> <oom@oscarotero.com> (MIT License)
Gettext is a PHP (>=5.4) library to import/export/edit gettext from PO, MO, PHP, JS files, etc.
## Installation
With composer (recomended):
```
composer require gettext/gettext
```
If you don't use composer in your project, you have to download and place this package in a directory of your project. You need to install also [gettext/languages](https://github.com/mlocati/cldr-to-gettext-plural-rules). Then, include the autoloaders of both projects in any place of your php code:
```php
include_once "libs/gettext/src/autoloader.php";
include_once "libs/cldr-to-gettext-plural-rules/src/autoloader.php";
```
## Classes and functions
This package contains the following classes:
* `Gettext\Translation` - A translation definition
* `Gettext\Translations` - A collection of translations
* `Gettext\Extractors\*` - Import translations from various sources (po, mo, php, js, etc)
* `Gettext\Generators\*` - Export translations to various formats (po, mo, php, json, etc)
* `Gettext\Translator` - To use the translations in your php templates instead the [gettext extension](http://php.net/gettext)
* `Gettext\GettextTranslator` - To use the [gettext extension](http://php.net/gettext)
## Usage example
```php
use Gettext\Translations;
//import from a .po file:
$translations = Translations::fromPoFile('locales/gl.po');
//edit some translations:
$translation = $translations->find(null, 'apple');
if ($translation) {
$translation->setTranslation('Mazá');
}
//export to a php array:
$translations->toPhpArrayFile('locales/gl.php');
//and to a .mo file
$translations->toMoFile('Locale/gl/LC_MESSAGES/messages.mo');
```
If you want use this translations in your php templates without using the gettext extension:
```php
use Gettext\Translator;
//Create the translator instance
$t = new Translator();
//Load your translations (exported as PhpArray):
$t->loadTranslations('locales/gl.php');
//Use it:
echo $t->gettext('apple'); // "Mazá"
//If you want use global functions:
$t->register();
echo __('apple'); // "Mazá"
```
To use this translations with the gettext extension:
```php
use Gettext\GettextTranslator;
//Create the translator instance
$t = new GettextTranslator();
//Set the language and load the domain
$t->setLanguage('gl');
$t->loadDomain('messages', 'Locale');
//Use it:
echo $t->gettext('apple'); // "Mazá"
//Or use the gettext functions
echo gettext('apple'); // "Mazá"
//If you want use the global functions
$t->register();
echo __('apple'); // "Mazá"
//And use sprintf/strtr placeholders
echo __('Hello %s', 'world'); //Hello world
echo __('Hello {name}', ['{name}' => 'world']); //Hello world
```
The benefits of using the functions provided by this library (`__()` instead `_()` or `gettext()`) are:
* You are using the same functions, no matter whether the translations are provided by gettext extension or any other method.
* You can use variables easier because `sprintf` functionality is included. For example: `__('Hello %s', 'world')` instead `sprintf(_('Hello %s'), 'world')`.
* You can also use named placeholders if the second argument is an array. For example: `__('Hello %name%', ['%name%' => 'world'])` instead of `strtr(_('Hello %name%'), ['%name%' => 'world'])`.
## Translation
The `Gettext\Translation` class stores all information about a translation: the original text, the translated text, source references, comments, etc.
```php
// __construct($context, $original, $plural)
$translation = new Gettext\Translation('comments', 'One comment', '%s comments');
$translation->setTranslation('Un comentario');
$translation->setPluralTranslation('%s comentarios');
$translation->addReference('templates/comments/comment.php', 34);
$translation->addComment('To display the amount of comments in a post');
echo $translation->getContext(); // comments
echo $translation->getOriginal(); // One comment
echo $translation->getTranslation(); // Un comentario
// etc...
```
## Translations
The `Gettext\Translations` class stores a collection of translations:
```php
$translations = new Gettext\Translations();
//You can add new translations using the array syntax
$translations[] = new Gettext\Translation('comments', 'One comment', '%s comments');
//Or using the "insert" method
$insertedTranslation = $translations->insert('comments', 'One comment', '%s comments');
//Find a specific translation
$translation = $translations->find('comments', 'One comment');
//Edit headers, domain, etc
$translations->setHeader('Last-Translator', 'Oscar Otero');
$translations->setDomain('my-blog');
```
## Extractors
The extrators allows to fetch gettext values from any source. For example, to scan a .po file:
```php
$translations = new Gettext\Translations();
//From a file
Gettext\Extractors\Po::fromFile('locales/en.po', $translations);
//From a string
$string = file_get_contents('locales2/en.po');
Gettext\Extractors\Po::fromString($string, $translations);
```
The better way to use extractors is using the magic methods of `Gettext\Translations`:
```php
//Create a Translations instance using a po file
$translations = Gettext\Translations::fromPoFile('locales/en.po');
//Add more messages from other files
$translations->addFromPoFile('locales2/en.po');
```
The available extractors are the following:
Name | Description | Example
---- | ----------- | --------
**Blade** | Scans a Blade template (For laravel users). | [example](https://github.com/oscarotero/Gettext/blob/master/tests/assets/blade/input.php)
**Csv** | Gets the messages from csv. | [example](https://github.com/oscarotero/Gettext/blob/master/tests/assets/po/Csv.csv)
**CsvDictionary** | Gets the messages from csv (without plurals and context). | [example](https://github.com/oscarotero/Gettext/blob/master/tests/assets/po/CsvDictionary.csv)
**Jed** | Gets the messages from a json compatible with [Jed](http://slexaxton.github.com/Jed/). | [example](https://github.com/oscarotero/Gettext/blob/master/tests/assets/po/Jed.json)
**JsCode** | Scans javascript code looking for all gettext functions (the same than PhpCode but for javascript). You can use [the javascript gettext-translator library](https://github.com/oscarotero/gettext-translator) | [example](https://github.com/oscarotero/Gettext/blob/master/tests/assets/jscode/input.js)
**Json** | Gets the messages from json compatible with [gettext-translator](https://github.com/oscarotero/gettext-translator). | [example](https://github.com/oscarotero/Gettext/blob/master/tests/assets/po/Json.json)
**JsonDictionary** | Gets the messages from a json (without plurals and context). | [example](https://github.com/oscarotero/Gettext/blob/master/tests/assets/po/JsonDictionary.json)
**Mo** | Gets the messages from MO. | [example](https://github.com/oscarotero/Gettext/blob/master/tests/assets/po/Mo.mo)
**PhpArray** | Gets the messages from a php file that ret
没有合适的资源?快使用搜索试试~ 我知道了~
Cacti for LinuxUnix(网络流量监测图形分析工具) v1.2.24.gz
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 54 浏览量
2024-03-18
21:13:14
上传
评论
收藏 40.98MB GZ 举报
温馨提示
共2000个文件
svg:838个
php:273个
js:200个
Cacti for LinuxUnix(网络流量监测图形分析工具) v1.2.24.gz
资源推荐
资源详情
资源评论
收起资源包目录
Cacti for LinuxUnix(网络流量监测图形分析工具) v1.2.24.gz (2000个子文件)
export-plural-rules.bat 280B
cacti_version 7B
openssl.cnf 104B
Gruntfile.coffee 950B
COMMITMENT 2KB
all.css 71KB
fontawesome.css 70KB
all.min.css 58KB
fontawesome.min.css 56KB
main.css 53KB
main.css 52KB
main.css 49KB
main.css 44KB
core.css 41KB
v4-shims.css 40KB
main.css 38KB
flag-icon.css 37KB
jquery-ui.css 36KB
jquery-ui.css 36KB
jquery-ui.css 36KB
jquery-ui.css 36KB
jquery-ui.css 36KB
jquery-ui.css 36KB
jquery-ui.css 36KB
flag-icon.min.css 33KB
main.css 30KB
style.css 30KB
style.css 30KB
style.css 29KB
style.css 29KB
style.css 29KB
style.css 29KB
style.css 29KB
v4-shims.min.css 26KB
style.min.css 25KB
style.min.css 25KB
style.min.css 25KB
style.min.css 25KB
style.min.css 25KB
style.min.css 25KB
Cacti-Github.css 16KB
svg-with-js.css 8KB
compact-landscape.css 8KB
compact-portrait.css 7KB
install.css 6KB
svg-with-js.min.css 6KB
jquery.zoom.css 6KB
jquery.zoom.css 6KB
jquery-ui.midwinter.css 6KB
jquery.zoom.css 5KB
jquery.zoom.css 5KB
jquery.zoom.css 5KB
jquery.zoom.css 5KB
jquery.zoom.css 5KB
jquery.colorpicker.css 5KB
jquery.colorpicker.css 5KB
jquery.colorpicker.css 5KB
jquery.colorpicker.css 5KB
jquery.colorpicker.css 5KB
jquery.colorpicker.css 5KB
jquery.colorpicker.css 5KB
billboard.midwinter.css 4KB
billboard.css 4KB
billboard.css 4KB
billboard.css 4KB
billboard.css 4KB
billboard.css 4KB
billboard.css 4KB
billboard.css 4KB
main.css 3KB
colors.css 3KB
jquery.multiselect.css 3KB
jquery.multiselect.css 3KB
jquery.multiselect.css 3KB
jquery.multiselect.css 3KB
jquery.multiselect.css 3KB
jquery.multiselect.css 3KB
jquery.multiselect.css 3KB
docs.css 3KB
pace.css 2KB
pace.css 2KB
pace.css 2KB
Diff.css 2KB
pace.css 2KB
Diff.css 2KB
Diff.css 2KB
pace.css 2KB
pace.css 2KB
pace.css 2KB
jquery.timepicker.css 2KB
jquery.timepicker.css 2KB
jquery.timepicker.css 2KB
jquery.timepicker.css 2KB
jquery.timepicker.css 2KB
jquery.timepicker.css 2KB
jquery.timepicker.css 2KB
Diff.css 2KB
Diff.css 2KB
Diff.css 2KB
Diff.css 2KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
芝麻粒儿
- 粉丝: 5w+
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功