在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):translation/laravel开源软件地址(OpenSource Url):https://github.com/translation/laravel开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Translation.io client for Laravel 5.5+ to 9.xAdd this package to localize your Laravel application. Use the official Laravel syntax (with PHP or JSON files), or use the GetText syntax. Write only the source text, and keep it synchronized with your translators on Translation.io. Technical Demo (2.5min) Need help? [email protected] Table of contents
Translation syntaxesLaravel Localization (PHP key/values)The default Laravel method to localize. // Regular
__('inbox.title');
// Regular with sublevel key
__('inbox.menu.title');
// Pluralization
trans_choice('inbox.message', $number);
// Interpolation
__('inbox.hello', ['name' => $user->name]); With the PHP file return [
'title' => 'Title to be translated',
'hello' => 'Hello :name',
'messages' => 'One message|Many messages',
'menu' => [
'title' => 'Title of menu'
]
]; Notes:
Laravel Localization (JSON source text)A new feature of Laravel 5.4
is the possibility to use These translations are stored into JSON files located in the // Regular
__("Text to be translated");
// Pluralization
trans_choice(__('One message|Many messages'), $number);
// Interpolation
__('Hello :name', ['name' => $user->name]); With the JSON file {
"Text to be translated": "",
"One message|Many messages": "",
"Hello :name": ""
} Notes:
public function boot()
{
$loader = $this->app['translation.loader'];
// or 'lang/my_feature' in Laravel >= 9
$loader->addJsonPath(base_path('resources/lang/my_feature'));
} GetTextThis package adds the GetText support to Laravel. We strongly suggest that you use GetText to localize your application since it allows an easier and more complete syntax. Moreover, you won't need to create and manage any PHP or JSON file since your code will be automatically scanned for any string to translate. // Regular
t("Text to be translated");
// Pluralization
n("Singular text", "Plural text", $number);
// Regular with context
p("context", "Text to be translated");
// Pluralization with context
np("context", "Singular text", "Plural text", $number);
// Simple Interpolations (works with n, p and np too)
t('Hello %s', $user->name);
// Complex Interpolations (works with n, p and np too)
t(':city1 is bigger than :city2', [ ':city1' => 'NYC', ':city2' => 'BXL' ]); Installation
$ composer require tio/laravel
The initializer looks like this: return [
'key' => env('TRANSLATIONIO_KEY'),
'source_locale' => 'en',
'target_locales' => ['fr', 'nl', 'de', 'es']
];
$ php artisan translation:init If you need to add or remove languages in the future, please read this section about that. UsageSyncTo send new translatable keys/strings and get new translations from Translation.io, simply run: $ php artisan translation:sync Sync and Show PurgeableIf you need to find out what are the unused keys/strings from Translation.io, using the current branch as reference: $ php artisan translation:sync_and_show_purgeable As the name says, this operation will also perform a sync at the same time. Sync and PurgeIf you need to remove unused keys/strings from Translation.io, using the current branch as reference: $ php artisan translation:sync_and_purge As the name says, this operation will also perform a sync at the same time. Warning: all keys that are not present in the current local branch will be permanently deleted from Translation.io. Manage LanguagesAdd or Remove LanguageYou can add or remove a language by updating If you want to add a new language with existing translations (ex. if you already have
a translated PHP file in your Edit LanguageTo edit existing languages while keeping their translations (e.g. changing from
Since you created a new project, the translation history and tags will unfortunately be lost. Custom LanguagesA custom language is always derived from an existing language. It's useful if you want to adapt some translations to another instance of your application, or to a specific customer. The structure of a custom language is: Examples: Custom languages can be added and used like any other language. Change the current localeGloballyThe easiest way to change the current locale is with the // in routes/web.php
// Solution 1: Apply the locale selection to root.
// => https://yourdomain.com?locale=fr
Route::get('/', function () {
return view('welcome');
})->middleware('set.locale');
// Solution 2: Apply the locale selection to many routes.
// => https://yourdomain.com/...?locale=fr
Route::middleware('set.locale')->group(function () {
Route::get('/', function () {
return view('welcome');
});
});
// Solution 3: prefix your routes with the locale and apply it.
// => https://yourdomain.com/fr
// => https://yourdomain.com/fr/...
Route::prefix('{locale?}')->middleware('set.locale')->group(function() {
Route::get('/', function () {
return view('welcome');
});
}); First time the user will connect, it will automatically set the locale extracted
from the browser The LocallyChange the current locale with: use Tio\Laravel\Facade as Translation;
Translation::setLocale('fr'); Frontend LocalizationUsing this PackageThis package is also able to cover frontend localization (React, Vue, ...). There are several ways to pass the translation strings from the backend
to the frontend: JavaScript serialization, The easiest strategy when dealing with React/Vue would be to pass the corresponding translations as props when mounting the components. Notes:
Using our official React & JavaScript packageAs Translation.io is directly integrated in the great Lingui internationalization framework, you can also consider frontend localization as a completely different localization project. Please read more about this on:
Advanced Configuration OptionsThe Some options are described below but for an exhaustive list, please refer to translation.php. Ignored PHP keysIf you would like to ignore specific PHP keys, or even entire PHP files or
subdirectories from the source language, you can use the For example: return [
...
'ignored_key_prefixes' => [
'validation', // ignore the whole validation.php file.
'validation.custom', // ignore the "custom" subtree in validation.php file.
'subfolder/more', // ignore the whole subfolder/more.php file.
],
...
]; TestingTo run the specs with oldest dependencies: $ composer update --no-interaction --prefer-stable --prefer-lowest
$ ./vendor/bin/phpunit To run the specs with latest dependencies: $ composer update --no-interaction --prefer-stable
$ ./vendor/bin/phpunit ContributingPlease read the CONTRIBUTING file. List of clients for Translation.ioThese implementations were usually started by contributors for their own projects. Some of them are officially supported by Translation.io and some are not yet supported. However, they are quite well documented. Thanks a lot to these contributors for their hard work! Ruby on Rails (Ruby)Officially Supported on https://translation.io/rails
Credits: @aurels, @michaelhoste Laravel (PHP)Officially Supported on https://translation.io/laravel
Credits: @armandsar, @michaelhoste React, React Native and JavaScriptOfficially Supported on https://translation.io/lingui Translation.io is directly integrated in the great Lingui internationalization project.
OthersIf you want to create a new client for your favorite language or framework, please read our Create a Translation.io Library guide and use the special init and sync endpoints. You can also use the more traditional API. Feel free to contact us on [email protected] if you need some help or if you want to share your library. LicenseThe MIT License (MIT). Please see License File for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论