Package Data | |
---|---|
Maintainer Username: | bernardomacedo |
Maintainer Contact: | bernardo.macedo@gmail.com (Bernardo Sousa de Macedo) |
Package Create Date: | 2015-12-17 |
Package Last Update: | 2018-04-18 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-19 03:02:41 |
Package Statistics | |
---|---|
Total Downloads: | 81 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 4 |
Total Watchers: | 1 |
Total Forks: | 0 |
Total Open Issues: | 0 |
This package allows you to add translations to database and generates the localization folders per group.
Note:
Laravel Framework 5.6
$ composer require bernardomacedo/laravel-db-translator
First register the service provider and facade in your application.
// config/app.php
'providers' => [
...
bernardomacedo\DBTranslator\DBTranslatorServiceProvider::class,
];
'aliases' => [
...
'DBTranslator' => bernardomacedo\DBTranslator\DBTranslatorFacade::class,
];
To publish all settings...
php artisan vendor:publish --provider="bernardomacedo\DBTranslator\DBTranslatorServiceProvider"
...or individually:
$ php artisan vendor:publish --provider="bernardomacedo\DBTranslator\DBTranslatorServiceProvider" --tag="config"
$ php artisan vendor:publish --provider="bernardomacedo\DBTranslator\DBTranslatorServiceProvider" --tag="migrations"
$ php artisan vendor:publish --provider="bernardomacedo\DBTranslator\DBTranslatorServiceProvider" --tag="lang"
And run migrations
$ php artisan migrate
Add a disk to the filesystems.php
filestorage:
If you change the default disk name, because it might conflict with another package or with a potential future one, be sure to change it under the published db-translator.php => storage_driver` parameter.
'disks' => [
...
'translator' => [
'driver' => 'local',
'root' => base_path('resources/lang/vendor/dbtranslator')
],
...
Default language set to App::getLocale()
.
In a blade template use:
function lang($text = false, $vars = null, $value = null, $group = null, $locale = null)
{{ lang('some text to translate') }}
{{ lang(':count apple named :name|:count apples named :name', ['name' => 'Bernardo'], 2) }}
{{ lang('{0} There are no apples (:count) named :name|[1,19] There are some (:count) apples named :name|[20,Inf] There are many (:count) apples named :name', ['name' => 'Bernardo'], 2) }}
This translation method is easier to interpret because even if the translation is not found, the text you input will be returned.
{{ lang('some text to translate') }} // returns 'algum texto para traduzir'
{{ lang('some text to translate', null, null, null, 'ru') }} // returns 'какой-нибудь текст' bypassing the current language forcing a locale.
{{ lang('this text does not exists on the database') }} // returns 'this text does not exists on the database' and will be added for future translation
Sometimes you need to generate a specific translation for a context based situation. Where the same phrase or text you wish to translate, means something different in other languages. So, the group parameter allows you to differentiate the same translation to be translated differently depending on context.
{{ lang('participations') }} /* general group assumed */
{{ lang('participations', null, null, 'some_group') }} /* some_group group assumed */
When using dynamic variables for translation, be sure to force a group named 'dynamic_...'
{{ lang($language_name, null, null, 'dynamic_language') }} /* language group assumed with dynamic flag on database */
{{ lang($SomeDynamicVar, null, null, 'dynamic_some_group') }} /* some_group group assumed with dynamic flag on database */
DBTranslator::doTranslation($variable_id, $text, $language_id, $group = 'general');
DBTranslator will try to find the $variable_id
and the $language_id
for you if you only supply strings.
DBTranslator::doTranslation('This is cool', 'Isto é cool', 'pt');
Sometimes you wish to create a variable for translating, adding a translation directly on a language you choose.
So, supplying a string on $variable_id that does not exist on the translations_variables
table, will generate a new one, adding an entry to the translations_translated
table directly.
on a controller class
use bernardomacedo\DBTranslator\DBTranslator;
class SomeControllerName extends BaseController
{
public function generate_translations()
{
/**
* This will generate the language translations for all
* translated texts in the database, and will assume
* the original language by default.
* This will work on all languages active by default.
*/
DBTranslator::generate(); /* will generate all translations */
DBTranslator::generate('pt'); /* will generate the Portuguese translations */
DBTranslator::generate(64); /* will generate the Portuguese translations based on ID */
/**
* Redirect or do whatever you wish after generation
*/
return redirect()->route('home');
}
}
This will create all required files under the directory supplied on the filesystems.php
file.
resources/lang/vendor/dbtranslator/
- en
- general.php
- some_group.php
- pt
- general.php
- some_group.php
use bernardomacedo\DBTranslator\Models\Intl;
class SomeControllerName extends BaseController
{
public function some_function()
{
$all = Intl::all();
$group = Intl::group('general')->get();
}
}
use bernardomacedo\DBTranslator\Models\Translated;
class SomeControllerName extends BaseController
{
public function some_function()
{
/**
* Gets all available translations
*/
$all = Translated::all();
/**
* Gets all available translations
*/
$portuguese = Translated::language('pt')->get(); // using string ISO
$portuguese = Translated::language(64)->get(); // using ID for the language
}
}
You can add translations to database without rendering a view.
For this you can run a artisan command and Laravel Database Translator
will check your view folders configured under config/view.php
config file paths
and will add any element of lang(*) found to the database.
$ php artisan dbtranslator:add
$ php artisan dbtranslator:remove
When running these commands, Dynamic groups
and `$variables will be ignored.
eg:
lang($php_var) /* is not supported so they will be ignored */
Generates for Portuguese
$ php artisan dbtranslator:generate pt
Generates for Spanish
$ php artisan dbtranslator:generate es
Generates all languages
$ php artisan dbtranslator:generate --status=all
Generates active languages
$ php artisan dbtranslator:generate --status=active
Generates inactive languages
$ php artisan dbtranslator:generate --status=inactive
The Laravel Database Translator is open-sourced software licensed under the MIT license