PHPackages                             michele-angioni/multi-language - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. michele-angioni/multi-language

ActiveLibrary[Localization &amp; i18n](/categories/localization)

michele-angioni/multi-language
==============================

A Laravel package to handle multi language localization.

v0.4(9y ago)116.5k5[1 PRs](https://github.com/micheleangioni/multi-language/pulls)1MITPHPPHP &gt;=7.0.0

Since Feb 4Pushed 6y ago2 watchersCompare

[ Source](https://github.com/micheleangioni/multi-language)[ Packagist](https://packagist.org/packages/michele-angioni/multi-language)[ RSS](/packages/michele-angioni-multi-language/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (6)Used By (1)

MULTI LANGUAGE
==============

[](#multi-language)

[![Build Status](https://camo.githubusercontent.com/c63b33051fdaa63721601cab96ac1924ad5c8f1ffedb0b7987c76ba1fbdd5814/68747470733a2f2f7472617669732d63692e6f72672f6d696368656c65616e67696f6e692f6d756c74692d6c616e67756167652e737667)](https://travis-ci.org/micheleangioni/multi-language)[![License](https://camo.githubusercontent.com/e566a4c916ac21126514deb8ac76efa51978e6ceed527b9e58c62abbd9d3bbf8/68747470733a2f2f706f7365722e707567782e6f72672f6d696368656c652d616e67696f6e692f6d756c74692d6c616e67756167652f6c6963656e73652e737667)](https://packagist.org/packages/michele-angioni/multi-language)[![Build Status](https://camo.githubusercontent.com/c63b33051fdaa63721601cab96ac1924ad5c8f1ffedb0b7987c76ba1fbdd5814/68747470733a2f2f7472617669732d63692e6f72672f6d696368656c65616e67696f6e692f6d756c74692d6c616e67756167652e737667)](https://travis-ci.org/micheleangioni/multi-language)[![SensioLabsInsight](https://camo.githubusercontent.com/f646bff4153048809775ed2b84937cb9d0a98cbb18bcef2882992c0d2f384eef/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f35373661383339652d376338342d343631352d383934612d3436356633306338663838312f736d616c6c2e706e67)](https://insight.sensiolabs.com/projects/576a839e-7c84-4615-894a-465f30c8f881)

Introduction
------------

[](#introduction)

Multi Language is a [Laravel 5.1+](http://laravel.com) package which handles localization. It acts as a wrapper for Laravel localization and lets ease translations of your default lang files into new languages.

In case of incompatibilities or errors with Laravel 5.1 - 5.3, or for PHP 5.6, please use [version 0.3](https://github.com/micheleangioni/multi-language/tree/0.3).

Installation
------------

[](#installation)

Multi Language can be installed through Composer, just include `"michele-angioni/multi-language": "~0.4"` to your composer.json and than run `composer update`.

Configuration
-------------

[](#configuration)

Add the following service providers under the providers array in your app.php configuration file

```
MicheleAngioni\MultiLanguage\MultiLanguageServiceProvider:class,
MicheleAngioni\MultiLanguage\MultiLanguageBindServiceProvider:class
```

Multi Language can be highly customized by publishing the configuration file through the artisan command artisan command `php artisan vendor:publish`.
It will create the `ma_multilanguage.php` file in your config directory. You can than edit the config.php file in your config directory to customize the Multi Language behaviour:

- allowed\_languages : it is the number of allowed languages. It can be used to prevent the creation of new supported languages
- allowed\_nested\_arrays : maximum number of nested arrays allowed in lang files
- language\_files\_path : path to the language files
- max\_text\_length : max text length allowed for the languages keys
- safe\_mode : enables / disables safe mode. If safe mode is enabled, only files with the same name of files of default locale can be created. Furthermore, only keys already present in the default locale can be added to new languages.

Usage
-----

[](#usage)

The `MicheleAngioni\MultiLanguage\LanguageManager` class is the class that accesses all Multi Language features. By default it will uses the [Laravel file system manager](http://laravel.com/api/5.4/Illuminate/Filesystem/Filesystem.html) and the [Laravel localization feature](http://laravel.com/docs/5.4/localization).

You can inject it in the constructor of the one of your classes or directly instance it by using the Laravel Application facade `App::make('MicheleAngioni\MultiLanguage\LanguageManager')` and use its methods:

- getLanguagesPath() : get current path to languages files
- setLanguagesPath($path) : set the path to languages files
- setMaxTextLength($value) : set dynamically the max allowed length text
- getDefaultLocale() : get the default locale language. It will be used when a language key is not found in the selected language
- getLocale() : get the current used locale language
- setLocale($locale) : set the current used locale language
- setSafeMode($bool) : change dynamically the Safe Mode setting
- getAvailableLanguages() : get a list of the available languages, that is. the languages which have a directory under the lang directory
- getLanguageFiles($useDefaultLocale = false) : get a list of language files of input language
- getLanguageFile($fileName, $useDefaultLocale = false) : return all keys contained in the input file (without extension)
- getLanguageFileKey($fileName, $key) : return the value of input key in the set locale input file (without extension)
- createNewLanguage($newLocale) : create a new directory under the languages directory for input locale
- writeLanguageFile($fileName, array $inputs) : write the input array on a php file under the /lang/locale directory with input name
- buildArray(array $inputs) : take an array with dot notation and build the final array that will be written into the file
- convertArrayToDotNotation(array $array) : convert an array containing nested arrays to an array with dot notation

(optional) Custom File System and Translator
--------------------------------------------

[](#optional-custom-file-system-and-translator)

By default the Language Manager uses the [Laravel file system manager](http://laravel.com/api/5.4/Illuminate/Filesystem/Filesystem.html) and the [Laravel localization feature](http://laravel.com/docs/5.4/localization). You can override that by defining your own file system (which has to implement the `MicheleAngioni\MultiLanguage\FileSystemInterface`) and translator (which has to implement the `MicheleAngioni\MultiLanguage\TranslatorInterface`) The two new files can be injected in the Language Manager constructor by commenint the 'MicheleAngioni\\MultiLanguage\\LanguageManagerBindServiceProvider' line in the app.php conf file and defining your custom binding in a new service provider.

Example
-------

[](#example)

Suppose we have a `users.php` file under the app/lang/en directory

```
/app
├--/controllers
├--/lang
|     └--/en
|          └--users.php
```

which contains

```
