Download the PHP package aewebsolutions/laravel-translator without Composer
On this page you can find all versions of the php package aewebsolutions/laravel-translator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aewebsolutions/laravel-translator
More information about aewebsolutions/laravel-translator
Files in aewebsolutions/laravel-translator
Package laravel-translator
Short Description Laravel's package for managing multiple locales and translation
License MIT
Homepage https://github.com/aewebsolutions/laravel-translator/
Informations about the package laravel-translator
Laravel Translator
Laravel Translator is the most complete and easiest to use Laravel manager for dealing with multiple locales and translations.
Extending Laravel's Router and URLGenerator allows us to deal with multiple locales in Laravel's way. And the Translator Repository allows us to deal with translations for those locales.
It is worth to say that, if we want, we can reserve a non-prefixed URL for our application's main locale. Thus, from all supported locales, we would have a clearer URL only for our main locale (www.site.com/apple) and prefixed ones for the rest of them (e.g., www.site.com/fr/apple).
To know more about the developer, visit www.aesolucionesweb.com.ar
Code Examples
Compatibility
Laravel 5.*
Features
- Routes and urls manager.
- Complete repository for translate your whole application.
- Eloquent extension for translate attributes inside your models.
- Schema support.
- Cache optimization.
Installation
Adding repository files to your proyect.
The best way to install Laravel Translator is with Composer. To install the most recent version, run the following command.
Integration
Then, you must add two lines in config/app.php
. First, add a new provider to $providers
array:
Second, for a proper access to TranslatorRespository, you must add a new facade to $aliases
array:
Optionally, if you wish to use our Schema support for an easily translatable tables creation, you need to replace Schema facade. So, comment or remove Laravel's and add the new one:
Publishing
Some files need be copied from vendor directory. Just run the following command:
This command will publish next files:
app\Translation.php
: an Eloquent model to deal with translations table,database\migrations\2016_01_01_000000_create_translations_table.php
: a migration file that will create translations table,config/translation.php
: a configuration file.
Migration
A translations table must be created in database. Running Artisan's migrate
command would be enough. But if you need to add extra columns to translations table,
you may do it before running command. Just add them to Schema::up in migration file.
Also, you may need to add them too to App\Translation::$fillable
array property.
Then, run:
Extending Router
Add a new line to App\Http\Kernel
in order to extend Laravel's Router.
Configuration
Configuration settings can be found in config/translator.php
with plenty information.For basic usage, you must add all locales supported by application in $locales_available
array. E.g., if application supports en, fr and es locales, array will look like this:
Main locale takes its value from application's default locale (see locale
in config/app.php
file). So, do not forget to set it correctly.
Usage
Routing
Basics
This router is an extension of Laravel's, thus you will find original features exactly like you know them.
All working routes must have at least one locale available.
You can associate, not just a single locale, but a group of them too. Also, if you need a route to be available for the whole group of supported locales, you can use the ´all´ keyword.
Taking defaults settings, next URIs will be available in application for routes above.
- /apple
- /es/apple
- /peach
- /es/peach
- /fr/peach
Requesting any of them, current locale will be set automatically.
It is worth to say that URIs are generated dynamically in order to optimize. Router does not have to lead with multiplied rules, but just with those that match with requested locale. Imagine 40 route rules and 10 languages; instead of an unnecessary route rule multiplication, you will have exactly what you need: 40 rules.
Groups
You can assign locales to a group, not just to a single route.
URL
To get a relative or absolute URL from a route name for the current locale, as usually, call either Laravel's route
or URL::route
methods:
If you ask for another locale, use dot notation. For getting a fallback locale's route, pass a locale (or set true) as the four argument.
Also, you can get all URLs for all supported locales. Call either Route::routes
or routes
new methods:
Laravel's URL::current
has been modified. Now, you can pass as an optional first argument a locale.
Translator Repository
Laravel Translator helps you to translate your application easily, dealing with translations from database.
Translations could be managed directly by App\Translation
Eloquent model. But, you should use the provided repository in order to guarantee stability. Translator
facade, probably, is all you need.
Getting
You can get a translated text using Translator::text
method or, better, the tt
helper. This works like Laravel's trans
. The tt
method accepts a locale (optionally), a group name and a needle as its first argument, using dot notation: locale.group.needle. Let's assume that current locale is 'en':
Sometimes a text may have no translation for a locale available; so, main locale is shown. Turning false
its third argument you can avoid this behavior.
As trans
do, you can make replacements:
Pluralization. Translator::choice
works like Laravel's trans_choice
(see Laravel Documentation), but with further arguments.
Last, If you need to get all texts from a group.needle, use Transalor::texts
Creating
You can create a text for an specific locale:
Or create multiple locales at the same time:
Also, you can add extra attributes. Of corse, extra columns attributes should have been added to translations table and should have been included in App\Translation::$fillable
array property.
Updating
Updating a text
Update a text for a specific locale or a group of them, with or without extra attributes:
Updating a text‘s group name or needle
Groups and needle are sensitive attributes, this is, they cannot be updated lightly without making a mess. In short, there cannot be duplicates for a locale.group.needle. So, even if you try, Translator::update
method will not allow you to change this attributes. Instead, you must use Translator::updateGroupNeedle
.
Deleting
Deleting is also easy with provided respository:
Translatable Models
Laravel Translator includes, not only Translator
repository, but also an Eloquent extension to manage multiple languajes directly inside your Models.
Suppose you need an Article
model. Would it be truly helpful if you were able to get properties like this:
Creating a table
Translatable columns have a clear syntaxis: column_name_locale. In order to simplify creation, optionally you can use our Schema extension (rememeber include facade. See #Installation).
All you have to do is call $table->localize(['column_name'])
to multiply column_name
for each locale available in application. Also, you can pass an array of locales
as a second argument.
The Model
Two things must be done: to extend your model to Translator\Eloquent\Model
and to fill $translatable
protected property.
Now, you can get a translatable property like this:
To update, insert or fill a row for current locale, you are able to do it like you usually do. But if you need to manage several locales at once, you can do it with an array, just like this:
Cache
In order to reduce database queries, groups should be stored in cache. Just look inside conf/translator.php
and make sure that $cache
is set TRUE
. Laravel Translation uses your application's cache settings.
If you are using (as you should) provided repository to create, update or delete translations, then cache does not need to be flushed manually. Just be sure that cache_auto_flush
is set TRUE
and cache will be automatically flushed only for compromised groups each time Translator's created
, updated
or deleted
events are fired.
Methods
TranslatorRespository (Facade: Translator)
Return | Method |
---|---|
string | text($localeGroupNeedle, $replacements = [], $orDefault = true) Get a text for a locale.group.needle. |
object | texts($groupNeedle, $replacements = []) Get all texts for a group.needle. |
string | choice($localeGroupNeedle, $count = 1, $replacements = [], $orDefault = true) Choice between two or more string options. |
Collection | getGroup($name) Get a Collection from a group name. |
Collection | getLocale($locale = NULL, $group = NULL) Get a Collection from a locale and, optionally, a group name. |
void | cacheFlush($group = NULL) Remove a group or all groups from cache. |
Collection | get($localeGroupNeedle = NULL) Get a Collection from a locale (optionally), a group name and a needle. By default, get current locale. |
void | delete($localeGroupNeedle) Delete a whole group or a group.needle for a specific locale or for all locales. |
void | create($localeGroupNeedle, $texts, array $extra = []) Insert a text for an specific locale or for all locales at once. |
void | update($localeGroupNeedle, $texts, array $extra = []) Edit a text for an specific locale or for all locales at once. |
void | updateGroupNeedle($groupNeedle, $newGroupNeedle) Edit the whole group name, edit the needle but not the group or edit a single group.needle row: |
void | created(Closure $callback) Register a listener for created event. Callback's params: array $locales , $group , $needle . |
void | updated(Closure $callback) Register a listener for updated event. Callback's params: array $locales , $group , $needle . |
void | deleted(Closure $callback) Register a listener for deleted event. Callback's params: array $locales , $group , $needle . |
TranslatorURL (Facade: URL)
TranslatorURL extends URLGenerator.
Return | Method |
---|---|
string | route($name, $parameters = [], $absolute = true, $default = NULL) Get the URL to a named route and locale (current locale, by default). Use dot notation. |
object | routes($name, $parameters = [], $absolute = true) Get an object with all URLs for all locales to a named route. |
string | current($locale = NULL, $absolute = true) Get the current URL for current locale or for another supported locale. |
array | currentAll($absolute = true) Get all current URLs for all locales available. |
bool | hasRouteLocale($route, $locale) Verify if route has a locale. |
bool | hasLocale($routename, $locale) Verify if a route' name has a locale. |
string | localize($uri, $locale, $absolute = true, $uriHasPrefix = true) Generate an absolute or relative URL for a given URI and a locale. |
License
Laravel Translator is licensed under the MIT License.
Copyright 2016 Ángel Espro