Package Data | |
---|---|
Maintainer Username: | Michele |
Maintainer Contact: | michele.angioni@gmail.com (Michele Angioni) |
Package Create Date: | 2015-01-05 |
Package Last Update: | 2019-09-16 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-23 03:27:37 |
Package Statistics | |
---|---|
Total Downloads: | 6,396 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 11 |
Total Watchers: | 4 |
Total Forks: | 5 |
Total Open Issues: | 1 |
Multi Language is a Laravel 5.1+ 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.
Multi Language can be installed through Composer, just include "michele-angioni/multi-language": "~0.4"
to your composer.json and than run composer update
.
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:
The MicheleAngioni\MultiLanguage\LanguageManager
class is the class that accesses all Multi Language features.
By default it will uses the Laravel file system manager and the Laravel localization feature.
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:
By default the Language Manager uses the Laravel file system manager and the Laravel localization feature.
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.
Suppose we have a users.php
file under the app/lang/en directory
/app
├--/controllers
├--/lang
| └--/en
| └--users.php
which contains
<?php
return array(
"password" => "Passwords must be at least six characters and match the confirmation.",
"user" => "We can't find a user with that e-mail address.",
"token" => "This password reset token is invalid.",
"sent" => "Password reminder sent!",
);
Let's suppose we want to create a Spanish version of the file. We can build a controller handling the language management
<?php
use MicheleAngioni\MultiLanguage\LanguageManager;
class LanguagesController extends \BaseController {
private $languageManager;
function __construct(LanguageManager $languageManager)
{
$this->languageManager = $languageManager;
}
}
and write down some methods to handle the requests.
public function index()
{
$languages = $this->languageManager->getAvailableLanguages(); // ['en']
return View::make('languages')->with('languages', $languages)
}
The above $languages variable would just be a single value array ['en']
since we only have the /en
folder under /lang
.
We now need a list of the English files:
public function showFiles()
{
$files = $this->languageManager->getLanguageFiles(); // ['users']
return View::make('languagesFiles')->with('files', $files)
}
The showFiles() method would just return ['users']
as we have just one file in our /lang/en
folder.
Let's examine the content of the file
// $fileName = 'users';
public function showFile($fileName)
{
$file = $this->languageManager->getLanguageFile($fileName)
return View::make('languagesFile')->with('file', $file)
}
The above method returns an array with the file content.
Let's now create a Spanish version. First of all we must create the /es
folder under the /lang
folder
public function createNewLanguage()
{
// [...] Input validation
$this->languageManager->createNewLanguage($input['locale']);
return Redirect::route('[...]')->with('ok', 'New language successfully created.');
}
We then obviously need a view to submit the Spanish sentences and we leave it up to you. An associative array with key => sentence structure must be sent from the view to the following method
public function saveFile($locale, $fileName)
{
// [...] Input validation
$this->languageManager->setLocale($languageCode);
$this->languageManager->writeLanguageFile($fileName, $input['all']);
return Redirect::route('[...]')->with('ok', 'Lang file successfully saved.');
}
Support follows PSR-1, PSR-2 and PSR-4 PHP coding standards and semantic versioning.
Please report any issue you find in the issues page. Pull requests are welcome.
Support is free software distributed under the terms of the MIT license.
Feel free to contact me.