eusonlito / laravel-gettext
简单的gettext加载器和解析器
v2.0.8
2019-09-05 17:47 UTC
Requires
- php: >=5.3
- gettext/gettext: 3.6.*
This package is auto-updated.
Last update: 2024-09-06 04:59:37 UTC
README
使用此包,您可以加载/解析/存储gettext字符串
安装
首先通过Composer安装此包。
{ "require": { "eusonlito/laravel-gettext": "2.0.*" } }
Laravel安装
注意:如果您正在使用Laravel 5.5或更高版本,以下步骤中的提供者和别名是不必要的。laravel-Gettext支持Laravel新的包发现。
// config/app.php 'providers' => [ '...', 'Eusonlito\LaravelGettext\GettextServiceProvider', ]; 'aliases' => [ '...', 'Gettext' => 'Eusonlito\LaravelGettext\Facade', ];
现在您有一个可用的Gettext
外观。
发布配置文件
php artisan vendor:publish
用法
__('Here your text'); __('Here your text with %s parameters', 1); __('Here your text with parameters %s and %s', 1, 2);
gettext文件
默认情况下,gettext .po和.mo文件存储在resources/gettext/xx_XX/LC_MESSAGES/messages.XX
xx_XX是语言代码,如en_US
、es_ES
等...
使用您自己的Gettext函数/助手
如果您想创建自己的替代gettext函数
// config/app.php 'providers' => [ '...', 'Eusonlito\LaravelGettext\GettextServiceProvider', 'App\Providers\GettextServiceProvider', ];
创建文件
// app/Providers/GettextServiceProvider.php <?php namespace App\Providers { use Illuminate\Support\ServiceProvider; class GettextServiceProvider extends ServiceProvider { public function register() { } } } namespace { function txt($original) { static $translator; if (empty($translator)) { $translator = app('gettext')->getTranslator(); } $text = $translator->gettext($original); if (func_num_args() === 1) { return $text; } $args = array_slice(func_get_args(), 1); return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); } }
配置
app/config/gettext.php
return array( /* |-------------------------------------------------------------------------- | Available locales |-------------------------------------------------------------------------- | | A array list with available locales to load | | Default locale will the first in array list | */ 'locales' => ['en_US', 'es_ES', 'it_IT', 'fr_FR'], /* |-------------------------------------------------------------------------- | Directories to scan |-------------------------------------------------------------------------- | | Set directories to scan to find gettext strings (starting with __) | */ 'directories' => ['app', 'resources'], /* |-------------------------------------------------------------------------- | Where the translations are stored |-------------------------------------------------------------------------- | | Full path is $storage/xx_XX/LC_MESSAGES/$domain.XX | */ 'storage' => 'storage/gettext', /* |-------------------------------------------------------------------------- | Store files as domain name |-------------------------------------------------------------------------- | | Full path is $storage/xx_XX/LC_MESSAGES/$domain.XX | */ 'domain' => 'messages', /* |-------------------------------------------------------------------------- | Use native gettext functions |-------------------------------------------------------------------------- | | Are faster than open files from PHP. If you have enabled the php-gettext | module, is recommended to enable. | */ 'native' => true, /* |-------------------------------------------------------------------------- | Use package gettext methods |-------------------------------------------------------------------------- | | Enable gettext methods: __, noop__, n__, p__, d__, dp__, np__, dnp__ | | Reference: https://github.com/oscarotero/Gettext/blob/master/src/translator_functions.php | */ 'functions' => false, /* |-------------------------------------------------------------------------- | Preference to load translations from format |-------------------------------------------------------------------------- | | Some systems and formats are fatest than others (low RAM or CPU usage) | Available options are mo, po, php | */ 'formats' => ['mo', 'php', 'po'], /* |-------------------------------------------------------------------------- | Cookie name |-------------------------------------------------------------------------- | | Locale cookie name. Cookie are stored as plain, without Laravel manager | */ 'cookie' => 'locale' );