From edb51e56f1b82f8afbab9266a7ed3daf3f531167 Mon Sep 17 00:00:00 2001 From: Attila Kerekes Date: Mon, 14 Nov 2022 17:51:43 +0100 Subject: [PATCH] feat: Dynamically add all languages to db Add unit test to find missing translations --- composer.json | 3 +- database/seeders/SettingsSeeder.php | 47 ++++++++++++------ readme.md | 19 ++++++-- resources/lang/lmo/app.php | 28 +++++------ resources/lang/{cn => zh}/app.php | 0 resources/lang/{cn => zh}/auth.php | 0 resources/lang/{cn => zh}/pagination.php | 0 resources/lang/{cn => zh}/passwords.php | 0 resources/lang/{cn => zh}/validation.php | 0 tests/Unit/ExampleTest.php | 19 -------- .../database/seeders/SettingsSeederTest.php | 23 +++++++++ tests/Unit/lang/LangTest.php | 48 +++++++++++++++++++ 12 files changed, 135 insertions(+), 52 deletions(-) rename resources/lang/{cn => zh}/app.php (100%) rename resources/lang/{cn => zh}/auth.php (100%) rename resources/lang/{cn => zh}/pagination.php (100%) rename resources/lang/{cn => zh}/passwords.php (100%) rename resources/lang/{cn => zh}/validation.php (100%) delete mode 100644 tests/Unit/ExampleTest.php create mode 100644 tests/Unit/database/seeders/SettingsSeederTest.php create mode 100644 tests/Unit/lang/LangTest.php diff --git a/composer.json b/composer.json index a85f901f..ecf58a9c 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "laravelcollective/html": "^6.0", "nunomaduro/collision": "^5.0", "symfony/yaml": "^5.4", - "ext-json": "*" + "ext-json": "*", + "ext-intl": "*" }, "require-dev": { "filp/whoops": "~2.0", diff --git a/database/seeders/SettingsSeeder.php b/database/seeders/SettingsSeeder.php index 913ad112..db9263ba 100644 --- a/database/seeders/SettingsSeeder.php +++ b/database/seeders/SettingsSeeder.php @@ -7,9 +7,40 @@ use App\SettingGroup; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; +use Locale; class SettingsSeeder extends Seeder { + + /** + * @return false|string + */ + public static function getSupportedLanguageMap() + { + $languageDirectories = array_filter(glob(resource_path().'/lang/*'), 'is_dir'); + $result = []; + + foreach ($languageDirectories as $languageDirectory) { + $language = self::getLanguageFromDirectory($languageDirectory); + $resultNative = mb_convert_case(Locale::getDisplayLanguage($language.'-', $language), MB_CASE_TITLE, 'UTF-8'); + $resultEn = ucfirst(Locale::getDisplayLanguage($language, 'en')); + $result[$language] = "$resultNative ($resultEn)"; + } + + return json_encode($result); + } + + /** + * @param $languageDirectory + * @return false|string[] + */ + public static function getLanguageFromDirectory($languageDirectory) + { + $directories = explode('/', $languageDirectory); + + return $directories[array_key_last($directories)]; + } + /** * Run the database seeds. * @@ -124,20 +155,8 @@ class SettingsSeeder extends Seeder $setting->save(); } - $language_options = json_encode([ - 'de' => 'Deutsch (German)', - 'en' => 'English', - 'cn' => '简体中文 (Simplified Chinese)', - 'fi' => 'Suomi (Finnish)', - 'fr' => 'Français (French)', - 'el' => 'Ελληνικά (Greek)', - 'it' => 'Italiano (Italian)', - 'no' => 'Norsk (Norwegian)', - 'pl' => 'Polski (Polish)', - 'sv' => 'Svenska (Swedish)', - 'es' => 'Español (Spanish)', - 'tr' => 'Türkçe (Turkish)', - ]); + $language_options = SettingsSeeder::getSupportedLanguageMap(); + if ($languages = Setting::find(5)) { $languages->options = $language_options; $languages->save(); diff --git a/readme.md b/readme.md index e7af1249..1331183a 100644 --- a/readme.md +++ b/readme.md @@ -84,15 +84,26 @@ When you are finished, create a pull request. Currently added languages are - English +- Breton +- Danish - German +- Greek +- Spanish - Finnish - French -- Swedish -- Spanish -- Turkish +- Hungarian +- Italian +- Korean +- Lombard +- Dutch +- Norwegian +- Polish +- Portuguese - Russian +- Slovenian +- Swedish +- Turkish - Chinese -- Português ## Web Server Configuration diff --git a/resources/lang/lmo/app.php b/resources/lang/lmo/app.php index 897d70c2..a72f6792 100644 --- a/resources/lang/lmo/app.php +++ b/resources/lang/lmo/app.php @@ -34,11 +34,11 @@ return [ 'settings.window_target.current' => 'In quella scheda chi', - 'settings.window_target.one' => 'In l'istessa scheda', + 'settings.window_target.one' => 'In l\'istessa scheda', - 'settings.window_target.new' => 'In d'una scheda noeuva', + 'settings.window_target.new' => 'In d\'una scheda noeuva', - 'settings.homepage_search' => 'Ricerca in l'home page', + 'settings.homepage_search' => 'Ricerca in l\'home page', 'settings.search_provider' => 'Provider de default', @@ -90,17 +90,17 @@ return [ 'buttons.add' => 'Sgionta', - 'buttons.upload' => 'Carega 'n icona', + 'buttons.upload' => 'Carega \'n icona', 'buttons.downloadapps' => 'Descarega la lista di app', - 'dash.pin_item' => 'Taca su l'oget', + 'dash.pin_item' => 'Taca su l\'oget', - 'dash.no_apps' => 'Ghe n'è minga de app tacade su, :link1 o :link2', + 'dash.no_apps' => 'Ghe n\'è minga de app tacade su, :link1 o :link2', - 'dash.link1' => 'Met 'n aplicazzion chi', + 'dash.link1' => 'Met \'n aplicazzion chi', - 'dash.link2' => 'Taca su 'n oget', + 'dash.link2' => 'Taca su \'n oget', 'dash.pinned_items' => 'Oget tacad su', @@ -110,7 +110,7 @@ return [ 'apps.add_application' => 'Sgionta aplicazzion', - 'apps.application_name' => 'Nom de l'aplicazzion', + 'apps.application_name' => 'Nom de l\'aplicazzion', 'apps.colour' => 'Color', @@ -140,7 +140,7 @@ return [ 'apps.tags' => 'Tag', - 'apps.override' => 'Se divers de l'url principal', + 'apps.override' => 'Se divers de l\'url principal', 'apps.preview' => 'Varda prima', @@ -150,7 +150,7 @@ return [ 'apps.description' => 'Descrizzion', - 'apps.only_admin_account' => 'Domà se te gh'heet un cunt admin!', + 'apps.only_admin_account' => 'Domà se te gh\'heet un cunt admin!', 'apps.autologin_url' => 'Auto login url', @@ -172,7 +172,7 @@ return [ 'user.secure_front' => 'Fà entra del front - El fonziona domà cont una password.', - 'user.autologin' => 'Permet el login de 'n ciar URL. Tucc quèi che ghe l'hann poden entrà.', + 'user.autologin' => 'Permet el login de \'n ciar URL. Tucc quèi che ghe l\'hann poden entrà.', 'url' => 'URL', @@ -202,9 +202,9 @@ return [ 'alert.success.tag_restored' => 'Tag ripescad', - 'alert.success.setting_updated' => 'L'impostazzion l'è stada mudada', + 'alert.success.setting_updated' => 'L\'impostazzion l\'è stada mudada', - 'alert.error.not_exist' => 'Quella impostazzion chi la gh'è no.', + 'alert.error.not_exist' => 'Quella impostazzion chi la gh\'è no.', 'alert.success.user_created' => 'Utent fad su', diff --git a/resources/lang/cn/app.php b/resources/lang/zh/app.php similarity index 100% rename from resources/lang/cn/app.php rename to resources/lang/zh/app.php diff --git a/resources/lang/cn/auth.php b/resources/lang/zh/auth.php similarity index 100% rename from resources/lang/cn/auth.php rename to resources/lang/zh/auth.php diff --git a/resources/lang/cn/pagination.php b/resources/lang/zh/pagination.php similarity index 100% rename from resources/lang/cn/pagination.php rename to resources/lang/zh/pagination.php diff --git a/resources/lang/cn/passwords.php b/resources/lang/zh/passwords.php similarity index 100% rename from resources/lang/cn/passwords.php rename to resources/lang/zh/passwords.php diff --git a/resources/lang/cn/validation.php b/resources/lang/zh/validation.php similarity index 100% rename from resources/lang/cn/validation.php rename to resources/lang/zh/validation.php diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php deleted file mode 100644 index 266ef352..00000000 --- a/tests/Unit/ExampleTest.php +++ /dev/null @@ -1,19 +0,0 @@ -assertTrue(true); - } -} diff --git a/tests/Unit/database/seeders/SettingsSeederTest.php b/tests/Unit/database/seeders/SettingsSeederTest.php new file mode 100644 index 00000000..af054eed --- /dev/null +++ b/tests/Unit/database/seeders/SettingsSeederTest.php @@ -0,0 +1,23 @@ +assertTrue(count($languageMap) === count($languageDirectories)); + } +} diff --git a/tests/Unit/lang/LangTest.php b/tests/Unit/lang/LangTest.php new file mode 100644 index 00000000..671fba73 --- /dev/null +++ b/tests/Unit/lang/LangTest.php @@ -0,0 +1,48 @@ +markTestSkipped('2022-11-14 Lot of keys missing. Enable this test to see them all.'); + $languageDirectories = array_filter(glob(resource_path().'/lang/*'), 'is_dir'); + + $enLanguageDirectory = array_values(array_filter($languageDirectories, function($v) { + return substr($v, -2) === 'en'; + }))[0]; + $notENLanguageDirectories = array_filter($languageDirectories, function ($v) { + return substr($v, -2) !== 'en'; + }); + + $enLanguageKeys = require_once($enLanguageDirectory.'/app.php'); + $missingKeys = []; + + foreach ($notENLanguageDirectories as $langDirectory) { + $testingLangKeys = require_once($langDirectory . '/app.php'); + + foreach ($enLanguageKeys as $langKey => $langValue) { + if (!array_key_exists($langKey, $testingLangKeys)) { + if(!isset($missingKeys[$langDirectory])) { + $missingKeys[$langDirectory] = []; + } + $missingKeys[$langDirectory][] = $langKey; + } + } + } + + if (count($missingKeys) > 0) { + print_r(json_encode($missingKeys)); + } + + $this->assertEmpty($missingKeys); + } +}