├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── public └── .gitkeep ├── src ├── UFirst │ └── LangImportExport │ │ ├── Console │ │ ├── ExportToCsvCommand.php │ │ └── ImportFromCsvCommand.php │ │ ├── Facades │ │ └── LangListService.php │ │ ├── LangImportExportServiceProvider.php │ │ └── LangListService.php ├── config │ └── .gitkeep ├── controllers │ └── .gitkeep ├── lang │ └── .gitkeep ├── migrations │ └── .gitkeep └── views │ └── .gitkeep └── tests └── .gitkeep /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | /vendor 3 | composer.phar 4 | composer.lock 5 | .DS_Store 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | - 5.6 8 | - hhvm 9 | 10 | before_script: 11 | - composer self-update 12 | - composer install --prefer-source --no-interaction --dev 13 | 14 | script: phpunit 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 UFirst Group 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | laravel-lang-import-export 2 | ========================== 3 | 4 | This package provides artisan commands to import and export language files from and to CSV. This can be used to send translations to agencies that normally work with Excel-like files. 5 | 6 | It turns some navigation.php file... 7 | 8 | ```php 9 | [ 13 | 'next' => 'Next', 14 | 'prev' => 'Previous', 15 | 'play' => 'Play', 16 | ] 17 | 'tips' => [ 18 | 'next' => 'Navigate to the next item', 19 | 'prev' => 'Navigate to the previous item', 20 | 'play' => 'Autoplay the slide show', 21 | ] 22 | ]; 23 | ``` 24 | ...to the following CSV... 25 | 26 | ```CSV 27 | key,en 28 | navigation.commands.next,Next 29 | navigation.commands.prev,Previous 30 | navigation.commands.play,Play 31 | navigation.tips.next,"Navigate to the next item" 32 | navigation.tips.prev,"Navigate to the previous item" 33 | navigation.tips.play,"Autoplay the slide show" 34 | 35 | ``` 36 | ...and vice versa. 37 | 38 | Installation 39 | ------------ 40 | 41 | ### Laravel 8.* and above 42 | 43 | ```bash 44 | composer require ufirst/lang-import-export:^8.1.0 45 | ``` 46 | 47 | ### Laravel 7.* 48 | 49 | ```bash 50 | composer require ufirst/lang-import-export:^7.0.0 51 | ``` 52 | 53 | Finally add the following line to the `providers` array of your `app/config/app.php` file: 54 | 55 | ```php 56 | 'providers' => [ 57 | /* ... */ 58 | 'UFirst\LangImportExport\LangImportExportServiceProvider' 59 | ] 60 | ``` 61 | 62 | ### Laravel 5.* 63 | 64 | For Laravel 5.* checkout the [legacy branch](https://github.com/ufirstgroup/laravel-lang-import-export/tree/legacy) and require version ^5.1.2 65 | 66 | > The usage of the legacy version of this package is slightly different 67 | 68 | 69 | > As an alternative you can checkout the fork of this repository [highsolutions/laravel-lang-import-export](https://github.com/highsolutions/laravel-lang-import-export) 70 | 71 | 72 | Usage 73 | ----- 74 | 75 | The package currently provides two commands, one for exporting the files and one for importing them back: 76 | 77 | ### Export 78 | 79 | ```bash 80 | # export all locales with all groups to console 81 | php artisan lang-export:csv 82 | # export all locales with all groups to csv file 83 | php artisan lang-export:csv --output /some/file.csv 84 | # custom csv delimiter and enclosure 85 | php artisan lang-export:csv --delimiter=";" --enclosure='"' --output=/some/file.csv 86 | # export single locale 87 | php artisan lang-export:csv -l en --output=/some/path/translations-en.csv 88 | # export single translation group 89 | php artisan lang-export:csv -g navigation --output=/some/path/navigation-all-langs.csv 90 | ``` 91 | 92 | You can optionally pass the __-l__ (locale) and the __-g__ (group) as options. The group is the name of the langauge file without its extension. You may define options for your desired CSV format. 93 | 94 | ### Import 95 | 96 | 97 | ```bash 98 | # import translations from csv 99 | php artisan lang-import:csv /some/file.csv 100 | # import from custom csv format 101 | php artisan lang-import:csv --delimiter=";" --enclosure='"' --escape='\\' /some/file.csv 102 | # import and merge over existing translation file 103 | php artisan lang-import:csv --merge=true /some/file.csv 104 | ``` 105 | 106 | During import the locale is extracted from the first row of the CSV file. Translation groups are guessed from the translation keys e.g. __navigation.tips.next__ is imported to __navigation__ group 107 | 108 | 109 | ### Changelog 110 | 111 | 8.1.1 112 | - added support for laravel 10 113 | - fixed deprecated warnings for PHP 8.2+ 114 | 115 | 8.1.0 116 | - added support for laravel 9 117 | - dropped support below laravel 8 118 | - automatic service provider and alias discovery 119 | 120 | 8.0.2 121 | - merge imported csv with existing translations by adding --merge=true option to import command 122 | 123 | 8.0.1 124 | - fix: header delimiter and enclosure did not respect the given options during export 125 | 126 | 8.0.0 127 | - added support for Laravel 8.x 128 | 129 | 7.1.0 130 | - automatically create translation files during import based on translation keys in csv 131 | - formatted code (PSR-2) 132 | 133 | 7.0.0 134 | - added support for laravel:^7.0.0 135 | - added feature to export all locales and groups in a single run 136 | - the import command will guess the locale and the translation group from the csv 137 | - added symfony/var-exporter for new array syntax during the import command 138 | 139 | 5.1.2 140 | - legacy version 141 | - compatible with laravel:^5.4 142 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ufirst/lang-import-export", 3 | "description": "A Laravel package providing artisan commands to import and export language files from and to CSV.", 4 | "keywords": ["laravel", "localization", "translation", "messages", "import", "export", "CSV"], 5 | "authors": [ 6 | { 7 | "name": "Michael Ruoss", 8 | "email": "michael.ruoss@ufirstgroup.com" 9 | } 10 | ], 11 | "license": "MIT", 12 | "require": { 13 | "php": ">=7.3", 14 | "illuminate/support": "^8.0|^9.0|^10.0", 15 | "symfony/var-exporter": "^5.1|^6" 16 | }, 17 | "autoload": { 18 | "classmap": [ 19 | "src/migrations" 20 | ], 21 | "psr-0": { 22 | "UFirst\\LangImportExport\\": "src/" 23 | } 24 | }, 25 | "extra": { 26 | "component": "package", 27 | "frameworks": ["Laravel 7"], 28 | "laravel": { 29 | "providers": [ 30 | "UFirst\\LangImportExport\\LangImportExportServiceProvider" 31 | ], 32 | "aliases": { 33 | "LangListService": "UFirst\\LangImportExport\\Facades\\LangListService" 34 | } 35 | } 36 | }, 37 | "minimum-stability": "stable" 38 | } 39 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /public/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ufirstgroup/laravel-lang-import-export/9e1f104b53837239e4ca84d14706689b4b1d041a/public/.gitkeep -------------------------------------------------------------------------------- /src/UFirst/LangImportExport/Console/ExportToCsvCommand.php: -------------------------------------------------------------------------------- 1 | option('delimiter'); 61 | $enclosure = $this->option('enclosure'); 62 | $groupOption = $this->option('group'); 63 | $locale = $this->option('locale'); 64 | $languages = LangListService::allLanguages()->all(); 65 | if ($locale && !in_array($locale, $languages)) { 66 | $this->error("Locale {$locale} does not exist"); 67 | return; 68 | } 69 | $languages = $locale ? [$locale => $locale] : $languages; 70 | $translations = []; 71 | foreach ($languages as $language) { 72 | $groups = LangListService::allGroup($language)->all(); 73 | if ($groupOption) { 74 | if (in_array($groupOption, $groups)) { 75 | $groups = [$groupOption]; 76 | } else { 77 | $this->error("Group {$groupOption} does not exist for locale {$language}. Skipping"); 78 | continue; 79 | } 80 | } 81 | 82 | foreach ($groups as $group) { 83 | $strings = LangListService::loadLangList($language, $group); 84 | $strings = array_map(function ($value) use ($language) { 85 | return [$language => $value]; 86 | }, $strings); 87 | $translations = array_merge_recursive($translations, $strings); 88 | } 89 | } 90 | //normalize 91 | $defaultLanguValues = array_map(function () { 92 | return ""; 93 | }, $languages); 94 | $translations = array_map(function ($translation) use ($defaultLanguValues) { 95 | return array_merge($defaultLanguValues, $translation); 96 | }, $translations); 97 | // Create output device and write CSV. 98 | $output = $this->option('output'); 99 | if (empty($output) || !($out = fopen($output, 'w'))) { 100 | $out = fopen('php://output', 'w'); 101 | } 102 | // Write CSV lintes 103 | fputcsv($out, array_merge(["key"], $languages), $delimiter, $enclosure); 104 | foreach ($translations as $key => $values) { 105 | try { 106 | fputcsv($out, array_merge([$key], $values), $delimiter, $enclosure); 107 | } catch (\Exception $e) { 108 | $this->error("Failed to write {$key} with error: " . $e->getMessage()); 109 | } 110 | } 111 | fclose($out); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/UFirst/LangImportExport/Console/ImportFromCsvCommand.php: -------------------------------------------------------------------------------- 1 | argument('file'); 65 | 66 | $delimiter = $this->option('delimiter'); 67 | $enclosure = $this->option('enclosure'); 68 | $escape = $this->option('escape'); 69 | $merge = $this->option('merge'); 70 | 71 | // Create output device and write CSV. 72 | if (($input_fp = fopen($file, 'r')) === FALSE) { 73 | $this->error('Can\'t open the input file!'); 74 | } 75 | 76 | // Write CSV lintes 77 | $languages = fgetcsv($input_fp, 0, $delimiter, $enclosure, $escape); 78 | array_shift($languages); 79 | $translations = []; 80 | $lineNumber = 1; 81 | while (($data = fgetcsv($input_fp, 0, $delimiter, $enclosure, $escape)) !== FALSE) { 82 | $lineNumber++; 83 | try { 84 | $translations[array_shift($data)] = array_combine($languages, $data); 85 | } catch (\Exception $e) { 86 | $this->error("Failed to import line {$lineNumber}. Languages" . implode(", ", $languages) . ' | Translations: ' . implode(", ", $data) . ' ' . $e->getMessage()); 87 | } 88 | } 89 | fclose($input_fp); 90 | $this->writeLangList($languages, $translations, $merge); 91 | } 92 | 93 | private function getGroupsFromNewTranslations($new_translations) 94 | { 95 | $groups = []; 96 | foreach ($new_translations as $key => $value) { 97 | $group = explode('.', $key)[0]; 98 | $groups[$group] = $group; 99 | } 100 | return $groups; 101 | } 102 | 103 | private function writeLangList($languages, $new_translations, $should_merge_translations = false) 104 | { 105 | $groups = $this->getGroupsFromNewTranslations($new_translations); 106 | foreach ($languages as $locale) { 107 | foreach ($groups as $group) { 108 | $translations = LangListService::loadLangList($locale, $group); 109 | $override_translations = array_filter($new_translations, function ($key) use ($group) { 110 | return strpos($key, $group) === 0; 111 | }, ARRAY_FILTER_USE_KEY); 112 | if (count($override_translations) === 0) { 113 | $this->info("No translations were found for locale {$locale} within group {$group}"); 114 | continue; 115 | } 116 | foreach ($override_translations as $key => $value) { 117 | if ($value[$locale]) { 118 | if (!$should_merge_translations) { 119 | Arr::set($translations, $key, $value[$locale]); 120 | } else { 121 | $translations[$key] = $value[$locale]; 122 | } 123 | } else { 124 | Arr::forget($translations, $key); 125 | } 126 | } 127 | if ($should_merge_translations) { 128 | $undotted_translations = []; 129 | foreach ($translations as $key => $translation) { 130 | Arr::set($undotted_translations, $key, $translation); 131 | } 132 | $translations = $undotted_translations; 133 | } 134 | $header = "error("Language directory $language_dir does not exist or is not writeable. Skipping"); 138 | continue; 139 | } 140 | $language_file = base_path("resources/lang/{$locale}/{$group}.php"); 141 | if (!is_writable($language_file)) { 142 | $this->info("Creating language file: $language_file"); 143 | touch($language_file); 144 | } 145 | if (($fp = fopen($language_file, 'w')) !== FALSE) { 146 | fputs($fp, $header . VarExporter::export($translations[$group]) . ";\n"); 147 | fclose($fp); 148 | } else { 149 | $this->error("Cannot open language file at {$language_file} for writing. Check the file permissions."); 150 | } 151 | } 152 | } 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /src/UFirst/LangImportExport/Facades/LangListService.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 30 | $this->commands([ 31 | ExportToCsvCommand::class, 32 | ImportFromCsvCommand::class 33 | ]); 34 | $this->app->singleton('LangImportExportLangListService', function ($app) { 35 | return new LangListService(app(Filesystem::class), app()['path.lang']); 36 | }); 37 | } 38 | } 39 | 40 | /** 41 | * Get the services provided by the provider. 42 | * 43 | * @return array 44 | */ 45 | public function provides() 46 | { 47 | return [ 48 | 'lang-export.csv', 'lang-import.csv' 49 | ]; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/UFirst/LangImportExport/LangListService.php: -------------------------------------------------------------------------------- 1 | disk = $disk; 16 | $this->languageFilesPath = $languageFilesPath; 17 | } 18 | 19 | public function loadLangList($locale, $group) 20 | { 21 | $translations = Lang::getLoader()->load($locale, $group); 22 | $translations_with_prefix = Arr::dot(array($group => $translations)); 23 | return $translations_with_prefix; 24 | } 25 | 26 | /** 27 | * Get all languages from the application. 28 | * 29 | * @return Collection 30 | */ 31 | public function allLanguages() 32 | { 33 | $directories = Collection::make($this->disk->directories($this->languageFilesPath)); 34 | return $directories->mapWithKeys(function ($directory) { 35 | $language = basename($directory); 36 | return [$language => $language]; 37 | })->filter(function ($language) { 38 | return $language != 'vendor'; 39 | }); 40 | } 41 | 42 | /** 43 | * Get all group translations from the application. 44 | * 45 | * @return array|Collection 46 | */ 47 | public function allGroup($language) 48 | { 49 | $groupPath = "{$this->languageFilesPath}" . DIRECTORY_SEPARATOR . "{$language}"; 50 | if (!$this->disk->exists($groupPath)) { 51 | return []; 52 | } 53 | $groups = Collection::make($this->disk->allFiles($groupPath)); 54 | return $groups->map(function ($group) { 55 | if (empty($group->getRelativePath())) { 56 | return $group->getBasename('.php'); 57 | } else { 58 | return $group->getRelativePath() . '/' . $group->getBasename('.php'); 59 | } 60 | }); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/config/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ufirstgroup/laravel-lang-import-export/9e1f104b53837239e4ca84d14706689b4b1d041a/src/config/.gitkeep -------------------------------------------------------------------------------- /src/controllers/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ufirstgroup/laravel-lang-import-export/9e1f104b53837239e4ca84d14706689b4b1d041a/src/controllers/.gitkeep -------------------------------------------------------------------------------- /src/lang/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ufirstgroup/laravel-lang-import-export/9e1f104b53837239e4ca84d14706689b4b1d041a/src/lang/.gitkeep -------------------------------------------------------------------------------- /src/migrations/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ufirstgroup/laravel-lang-import-export/9e1f104b53837239e4ca84d14706689b4b1d041a/src/migrations/.gitkeep -------------------------------------------------------------------------------- /src/views/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ufirstgroup/laravel-lang-import-export/9e1f104b53837239e4ca84d14706689b4b1d041a/src/views/.gitkeep -------------------------------------------------------------------------------- /tests/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ufirstgroup/laravel-lang-import-export/9e1f104b53837239e4ca84d14706689b4b1d041a/tests/.gitkeep --------------------------------------------------------------------------------