Package Data | |
---|---|
Maintainer Username: | jcarrizalez |
Maintainer Contact: | sitgem@gmail.com (Juan -Carrizalez) |
Package Create Date: | 2019-04-20 |
Package Last Update: | 2019-04-24 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-23 03:22:26 |
Package Statistics | |
---|---|
Total Downloads: | 234 |
Monthly Downloads: | 5 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 1 |
Total Forks: | 0 |
Total Open Issues: | 0 |
[ [
Translator is a package to generate data and to translate data:
Read the Lumen Application Seeders files and write the json files with a friendly structure for editing "see configuration"
Has several methods to translate array, obejct and string can receive a matrix of objects with array and the answer will be the translation of it without altering its structure
Install the latest version with
$ composer require lumen/translator
or edit your composer.json
{
"require": {
"lumen/translator": "^2.0@dev"
}
}
$ composer update
$ php vendor/lumen/translator/configuration.php --config
The previous command will create the following directory and files:
your_app_name/
├── translator/
│ ├── configuration.json
│ ├── default.json
│ ├── en-AR.json
│ ├── en-US.json
│ └── pt-BR.json
configuration.json:
{
"default": "",
"column_table": [],
"not_tables": [],
"file_config": [],
"filter_column": []
}
Desciption configuration.json:
Configuration file with
`default`: default language example file name "es-AR"
`column_table`: columns of the tables to read (only columns string, text, varchar*)
`not_tables`: tables that will not be translated
`file_config`: configuration translation files that are in your_app_name/config/
`filter_column`: columns with data that does not want translation
default.json:
{
"table_a.000": "Hola Mundo",
"table_b.001": "Mi nombre es",
"table_b.002": "el mundo es todo"
}
Description default.json:
dynamic data created by the execution of seeders
es-AR.json:
{
"table_a.000": "Hola Mundo",
"table_b.001": "Mi nombre es",
"table_b.002": "el mundo es todo"
}
Description es-AR.json:
for user data and you must add the keys that are in default.json with the translation that applies
en-US.json:
{
"table_a.000": "Hello World",
"table_b.001": "My name is",
"table_b.002": "the world is all"
}
Description en-US.json:
manual data and you must add the keys that are in default.json with the translation that applies
pt-BR.json:
{
"table_a.000": "Olá mundo",
"table_b.001": "Meu nome é",
"table_b.002": "o mundo é tudo"
}
Description pt-BR.json:
manual data and you must add the keys that are in default.json with the translation that applies
Edit your .gitignore
and add
/laravel/translator/default.json
Create Traits for more control and use in /app/Traits/Translator.php
<?php
namespace App\Traits;
use Translator\Translator;
use Translator\Build;
trait Translator {
/**
* @return mixed
*/
public function dictionaryBuild(){
(new Build())->make();
}
/**
* @return array
*/
public function dictionaries(){
return (new Translator())->dictionaries();
}
/**
* @return string
*/
public function dictionaryDefault(){
return (new Translator())->default();
}
/**
* indicative if it is going to translate in favor of the user or the system (save system as TRUE)
*
* @param $data data to translation
* @param $system TRUE => as in database or FALSE => user language
* @return mixed
*/
public function dictionary($data, $system=FALSE){
return (new Translator())->dictionary($data, $system);
}
/**
* @return string
*/
public function language($value = NULL){
$languageC = new Translator();
$language = $languageC->language($value);
$language = $languageC->validateLanguage($language);
return $language;
}
/**
* @param $request
* @return mixed
*/
public function dictionaryRequest($request){
return (new Translator())->dictionaryRequest($request);
}
/**
* Translation method indicating the language of the system
* @param $data
* @param $language
* @return mixed
*/
public function dictionarySetLanguage($data, $language){
return (new Translator())->dictionarySetLanguage($data, $language);
}
}
$this->dictionaryDefault()
to determine the language you are sending from the headers (if it does not exist it's null)
$this->dictionary($data)
used when we want to return a data with translation
$this->dictionaryRequest($request)
used when we receive data in some language and it is passed to data according to default.json
$this->dictionaryBuild()
rewrite the file translator/default.json
with the existing data in your database read from the seeder of your application
frontend
en las headers
Accept-Language=es-AR
Note
: if Accept-Language is not sent, the default is ../translator/configuration.json
, also if you only send two letters as they are en
, pt
, es
there are no problems only that backend will take the first language that you get with those features
backend
<?php
use App\Traits\Translator;
class yourClassX {
use Translator;
public function yourMethodXGet(){
//part of your code
$your_data_example = ['product', 'call', ['colour' => 'white']];
//use of translator
$data = $this->dictionary($your_data_example);
//part of your code
return response()->json($data, 200);
}
public function yourMethodXPostUpdate(Request $request){
//translator to all the request
$request = $this->dictionaryRequest($request);
//part of your code
}
public function yourMethodX(){
//part of your code
$your_data_example = ['product', 'call', ['colour' => 'white']];
//use of translator
$your_lenguage = 'en-US';
$language = isset($your_lenguage) ? $your_lenguage : $this->dictionaryDefault();
$language = $this->language($language);
$text = $this->dictionarySetLanguage($your_data_example, $language);
}
}
Edit your ../database/seeds/DatabaseSeeder.php
and add
<?php
use App\Traits\Translator;
class DatabaseSeeder extends Seeder {
use Translator;
public function run(){
//part of your code
$this->dictionaryBuild();
}
}
-run your seeders
$ php artisan db:seed
Manuel Martinez - sitgem@gmail.com
Translator is licensed under the MIT License - see the LICENSE
file for details