Download the PHP package 10pearls/laravel-multilingual without Composer

On this page you can find all versions of the php package 10pearls/laravel-multilingual. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-multilingual

Better localization management for Laravel

Introduction

Keeping a project's translations properly updated is cumbersome. Usually translators do not have access to the codebase, and even when they do it's hard to keep track of which translations are missing for each language or when updates to the original text require that translations be revised.

This package allows developers to leverage their database and cache to manage multilanguage sites, while still working on language files during development and benefiting from all the features Laravel's Translation bundle has, like pluralization or replacement.

Table of contents

Laravel compatibility

Laravel translation
4.x 1.0.x
5.0.x 2.0.x
5.1.x 5.3.x 2.1.x
5.4.x 2.2.x
5.5.x 2.3.x and higher

Features overview

Installation

Require through composer

composer require 10pearls/laravel-multilingual 2.3.x

Or manually edit your composer.json file:

"require": {
    "10pearls/laravel-multilingual": "2.3.x"
}

Once installed, in your project's config/app.php file replace the following entry from the providers array:

Illuminate\Translation\TranslationServiceProvider::class

with:

Waavi\Translation\TranslationServiceProvider::class

Remove your config cache:

php artisan config:cache

Publish both the configuration file and the migrations:

php artisan vendor:publish --provider="Waavi\Translation\TranslationServiceProvider"

Execute the database migrations:

php artisan migrate

You may check the package's configuration file at:

config/translator.php

Translations source

This package allows you to load translation from the regular Laravel localization files (in /resources/lang), from the database, from cache or in a mix of the previous for development. You may configure the desired mode of operation through the translator.php config file and/or the TRANSLATION_SOURCE environment variable. Accepted values are:

For cache configuration, please go to cache configuration

Load translations from files

If you do not wish to leverage your database for translations, you may choose to load language lines exclusively through language files. This mode differs from Laravel in that, in case a line is not found in the specified locale, instead of returning the key right away, we first check the default language for an entry. In case you wish to use this mode exclusively, you will need to set the 'available_locales' config file:

config/translator.php
    'available_locales' => ['en', 'es', 'fr'],

Example:

The content in en/validations.php, where 'en' is the default locale, is:

The content in es/validations.php is:

Output for different keys with 'es' locale:

Load translations from the database

You may choose to load translations exclusively from the database. This is very useful if you intend to allow users or administrators to live edit the site's text and translations. In a live production environment, you will usually want this source mode to be activated with the translation's cache. Please see Loading your files into the database for details on the steps required to use this source mode.

Example:

The content in the languages table is:

    | id | locale | name    |
    -------------------------
    | 1  | en     | english |
    | 2  | es     | spanish |

The relevant content in the language_entries table is:

    | id | locale | namespace | group       | item            | text                    |
    -------------------------------------------------------------------------------------
    | 1  | en     | *         | validations | missing.name    | Name is missing         |
    | 2  | en     | *         | validations | missing.surname | Surname is missing      |
    | 3  | en     | *         | validations | min_number      | Number is too small     |
    | 4  | es     | *         | validations | missing.name    | Falta nombre            |
    | 5  | es     | *         | validations | missing.surname | Falta apellido          |

Output for different keys with es locale:

Mixed mode

In mixed mode, both the language files and the database are queried when looking for a group of language lines. Entries found in the filesystem take precedence over the database. This source mode is useful when in development, so that both the filesystem and the user entries are taken into consideration.

Example:

When files and database are set like in the previous examples:

Loading your files into the database

When using either the database or mixed translation sources, you will need to first load your translations into the database. To do so, follow these steps:

When executing the artisan command, the following will happen:

Both vendor files and subdirectories are supported. Please keep in mind that when loading an entry inside a subdirectory, Laravel 5 has changed the syntax to:

Cache translations

Since querying the database everytime a language group must be loaded is grossly inefficient, you may choose to leverage Laravel's cache system. This module will use the same cache configuration as defined by you in app/config/cache.php.

You may enable or disable the cache through the translator.php config file or the 'TRANSLATION_CACHE_ENABLED' environment variable. Config options are:

Env key type description
TRANSLATION_CACHE_ENABLED boolean Enable / disable the translations cache
TRANSLATION_CACHE_TIMEOUT integer Minutes translation items should be kept in the cache.
TRANSLATION_CACHE_SUFFIX string Default is 'translation'. This will be the cache suffix applied to all translation cache entries.

Cache tags

Available since version 2.1.3.8, if the cache store in use allows for tags, the TRANSLATION_CACHE_SUFFIX will be used as the common tag to all cache entries. This is recommended to be able to invalidate only the translation cache, or even just a given locale, namespace and group configuration.

Clearing the cache

Available since version 2.1.3.8, you may clear the translation cache through both an Artisan Command and a Facade. If cache tags are in use, only the translation cache will be cleared. All of your application cache will however be cleared if you cache tags are not available.

Cache flush command:

php artisan translator:flush

In order to access the translation cache, add to your config/app.php files, the following alias:

Once done, you may clear the whole translation cache by calling:

You may also choose to invalidate only a given locale, namespace and group combination.

For example, say we have the following file in our resources/lang directory: en/auth.php, en/auth/login.php and en/vendor/waavi/login.php. To clear the cache entries for each of them you would call:

Managing languages and translations in the Database

The recommended way of managing both languages and translations is through the provided repositories. You may circumvent this by saving changes directly through the Language and Translation models, however validation is no longer executed automatically on model save and could lead to instability and errors.

Both the Language and the Translation repositories provide the following methods:

Method Description
hasTable(); Returns true if the corresponding table exists in the database, false otherwise
all($related = [], $perPage = 0); Retrieve all records from the DB. A paginated record will be return if the second argument is > 0, with $perPage items returned per page
find($id); Find a record by id
create($attributes); Validates the given attributes and inserts a new record. Returns false if validation errors occured
delete($id); Delete a record by id
restore($id); Restore a record by id
count(); Return the total number of entries
validate(array $attributes); Checks if the given attributes are valid
validationErrors(); Get validation errors for create and update methods

Managing Languages

Language management should be done through the \Waavi\Translation\Repositories\LanguageRepository to ensure proper data validation before inserts and updates. It is recommended that you instantiate this class through Dependency Injection.

A valid Language record requires both its name and locale to be unique. It is recommended you use the native name for each language (Ex: English, Español, Français)

The provided methods are:

Method Description
update(array $attributes); Updates a Language entry [id, name, locale]
trashed($related = [], $perPage = 0); Retrieve all trashed records from the DB.
findTrashed($id, $related = []); Find a trashed record by id
findByLocale($locale); Find a record by locale
findTrashedByLocale($locale); Finds a trashed record by locale
allExcept($locale); Returns a list of all languages excluding the given locale
availableLocales(); Returns a list of all available locales
isValidLocale($locale); Checks if a language exists with the given locale
percentTranslated($locale); Returns the percent translated for the given locale

Managing Translations

Translation management should be done through the \Waavi\Translation\Repositories\TranslationRepository to ensure proper data validation before inserts and updates. It is recommended that you instantiate this class through Dependency Injection.

A valid translation entry cannot have the same locale and language code than another.

The provided methods are:

Method Description
update($id, $text); Update an unlocked entry
updateAndLock($id, $text); Update and lock an entry (locked or not)
allByLocale($locale, $perPage = 0); Get all by locale
untranslated($locale, $perPage = 0, $text = null); Get all untranslated entries. If $text is set, entries will be filtered by partial matches to translation value.
pendingReview($locale, $perPage = 0); List all entries pending review
search($locale, $term, $perPage = 0); Search by all entries by locale and a partial match to both the text value and the translation code.
randomUntranslated($locale); Get a random untranslated entry
translateText($text, $textLocale, $targetLocale); Translate text to another locale
flagAsReviewed($id); Flag entry as reviewed

Things to consider:

Model attributes translation

You can also use the translation management system to manage your model attributes translations. To do this, you only need to:

Example:

Uri localization

You may use Waavi\Translation\Middleware\TranslationMiddleware to make sure all of your urls are properly localized. The TranslationMiddleware will only redirect GET requests that do not have a locale in them.

For example, if a user visits the url /home, the following would happen:

You may choose to activate this Middleware globally by adding the middleware to your App\Http\Kernel file:

Or to apply it selectively through the 'localize' route middleware, which is already registered when installing the package through the ServiceProvider.

It is recommended you add the following alias to your config/app.php aliases:

Every localized route must be prefixed with the current locale:

Starting on v2.1.6, you may also specify a custom position for the locale segment in your url. For example, if the locale info is the third segment in a URL (/api/v1/es/my_resource), you may use:

In your views, for routes where the Middleware is active, you may present the user with a menu to switch from the current language to another by using the shared variables. For example:


All versions of laravel-multilingual with dependencies

PHP Build Version
Package Version
Requires laravel/framework Version ~5.5
doctrine/dbal Version ^2.5
10pearls/laravel-multilingual Version dev-master
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package 10pearls/laravel-multilingual contains the following files

Loading the files please wait ....