Merge pull request #14062 from uberbrady/add_down_migration_for_new_localization_refactor

Added back-migration for Big Locale Refactoring
This commit is contained in:
snipe 2023-12-19 20:08:27 +00:00 committed by GitHub
commit de62e2775e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 52 deletions

View file

@ -1342,14 +1342,8 @@ class Helper
* @param $language_code
* @return string []
*/
public static function mapLegacyLocale($language_code = null)
{
if (strlen($language_code) > 4) {
return $language_code;
}
$languages = [
public static $language_map = [
'af' => 'af-ZA', // Afrikaans
'am' => 'am-ET', // Amharic
'ar' => 'ar-SA', // Arabic
@ -1399,7 +1393,15 @@ class Helper
'zu' => 'zu-ZA', // Zulu
];
foreach ($languages as $legacy => $new) {
public static function mapLegacyLocale($language_code = null)
{
if (strlen($language_code) > 4) {
return $language_code;
}
foreach (self::$language_map as $legacy => $new) {
if ($language_code == $legacy) {
\Log::debug('Current language is '.$legacy.', using '.$new.' instead');
return $new;
@ -1407,4 +1409,19 @@ class Helper
}
}
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'
}
}

View file

@ -42,6 +42,19 @@ class FixLanguageDirs extends Migration
*/
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)]);
}
}
}