nodes / translate
Enable usage of translation services rather than local files
Requires
- laravel/framework: >=5.1.0 <6.15.0
- nodes/core: 1.1.*
This package is auto-updated.
Last update: 2024-05-17 19:30:58 UTC
README
A package to enable usage of translation services rather than local files.
📝 Introduction
At Nodes we create a lot of stuff, which needs to be supported in multiple languages. The translation feature that comes out of the box requires a developer to change it in a local file and commit and deploy that. But we wanted to use a service where other people - clients, project managers etc. - would be able to manage translations.
This package makes it easy to create translation providers and use them genericly in your Laravel application.
Right it only comes with support for:
But we very much welcome pull requests with providers to other services.
📦 Installation
To install this package you will need:
- Laravel 5.1+
- PHP 5.5.9+
You must then modify your composer.json
file and run composer update
to include the latest version of the package in your project.
"require": { "nodes/translate": "^1.0" }
Or you can run the composer require command from your terminal.
composer require nodes/translate:^1.0
🔧 Setup
In Laravel 5.5 and above, service providers and aliases are automatically registered. If you're using Laravel 5.5 or above, skip ahead directly to Publish config files.
Setup service provider in config/app.php
Nodes\Translate\ServiceProvider::class
Setup alias in config/app.php
'Translate' => Nodes\Translate\Support\Facades\Translate::class
Publish config files
php artisan vendor:publish --provider="Nodes\Translate\ServiceProvider"
If you want to overwrite any existing config files use the --force
parameter
php artisan vendor:publish --provider="Nodes\Translate\ServiceProvider" --force
⚙ Usage
Global methods
translate($key, $replacements = [], $locale = null, $platform = null) translate_app($app, $key, $replacements = [], $locale = null, $platform = null) translate_or_fail($key, array $replacements = [], $locale = null, $platform = null) translate_app_or_fail($application, $key, $replacements = [], $locale = null, $platform = null) translate_with_fallback($key, $fallback, array $replacements = [], $locale = null, $platform = null) translate_app_with_fallback($application, $key, $fallback, $replacements = [], $locale = null, $platform = null)
Fallbacks
There are two ways to handle fallbacks, if key is either missing or NStack is down and cache is invalid
-
By default the translate() func will use laravel's trans('nstack.' . key) as fallback, that means you can download the nstack keys and reformat them to a php array in insert the file into
/ressources/lang/en/nstack.php
-
Use the translate_with_fallback and decide what to use
🏆 Credits
This package is developed and maintained by the PHP team at Nodes
📄 License
This package is open-sourced software licensed under the MIT license