在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):shaggyz/laravel-gettext开源软件地址(OpenSource Url):https://github.com/shaggyz/laravel-gettext开源编程语言(OpenSource Language):PHP 99.1%开源软件介绍(OpenSource Introduction):https://github.com/Belphemur/laravel-gettextThis repo is no longer maintained. For newer versions of Laravel check this fork:Laravel GettextLaravel Gettext is a package compatible with the great Laravel PHP Framework. It provides a simple way to add localization support to Laravel applications. It is designed to work with GNU gettext and Poedit. Former versions of this package (before 4.x) works with the native php-gettext module. Current versions uses the Symfony translation package by default instead of native php extension. Latest Laravel 5.3.x stable release (4.0.4)
Older versionsLatest Laravel 5.2.x stable release (3.1.0) Latest Laravel 5.1.x stable release (3.0.3) Latest Laravel 5.0 stable release (2.0.3) Latest Laravel 4.x stable release (1.0.3) Development master Unstable, only for development (dev-master) 1. Requirements
Optional requirements if you want to use the native php-gettext extension:
2. InstallAdd the composer repository to your composer.json file: "xinax/laravel-gettext": "4.x" And run composer update. Once it's installed, you can register the service provider in config/app.php in the providers array: 'providers' = [
// ...
Xinax\LaravelGettext\LaravelGettextServiceProvider::class,
// ...
] Now you need to publish the configuration file in order to set your own application values: php artisan vendor:publish This command creates the package configuration file in: You also need to register the LaravelGettext middleware in the protected $middlewareGroups = [
'web' => [
// ...
\Xinax\LaravelGettext\Middleware\GettextMiddleware::class,
],
// ...
]
3. ConfigurationAt this time your application has full gettext support. Now you need to set some configuration values in /**
* Default locale: this will be the default for your application all
* localized strings. Is to be supposed that all strings are written
* on this language.
*/
'locale' => 'es_ES', /**
* Supported locales: An array containing all allowed languages
*/
'supported-locales' => array(
'es_ES',
'en_US',
'it_IT',
'es_AR',
), /**
* Default charset encoding.
*/
'encoding' => 'UTF-8', Ok, now it's configured. It's time to generate the directory structure and translation files for the first time.
php artisan gettext:create With this command the needed directories and files are created on resources/lang/i18n 4. WorkflowA. Write strings :DBy default LaravelGettext looks on app/Http/Controllers and resources/views recursively searching for translations. Translations are all texts printed with the __() function. Let's look a simple view example: // an example view file
echo 'Non translated string';
echo __('Translated string');
echo __('Another translated string');
// with parameter
$str = 'parameter';
$n = 2;
echo __('Translated string with %s', $str);
echo __('%dnd translated string with %s', [$n, $str]); // an example view in blade
{{ __('Translated string') }}
B. Plural stringsThe plural translations follow the same pattern above. Plural translations are all texts printed with the _n() function, and it follow the php ngettext. Let's look a simple view example: // an example view file
$n = 2;
echo ($n > 1) ? 'Non translated plural string' : 'Non translated string';
echo _n('Translated string', 'Translated plural string', $n);
// with parameter
$str = 'parameter';
echo _n('Translated string %s', 'Translated plural string %s', 2, $str); // an example view in blade
{{ _n('Translated string', 'Translated plural string', $n) }}
['_n:1,2', 'ngettext:1,2'] See Plural forms used by Poedit to configure for your language. C. Translate with PoeditOpen the PO file for the language that you want to translate with Poedit. The PO files are located by default in resources/lang/i18n/[locale]/LC_MESSAGES/[domain].po. If you have multiple gettext domains, one file is generated by each domain. Once Poedit is loaded press the Update button to load all localized strings. You can repeat this step anytime you add a new localized string. Fill translation fields in Poedit and save the file. The first time that you do this the MO files will be generated for each locale. C. Runtime methodsTo change configuration on runtime you have these methods: /**
* Sets the Current locale.
* Example param value: 'es_ES'
*
* @param mixed $locale the locale
* @return LaravelGettext
*/
LaravelGettext::setLocale($locale); /**
* Gets the Current locale.
* Example returned value: 'es_ES'
*
* @return String
*/
LaravelGettext::getLocale(); /**
* Gets the language portion of the locale.
* Eg from en_GB, returns en
*
* @return mixed
*/
LaravelGettext::getLocaleLanguage() /**
* Sets the Current encoding.
* Example param value: 'UTF-8'
*
* @param mixed $encoding the encoding
* @return LaravelGettext
*/
LaravelGettext::setEncoding($encoding); /**
* Gets the Current encoding.
* Example returned value: 'UTF-8'
*
* @return String
*/
LaravelGettext::getEncoding(); /**
* Sets the current domain
*
* @param String $domain
*/
LaravelGettext::setDomain($domain); /**
* Returns the current domain
*
* @return String
*/
LaravelGettext::getDomain(); /**
* Returns the language selector object
*
* @param Array $labels
* @return LanguageSelector
*/
LaravelGettext::getSelector($labels = []); 5. Features and examples:A. Route and controller implementation example:app/Http/routes.php Route::get('/lang/{locale?}', [
'as'=>'lang',
'uses'=>'HomeController@changeLang'
]); app/Http/Controllers/HomeController.php /**
* Changes the current language and returns to previous page
* @return Redirect
*/
public function changeLang($locale=null)
{
LaravelGettext::setLocale($locale);
return Redirect::to(URL::previous());
} B. A basic language selector example: <ul>
@foreach(Config::get('laravel-gettext.supported-locales') as $locale)
<li><a href="/lang/{{$locale}}">{{$locale}}</a></li>
@endforeach
</ul> C. Built-in language selector:You can use the built-in language selector in your views: // Plain php:
LaravelGettext::getSelector()->render();
// Blade views:
{!! LaravelGettext::getSelector()->render() !!} It also supports custom labels: LaravelGettext::getSelector([
'en_US' => 'English',
'es_ES' => 'Spanish',
'de_DE' => 'Deutsch',
])->render(); D. Adding source directories and domainsYou can achieve this editing the source-paths configuration array. By default resources/views and app/Http/Controllers are set. /**
* Paths where Poedit will search recursively for strings to translate.
* All paths are relative to app/ (don't use trailing slash).
*
* Remember to call artisan gettext:update after change this.
*/
'source-paths' => array(
'Http/Controllers',
'../resources/views',
'foo/bar', // app/foo/bar
), You may want your translations in different files. Translations in GNUGettext are separated by domains, domains are simply context names. Laravel-Gettext set always a default domain that contains all paths that doesn't belong to any domain, its name is established by the 'domain' configuration option. To add a new domain just wrap your paths in the desired domain name, like this example: 'source-paths' => array(
'frontend' => array(
'Http/Controllers',
'../resources/views/frontend',
),
'backend' => array(
'../resources/views/backend',
),
'../resources/views/misc',
), This configuration generates three translation files by each language: messages.po, frontend.po and backend.po To change the current domain in runtime (a route-middleware would be a nice place for do this): LaravelGettext::setDomain("backend"); Remember: update your gettext files every time you change the 'source-paths' option, otherwise is not necessary. php artisan gettext:update This command will update your PO files and will keep the current translations intact. After this you can open Poedit and click on update button to add the new text strings in the new paths. You can update only the files of a single domain with the same command: php artisan gettext:update --domain backend E. About gettext cache (only applies to php-gettext native module)Sometimes when you edit/add translations on PO files the changes does not appear instantly. This is because the gettext cache system holds content. The most quick fix is restart your web server. 6. ContributingIf you want to help with the development of this package, you can:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论