feat: Dynamically add all languages to db

Add unit test to find missing translations
This commit is contained in:
Attila Kerekes 2022-11-14 17:51:43 +01:00 committed by Attila Jozsef Kerekes
parent 99193a578e
commit edb51e56f1
No known key found for this signature in database
GPG key ID: E1121565A016ADFD
12 changed files with 135 additions and 52 deletions

View file

@ -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",

View file

@ -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();

View file

@ -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

View file

@ -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',

View file

@ -1,19 +0,0 @@
<?php
namespace Tests\Unit;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace Tests\Unit\database\seeders;
use Database\Seeders\SettingsSeeder;
use Tests\TestCase;
class SettingsSeederTest extends TestCase
{
/**
* All language keys are defined in all languages based on the en language file.
*
* @return void
*/
public function testReturnsAJSONMapWithSameAmountOfItemsAsLanguageDirectoriesPresent()
{
$languageDirectories = array_filter(glob(resource_path().'/lang/*'), 'is_dir');
$languageMap = json_decode(SettingsSeeder::getSupportedLanguageMap(), true);
$this->assertTrue(count($languageMap) === count($languageDirectories));
}
}

View file

@ -0,0 +1,48 @@
<?php
namespace Tests\Unit\lang;
use Tests\TestCase;
class LangTest extends TestCase
{
/**
* All language keys are defined in all languages based on the en language file.
*
* @return void
*/
public function testAllLanguageKeysAreDefined()
{
$this->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);
}
}