在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Anahkiasen/polyglot开源软件地址(OpenSource Url):https://github.com/Anahkiasen/polyglot开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):PolyglotIntroductionPolyglot is a localization helper for the Laravel framework, it's an helper class to localize your routes, models and views. To install it, do Publish config to your laravel app : Model localizationSetting a model as polyglot will allow you to make fields speak several languages. Polyglot requires you to separate common fields from localized ones assuming the following common pattern : Take the example of a blog article model
From there you can either access any language easily by doing the following : class Article extends Polyglot
{
protected $polyglot = ['title', 'content'];
}
// Get an automatically localized Article
$article = Article::find(4)
echo $article->fr->title // This will print out the french title
echo $article->title // This will print out the title in the current language Polyglot also helps you saving localized attributes : $article->fill([
'title' => 'Titre',
'content' => 'Contenu',
'lang' => 'fr',
])->save();
// Is the same as
$article->fr->fill([
'title' => 'Titre',
'content' => 'Contenu',
])->save(); Globally speaking when Polyglot sees you're trying to save localized attribute on the parent model, it will automatically fetch the Lang model and save them on it instead.
If no Note that, as your attributes are now split into two tables, you can Polyglot eager load the correct Lang relation with the Routes localizationTo localize your routes, you need to set the Route::groupLocale(['before' => 'auth'], function() {
Route::get('/', 'HomeController@index');
Route::get('articles', 'ArticlesController@index');
// etc...
}); Now you can access Views localizationViews localization work by setting up gettext for you and providing two commands to extract translations from your views to PO files and compile those to MO files. This is currently only possible for Twig but will soon for Blade and classic PHP files, and will require the To use simply configure your domain in the configuration, then run Locales helpersPolyglot also provide various locale helpers hooked into the URL::locale() // Returns the locale in the current URL
Lang::active('fr') // Check if fr is the current locale
Lang::setInternalLocale('fr') // Set both the locale with the Translator class and setlocale method
Lang::valid('fr') // Check if a locale is valid
Lang::sanitize('fr') // Returns the locale if valid, or the default locale if not |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论