novasa / laravel-language-center
Laravel 语言中心。
Requires
- guzzlehttp/guzzle: >=6.0
- laravel/framework: >=5.1
This package is auto-updated.
Last update: 2024-09-06 12:34:55 UTC
README
Laravel 语言中心。
这需要访问 Novasa 的语言中心。
安装
运行 composer require novasa/laravel-language-center:~2.0
。
在 config/app.php
中,将 Illuminate\Translation\TranslationServiceProvider::class,
替换为 Novasa\LaravelLanguageCenter\TranslationServiceProvider::class,
在 providers
-部分。
然后向您的应用程序添加以下环境
LANGUAGE_CENTER_URL=The url for API v1 (with ending slash) (required)
LANGUAGE_CENTER_USERNAME=Language Center username for Basic Auth (required)
LANGUAGE_CENTER_PASSWORD=Language Center password for Basic Auth (required)
LANGUAGE_CENTER_PLATFORM=The default platform to use. (default 'web')
LANGUAGE_CENTER_UPDATE_AFTER=The amount of seconds before updating the LanguageCenter data. (default '60' seconds)
要发布配置文件,运行
php artisan vendor:publish --provider="Novasa\LaravelLanguageCenter\TranslationServiceProvider" --tag="config"
用法
您可以使用 Laravel 标准的 trans
-函数。
trans('header.login');
将返回字符串 ID
header.login
的值。
然而,如果header.login
的字符串 ID 不存在,它将返回header.login
。
trans('header.welcome_back', [
'username' => 'Mark',
])
将返回字符串 ID
header.welcome_back
的值,但将:username
替换为Mark
在翻译中。然而,如果header.welcome_back
的字符串 ID 不存在,它将返回header.welcome_back
。
trans([
'key' => 'header.hello_world',
'string' => 'Hello World!',
])
将返回字符串 ID
header.hello_world
的值。然而,如果header.hello_world
的字符串 ID 不存在,它将返回Hello World!
。
trans([
'key' => 'header.hello_user',
'string' => 'Hello :username!',
], [
'username' => 'Mark',
])
将返回字符串 ID
header.hello_user
的值,但将:username
替换为Mark
在翻译中。然而,如果header.hello_user
的字符串 ID 不存在,它将返回Hello Mark!
。
trans([
'key' => 'header.download',
'string' => 'You should download our iOS app!',
'platform' => 'ios',
])
将返回字符串 ID
header.download
的值,但针对平台ios
。然而,如果header.download
的字符串 ID 不存在,它将返回您应该下载我们的 iOS 应用!
。
trans([
'key' => 'footer.copyright',
'string' => 'Copyright 2016 Novasa',
'comment' => 'A comment that you would like to show at the Language Center.',
])
将返回字符串 ID
footer.copyright
的值。然而,如果footer.copyright
的字符串 ID 不存在,它将返回版权所有 2016 Novasa
。这还会向语言中心添加注释。
注意
如果不存在翻译,它将自动在语言中心创建。
另外,请注意,Laravel 配置中的 locale
和 local_fallback
被语言中心的设置覆盖。