Download the PHP package mcamara/laravel-localization without Composer

On this page you can find all versions of the php package mcamara/laravel-localization. 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-localization

Laravel Localization

Join the chat at https://gitter.im/mcamara/laravel-localization

Latest Stable Version Total Downloads Build Status Open Source Helpers Reviewed by Hound

Easy i18n localization for Laravel, an useful tool to combine with Laravel localization classes.

The package offers the following:

Table of Contents

Laravel compatibility

Laravel laravel-localization
4.0.x 0.13.x
4.1.x 0.13.x
4.2.x 0.15.x
5.0.x/5.1.x 1.0.x
5.2.x-5.4.x (PHP 7 not required) 1.2.
5.2.0-6.x (PHP version >= 7 required) 1.4.x
6.x-10.x (PHP version >= 7 required) 1.8.x
10.x-11.x (PHP version >= 8.2 required) 2.0.x

Installation

Install the package via composer: composer require mcamara/laravel-localization

For Laravel 5.4 and below it necessary to register the service provider.

Config Files

In order to edit the default configuration you may execute:

After that, config/laravellocalization.php will be created.

The configuration options are:

Register Middleware

You may register the package middleware in the app/Http/Kernel.php file:

Usage

Add the following to your routes file:

Once this route group is added to the routes file, a user can access all locales added into supportedLocales (en and es by default). For example, the above route file creates the following addresses:

The package sets your application locale App::getLocale() according to your url. The locale may then be used for Laravel's localization features.

You may add middleware to your group like this:

Recommendations

1.: It is strongly recommended to use a redirecting middleware. Urls without locale should only be used to determine browser/default locale and to redirect to the localized url. Otherwise, when search engine robots crawl for example http://url-to-laravel/test they may get different language content for each visit. Also having multiple urls for the same content creates a SEO duplicate-content issue.

2.: It is strongly recommended to localize your links, even if you use a redirect middleware. Otherwise, you will cause at least one redirect each time a user clicks on a link. Also, any action url from a post form must be localized, to prevent that it gets redirected to a get request.

Redirect Middleware

The following redirection middleware depends on the settings of hideDefaultLocaleInURL and useAcceptLanguageHeader in config/laravellocalization.php:

LocaleSessionRedirect

Whenever a locale is present in the url, it will be stored in the session by this middleware.

If there is no locale present in the url, then this middleware will check the following

For example, if a user navigates to http://url-to-laravel/test and en is the current locale, it would redirect him automatically to http://url-to-laravel/en/test.

LocaleCookieRedirect

Similar to LocaleSessionRedirect, but it stores value in a cookie instead of a session.

Whenever a locale is present in the url, it will be stored in the cookie by this middleware.

In there is no locale present in the url, then this middleware will check the following

For example, if a user navigates to http://url-to-laravel/test and de is the current locale, it would redirect him automatically to http://url-to-laravel/de/test.

LaravelLocalizationRedirectFilter

When the default locale is present in the url and hideDefaultLocaleInURL is set to true, then the middleware redirects to the url without locale.

For example, if es is the default locale, then http://url-to-laravel/es/test would be redirected to http://url-to-laravel/test and theApp::getLocale() would be set to es.

Helpers

This package comes with a bunch of helpers.

Localized URLs

Localized URLS taken into account route model binding when generating the localized route, aswell as the hideDefaultLocaleInURL and Translated Routes settings.

Get localized URL

Get localized URL for an specific locale

Get current URL in specific locale:

Get Clean routes

Returns a URL clean of any localization.

Get URL for an specific translation key

Returns a route, localized to the desired locale. If the translation key does not exist in the locale given, this function will return false.

Example of a localized link using routes with attributes

Get Supported Locales

Return all supported locales and their properties as an array.

Get Supported Locales Custom Order

Return all supported locales but in the order specified in the configuration file. You can use this function to print locales in the language selector.

Get Supported Locales Keys

Return an array with all the keys for the supported locales.

Get Current Locale

Return the key of the current locale.

Get Current Locale Name

Return current locale's name as string (English/Spanish/Arabic/ ..etc).

Get Current Locale Native Name

Return current locale's native name as string (English/Español/عربى/ ..etc).

Get Current Locale Regional Name

Return current locale's regional name as string (en_GB/en_US/fr_FR/ ..etc).

Get Current Locale Direction

Return current locale's direction as string (ltr/rtl).

Get Current Locale Script

Return the ISO 15924 code for the current locale script as a string; "Latn", "Cyrl", "Arab", etc.

Set view-base-path to current locale

Register the middleware LaravelLocalizationViewPath to set current locale as view-base-path.

Now you can wrap your views in language-based folders like the translation files.

resources/views/en/, resources/views/fr, ...

Map your own custom lang url segments

As you can modify the supportedLocales even by renaming their keys, it is possible to use the string instead of to provide custom lang url segments. Of course, you need to prevent any collisions with already existing keys and should stick to the convention as long as possible. But if you are using such a custom key, you have to store your mapping to the array. This is needed to enable the LanguageNegotiator to correctly assign the desired locales based on HTTP Accept Language Header. Here is a quick example how to map HTTP Accept Language Header 'en-GB' to url segment 'uk':

After that becomes .

Creating a language selector

If you're supporting multiple locales in your project you will probably want to provide the users with a way to change language. Below is a simple example of blade template code you can use to create your own language selector.

Here default language will be forced in getLocalizedURL() to be present in the URL even hideDefaultLocaleInURL = true.

Note that Route Model Binding is supported.

Translated Routes

You may translate your routes. For example, http://url/en/about and http://url/es/acerca (acerca is about in spanish) or http://url/en/article/important-article and http://url/es/articulo/important-article (article is articulo in spanish) would be redirected to the same controller/view as follows:

It is necessary that at least the localize middleware in loaded in your Route::group middleware (See installation instruction).

For each language, add a routes.php into resources/lang/**/routes.php folder. The file contains an array with all translatable routes. For example, like this:

You may add the routes in routes/web.php like this:

Once files are saved, you can access http://url/en/about , http://url/es/acerca , http://url/en/article/important-article and http://url/es/articulo/important-article without any problem.

Translatable route parameters

Maybe you noticed in the previous example the English slug in the Spanish url:

http://url/es/articulo/important-article

It is possible to have translated slugs, for example like this:

http://url/en/article/important-change
http://url/es/articulo/cambio-importante

However, in order to do this, each article must have many slugs (one for each locale). Its up to you how you want to implement this relation. The only requirement for translatable route parameters is, that the relevant model implements the interface LocalizedUrlRoutable.

Implementing LocalizedUrlRoutable

To implement \Mcamara\LaravelLocalization\Interfaces\LocalizedUrlRoutable, one has to create the function getLocalizedRouteKey($locale), which must return for a given locale the translated slug. In the above example, inside the model article, getLocalizedRouteKey('en') should return important-change and getLocalizedRouteKey('es') should return cambio-importante.

Route Model Binding

To use route-model-binding, one should overwrite the function resolveRouteBinding($slug) in the model. The function should return the model that belongs to the translated slug $slug. For example:

Tutorial Video

You may want to checkout this video which demonstrates how one may set up translatable route parameters.

Events

You can capture the URL parameters during translation if you wish to translate them too. To do so, just create an event listener for the routes.translation event like so:

Be sure to pass the locale and the attributes as parameters to the closure. You may also use Event Subscribers, see: http://laravel.com/docs/events#event-subscribers

Caching routes

To cache your routes, use:

... instead of the normal route:cache command. Using artisan route:cache will not work correctly!

For the route caching solution to work, it is required to make a minor adjustment to your application route provision.

In your App's RouteServiceProvider, use the LoadsTranslatedCachedRoutes trait:

For more details see here.

Common Issues

POST is not working

This may happen if you do not localize your action route that is inside your Routes::group. This may cause a redirect, which then changes the post request into a get request. To prevent that, simply use the localize helper.

For example, if you use Auth::routes() and put them into your Route::group Then

will not work. Instead, one has to use

Another way to solve this is to put http method to config to 'laravellocalization.httpMethodsIgnored' to prevent of processing this type of requests

MethodNotAllowedHttpException

If you do not localize your post url and use a redirect middleware, then the post request gets redirected as a get request. If you have not defined such a get route, you will cause this exception.

To localize your post url see the example in POST is not working.

Validation message is only in default locale

This also happens if you did not localize your post url. If you don't localize your post url, the default locale is set while validating, and when returning to back() it shows the validation message in default locale.

To localize your post url see the example in POST is not working.

Testing

During the test setup, the called route is not yet known. This means no language can be set. When a request is made during a test, this results in a 404 - without the prefix set the localized route does not seem to exist.

To fix this, you can use this function to manually set the language prefix:

Collaborators

Ask mcamara if you want to be one of them!

Changelog

View changelog here -> changelog

License

Laravel Localization is an open-sourced laravel package licensed under the MIT license


All versions of laravel-localization with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1.0
laravel/framework Version ~5.2.0||~5.3.0||~5.4.0||~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0||^7.0||^8.0||^9.0|^10.0||^11.0
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 mcamara/laravel-localization contains the following files

Loading the files please wait ....