├── .gitignore ├── .travis.yml ├── src ├── Services │ ├── ResourcesRepository.php │ └── GithubResourcesRepository.php ├── LanguageInstallerServiceProvider.php └── Console │ └── Commands │ └── LanguageInstaller.php ├── phpunit.xml ├── composer.json ├── README.md └── config └── lang-installer.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.5 5 | - 5.6 6 | - hhvm 7 | 8 | before_script: 9 | - travis_retry composer self-update 10 | - travis_retry composer install --prefer-source --no-interaction 11 | 12 | script: phpunit --coverage-text 13 | 14 | notifications: 15 | email: 16 | - "adrian@anavallasuiza.com" 17 | - "carlos@anavallasuiza.com" 18 | -------------------------------------------------------------------------------- /src/Services/ResourcesRepository.php: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ablunier/laravel-lang-installer", 3 | "description": "Command for easily add languages to a Laravel project", 4 | "keywords": [ 5 | "laravel", 6 | "translation", 7 | "languages" 8 | ], 9 | "authors": [ 10 | { 11 | "name": "Adrian P. Blunier", 12 | "email": "adrian.blunier@gmail.com" 13 | } 14 | ], 15 | "require": { 16 | "php": ">=5.5.9", 17 | "illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*" 18 | }, 19 | "autoload": { 20 | "psr-4": { 21 | "Ablunier\\Laravel\\Translation\\": "src/" 22 | } 23 | }, 24 | "minimum-stability": "stable", 25 | "extra": { 26 | "laravel": { 27 | "providers": [ 28 | "Ablunier\\Laravel\\Translation\\LanguageInstallerServiceProvider" 29 | ] 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel languages installer 2 | 3 | Install translation files for any language with a command in your laravel projects. The language files will be downloaded from the awesome [caouecs languages repository](https://github.com/caouecs/Laravel-lang). 4 | 5 | ### Requirements 6 | * PHP 5.5 or higher. 7 | * Laravel >= 5.1 8 | 9 | ## Installation 10 | 11 | Add the package to your composer.json: 12 | 13 | ``` 14 | composer require ablunier/laravel-lang-installer 15 | ``` 16 | 17 | Add the service provider in app.php: 18 | 19 | ``` 20 | Ablunier\Laravel\Translation\LanguageInstallerServiceProvider::class, 21 | ``` 22 | 23 | Publish the package's assets: 24 | 25 | ``` 26 | php artisan vendor:publish 27 | ``` 28 | 29 | ## Configuration 30 | 31 | Edit the 'lang-installer.php' file in your config folder to add your required languages and files. 32 | 33 | ## Usage 34 | 35 | Execute the command: 36 | 37 | ``` 38 | php artisan lang:install 39 | ``` 40 | -------------------------------------------------------------------------------- /config/lang-installer.php: -------------------------------------------------------------------------------- 1 | 'https://raw.githubusercontent.com/caouecs/Laravel-lang', 11 | 12 | /* 13 | |-------------------------------------------------------------------------- 14 | | Latest laravel translations available version 15 | |-------------------------------------------------------------------------- 16 | | 17 | */ 18 | 'latest_version' => '5', 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | Laravel versions => repository branch equivalence 23 | |-------------------------------------------------------------------------- 24 | | 25 | */ 26 | 'versions' => [ 27 | '4' => 'laravel4', 28 | '5' => 'master' 29 | ], 30 | 31 | /* 32 | |-------------------------------------------------------------------------- 33 | | Languages to install 34 | |-------------------------------------------------------------------------- 35 | | 36 | */ 37 | 'languages' => [ 38 | 39 | ], 40 | 41 | /* 42 | |-------------------------------------------------------------------------- 43 | | Translation files to install 44 | |-------------------------------------------------------------------------- 45 | | 46 | */ 47 | 'files' => [ 48 | 'auth', 49 | 'pagination', 50 | 'passwords', 51 | 'validation' 52 | ] 53 | ]; 54 | -------------------------------------------------------------------------------- /src/LanguageInstallerServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 20 | __DIR__.'/../config/lang-installer.php' => config_path('lang-installer.php'), 21 | ], 'config'); 22 | } 23 | 24 | /** 25 | * Register the service provider. 26 | * 27 | * @return void 28 | */ 29 | public function register() 30 | { 31 | $this->mergeConfigFrom(__DIR__.'/../config/lang-installer.php', 'lang-installer'); 32 | 33 | $this->app->bind('Ablunier\Laravel\Translation\Services\ResourcesRepository', function ($app) { 34 | return new GithubResourcesRepository; 35 | }); 36 | 37 | $this->registerCommands(); 38 | } 39 | 40 | protected function registerCommands() 41 | { 42 | $this->app->singleton('command.language.installer', function ($app) { 43 | $resourcesRepository = $app->make('Ablunier\Laravel\Translation\Services\ResourcesRepository'); 44 | 45 | return new LanguageInstaller($resourcesRepository); 46 | }); 47 | 48 | $this->commands('command.language.installer'); 49 | } 50 | 51 | /** 52 | * Get the services provided by the provider. 53 | * 54 | * @return array 55 | */ 56 | public function provides() 57 | { 58 | return [ 59 | 'command.language.installer' 60 | ]; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Services/GithubResourcesRepository.php: -------------------------------------------------------------------------------- 1 | makeRequest($version, $lang, $file); 15 | } 16 | 17 | /** 18 | * @return string 19 | */ 20 | public function getLatestVersion() 21 | { 22 | return config('lang-installer.latest_version'); 23 | } 24 | 25 | /** 26 | * @return array 27 | */ 28 | public function getVersions() 29 | { 30 | return config('lang-installer.versions'); 31 | } 32 | 33 | /** 34 | * @param $version 35 | * @param $lang 36 | * @param $file 37 | * 38 | * @return string 39 | */ 40 | protected function getUrl($version, $lang, $file) 41 | { 42 | return $this->getLocation() . '/' . $version . '/src/' . $lang . '/' . $file . '.php'; 43 | } 44 | 45 | /** 46 | * @param $version 47 | * @param $package 48 | * @param $page 49 | * 50 | * @return string 51 | */ 52 | protected function makeRequest($version, $lang, $file) 53 | { 54 | $url = $this->getUrl($version, $lang, $file); 55 | 56 | return (string) @file_get_contents($url); 57 | } 58 | 59 | /** 60 | * Prepend http:// to a string if the string does not already contain it 61 | * 62 | * @param $url 63 | * @return string 64 | */ 65 | protected function addHttp($url) 66 | { 67 | if (! preg_match("~^(?:f|ht)tps?://~i", $url)) { 68 | $url = "http://" . $url; 69 | } 70 | return $url; 71 | } 72 | 73 | /** 74 | * @return mixed 75 | */ 76 | protected function getLocation() 77 | { 78 | return $this->addHttp(config('lang-installer.location')); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/Console/Commands/LanguageInstaller.php: -------------------------------------------------------------------------------- 1 | resourcesRepository = $resourcesRepository; 33 | } 34 | 35 | /** 36 | * Execute the console command. 37 | * 38 | * @return mixed 39 | */ 40 | public function handle() 41 | { 42 | $versions = $this->resourcesRepository->getVersions(); 43 | $lang = $this->argument('lang'); 44 | 45 | if ($lang=="") { 46 | foreach (config('lang-installer.languages') as $lang) { 47 | $this->installLang($lang, $versions); 48 | } 49 | 50 | $this->info('All language files installed correctly.'.PHP_EOL); 51 | } 52 | else { 53 | $this->installLang($lang, $versions); 54 | 55 | $this->info(sprintf('Language %s files installed correctly.', $lang).PHP_EOL); 56 | } 57 | } 58 | 59 | protected function installLang($lang, $versions) 60 | { 61 | $this->info(sprintf('Retrieving files for [%s] language...', $lang).PHP_EOL); 62 | 63 | $langPath = $this->getLangResourcesPath().DIRECTORY_SEPARATOR.$lang; 64 | 65 | if (! is_dir($langPath)) { 66 | mkdir($langPath, 0755, true); 67 | } 68 | 69 | foreach (config('lang-installer.files') as $file) { 70 | $this->info(sprintf('Retrieving content for [%s] translations file...', $file).PHP_EOL); 71 | 72 | $fileContent = $this->resourcesRepository->findForVersion($versions[$this->resourcesRepository->getLatestVersion()], $lang, $file); 73 | 74 | if (empty($fileContent)) { 75 | continue; 76 | } 77 | 78 | $filename = $langPath.DIRECTORY_SEPARATOR.$file.'.php'; 79 | 80 | if (! file_exists($filename)) { 81 | file_put_contents($filename, $fileContent); 82 | } 83 | } 84 | } 85 | 86 | protected function getLangResourcesPath() 87 | { 88 | return app()->basePath().DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'lang'; 89 | } 90 | } 91 | --------------------------------------------------------------------------------