Package Data | |
---|---|
Maintainer Username: | 24aitor |
Maintainer Contact: | contact@aitorriba.com (Aitor Riba) |
Package Create Date: | 2016-09-28 |
Package Last Update: | 2017-12-21 |
Home Page: | https://aitorriba.com |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-25 15:02:01 |
Package Statistics | |
---|---|
Total Downloads: | 306 |
Monthly Downloads: | 2 |
Daily Downloads: | 0 |
Total Stars: | 45 |
Total Watchers: | 5 |
Total Forks: | 8 |
Total Open Issues: | 1 |
Laralang is a laravel packages that allows you to translate your app or parts of your app from views using translations services like google or mymemory, and then store it on your DB to filter and manage easily from our admin panel.
Moreover, allows you to translate PHP files easily and then save lot of time to you.
Personally I think the best way to translate your website is using the Laravel methods such as @lang()
but sometimes it's impossible.
Imagine you have a blog, and you need to translate it to different languages. With Laralang it's soo easy to do it, only place a little code like
following example and laralang will translate it from your favourite translation service or well loading a stored translation on your database.
{{ Laralang::trans($post->content) }}
Running the command below:
composer require aitor24/laralang
Include the line below to config/app.php inside providers array :
Aitor24\Laralang\LaralangServiceProvider::class,
Include the line below to config/app.php inside aliases array :
'Laralang' => Aitor24\Laralang\Facades\Laralang::class,
It will publish assets and config file.
Running the command below:
php artisan vendor:publish
Running the command below:
php artisan migrate
STRONGLY IMPORTANT: Change the password on config (Default password: laralangAdmin )
Apart from the password, the rest of default values can be modified also on config/laralang.php
. Furthermore you can modify these in a specific translation with the functions below.
Available Translators: Google Translator, MyMemory, Apertium (Strongly not recomended)
config values for translators: 'google', 'mymemory', 'apertium'
Then few examples of use laralang:
<center>
{!! Laralang::trans('Hello world!') !!}
<br>
{!! Laralang::trans('Hello world!')->debug(true) !!}
<br>
{!! Laralang::trans('Hello world!')->to('es') !!}
<br>
{!! Laralang::trans('Hello world!')->to('ca') !!}
<br>
{!! Laralang::trans('Hello world!')->to('ca')->debug(true) !!}
<br>
{!! Laralang::trans('Hallo welt!')->from('de')->to('fr') !!}
<br>
{!! Laralang::trans('Hello world!')->to('pt') !!}
<br>
{!! Laralang::trans('Hello world!')->to('de') !!}
<br>
{!! Laralang::trans('Hello world!')->to('ml') !!}
<br>
{!! Laralang::trans('Hello world!')->to('zh')->translator('apertium') !!}
<br>
From langs:
@foreach(Laralang::fromLanguages() as $lang)
{{$lang}}
@endforeach
<br>
To langs:
@foreach(Laralang::toLanguages() as $lang)
{{$lang}}
@endforeach
</center>
NOTE: Use {{ }} statements if translations comes from users to prevent XSS atacks
Then the result:
First you should be logged into loaralang
Route prefix can be changed on your config file, but by default it's laralang
You should visit next url:
http://domain/laralang/login
or in localhost you should visit
http://localhost/project-path/public/laralang/login
Then you should see the laralang login page (photo below)
Now you must enter the password you set on Step 5 and then click login to manage your translations as can be seen on photos below!
Laralang also lets you filter translations by from_lang and / or to_lang. Below you have an example:
First we must access to filter view in route http://domain/laralang/translations/filter or well accessing across menu.
Then you can select from which language provides the originals strings and then from which language are translated this string with two selectors:
Then the result:
There exists an api to get translated text. First of all you should enable it on config, then you should configure ajax as explained in laravel official site and then call api (method=POST)
$.post('{{route("laralang::apiTranslate")}}', {'string' : 'This is my string to translate', 'to' : 'de'}, function(response) {
var translatedText = response.translatedText;
}, 'json');
Laralang can generate PHP translations files quickly by translating from english to language you want and save a lot of time!
After login to admin panel, you will find something like this:
Here you simply click on translate PHP files and then you should see this view:
Here you can click translate and all files of resources/lang/en will be translated into languages you choose in the form input!
Then on resources/lang/ you will find a folder for each language with it's respective files translated.
This functions is used to get a translation from a string to another language specified on default values, on app_locale or in every function.
To simplify work we've implemented another package (Localizer) to set app_locale via middleware and it allows to get user browser language to set it as app_locale easily.
from()
It sets the language of the string to translate in a specific translation.
Default: en
to()
It sets the language that you'll output in a specific translation.
Default: app_locale
debug()
Debug option let you to know the reason of an unexpected result with colorful messages in a specific translation.
Default: false
Save()
Save option let you to save a specific translation.
Default: false
Returns an array with all languages from provides strings to translate.
Returns an array with all languages that at least one string has been translated.
Returns an array with ['key' => 'value'] where key is a language and value is an acronym from all languages tested in laralang.