Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added back-migration for Big Locale Refactoring #14062

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 68 additions & 51 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1342,69 +1342,86 @@ public static function showDemoModeFieldWarning() {
* @param $language_code
* @return string []
*/

public static $language_map = [
'af' => 'af-ZA', // Afrikaans
'am' => 'am-ET', // Amharic
'ar' => 'ar-SA', // Arabic
'bg' => 'bg-BG', // Bulgarian
'ca' => 'ca-ES', // Catalan
'cs' => 'cs-CZ', // Czech
'cy' => 'cy-GB', // Welsh
'da' => 'da-DK', // Danish
'de-i' => 'de-if', // German informal
'de' => 'de-DE', // German
'el' => 'el-GR', // Greek
'en' => 'en-US', // English
'et' => 'et-EE', // Estonian
'fa' => 'fa-IR', // Persian
'fi' => 'fi-FI', // Finnish
'fil' => 'fil-PH', // Filipino
'fr' => 'fr-FR', // French
'he' => 'he-IL', // Hebrew
'hr' => 'hr-HR', // Croatian
'hu' => 'hu-HU', // Hungarian
'id' => 'id-ID', // Indonesian
'is' => 'is-IS', // Icelandic
'it' => 'it-IT', // Italian
'iu' => 'iu-NU', // Inuktitut
'ja' => 'ja-JP', // Japanese
'ko' => 'ko-KR', // Korean
'lt' => 'lt-LT', // Lithuanian
'lv' => 'lv-LV', // Latvian
'mi' => 'mi-NZ', // Maori
'mk' => 'mk-MK', // Macedonian
'mn' => 'mn-MN', // Mongolian
'ms' => 'ms-MY', // Malay
'nl' => 'nl-NL', // Dutch
'no' => 'no-NO', // Norwegian
'pl' => 'pl-PL', // Polish
'ro' => 'ro-RO', // Romanian
'ru' => 'ru-RU', // Russian
'sk' => 'sk-SK', // Slovak
'sl' => 'sl-SI', // Slovenian
'so' => 'so-SO', // Somali
'ta' => 'ta-IN', // Tamil
'th' => 'th-TH', // Thai
'tl' => 'tl-PH', // Tagalog
'tr' => 'tr-TR', // Turkish
'uk' => 'uk-UA', // Ukrainian
'vi' => 'vi-VN', // Vietnamese
'zu' => 'zu-ZA', // Zulu
];


public static function mapLegacyLocale($language_code = null)
{

if (strlen($language_code) > 4) {
return $language_code;
}

$languages = [
'af' => 'af-ZA', // Afrikaans
'am' => 'am-ET', // Amharic
'ar' => 'ar-SA', // Arabic
'bg' => 'bg-BG', // Bulgarian
'ca' => 'ca-ES', // Catalan
'cs' => 'cs-CZ', // Czech
'cy' => 'cy-GB', // Welsh
'da' => 'da-DK', // Danish
'de-i' => 'de-if', // German informal
'de' => 'de-DE', // German
'el' => 'el-GR', // Greek
'en' => 'en-US', // English
'et' => 'et-EE', // Estonian
'fa' => 'fa-IR', // Persian
'fi' => 'fi-FI', // Finnish
'fil' => 'fil-PH', // Filipino
'fr' => 'fr-FR', // French
'he' => 'he-IL', // Hebrew
'hr' => 'hr-HR', // Croatian
'hu' => 'hu-HU', // Hungarian
'id' => 'id-ID', // Indonesian
'is' => 'is-IS', // Icelandic
'it' => 'it-IT', // Italian
'iu' => 'iu-NU', // Inuktitut
'ja' => 'ja-JP', // Japanese
'ko' => 'ko-KR', // Korean
'lt' => 'lt-LT', // Lithuanian
'lv' => 'lv-LV', // Latvian
'mi' => 'mi-NZ', // Maori
'mk' => 'mk-MK', // Macedonian
'mn' => 'mn-MN', // Mongolian
'ms' => 'ms-MY', // Malay
'nl' => 'nl-NL', // Dutch
'no' => 'no-NO', // Norwegian
'pl' => 'pl-PL', // Polish
'ro' => 'ro-RO', // Romanian
'ru' => 'ru-RU', // Russian
'sk' => 'sk-SK', // Slovak
'sl' => 'sl-SI', // Slovenian
'so' => 'so-SO', // Somali
'ta' => 'ta-IN', // Tamil
'th' => 'th-TH', // Thai
'tl' => 'tl-PH', // Tagalog
'tr' => 'tr-TR', // Turkish
'uk' => 'uk-UA', // Ukrainian
'vi' => 'vi-VN', // Vietnamese
'zu' => 'zu-ZA', // Zulu
];

foreach ($languages as $legacy => $new) {
foreach (self::$language_map as $legacy => $new) {
if ($language_code == $legacy) {
\Log::debug('Current language is '.$legacy.', using '.$new.' instead');
return $new;
}
}
}

public static function mapBackToLegacyLocale($new_locale = null)
{
if (strlen($new_locale) <= 4) {
return $new_locale; //"new locale" apparently wasn't quite so new
}

// This does a *reverse* search against our new language map array - given the value, find the *key* for it
$legacy_locale = array_search($new_locale, self::$language_map);

if($legacy_locale !== false) {
return $legacy_locale;
}
return $new_locale; // better that you have some weird locale that doesn't fit into our mappings anywhere than 'void'
}

}
15 changes: 14 additions & 1 deletion database/migrations/2023_12_19_081112_fix_language_dirs.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ public function up()
*/
public function down()
{
//
$settings = Setting::getSettings();
if (($settings) && ($settings->locale != '')) {
DB::table('settings')->update(['locale' => Helper::mapBackToLegacyLocale($settings->locale)]);
}

/**
* Update the users table
*/
$users = User::whereNotNull('locale')->whereNull('deleted_at')->get();
// Skip the model in case the validation rules have changed
foreach ($users as $user) {
DB::table('users')->where('id', $user->id)->update(['locale' => Helper::mapBackToLegacyLocale($user->locale)]);
}

}
}
Loading