Skip to content

Commit

Permalink
Update CSV export language codes to align with the languages supporte…
Browse files Browse the repository at this point in the history
…d by the server (#8190)

Co-authored-by: Dan Paun <[email protected]>
  • Loading branch information
mordeth and dpaun1985 authored Feb 14, 2024
1 parent 1829f72 commit e0efe77
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions changelog/fix-csv-export-localization-locale
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Adjust WordPress locale code to match the languages supported by the server.
46 changes: 45 additions & 1 deletion includes/class-wc-payments-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ public static function get_language_data( $language ) {

if ( isset( $translations[ $language ] ) ) {
return [
'code' => $language,
'code' => self::convert_to_server_locale( $language ),
'english_name' => $translations[ $language ]['english_name'] ?? $language,
'native_name' => $translations[ $language ]['native_name'] ?? $language,
];
Expand All @@ -1020,4 +1020,48 @@ public static function get_language_data( $language ) {
'native_name' => 'English (United States)',
];
}

/**
* Converts a locale to the server supported languages.
*
* @param string $locale The locale to convert.
*
* @return string Closest locale supported ('en' if NONE)
*/
public static function convert_to_server_locale( string $locale ): string {
$supported = [
'ar', // Arabic.
'de', // German (Germany).
'es', // Spanish (Spain).
'fr', // French (France).
'he', // Hebrew (Israel).
'id', // Indonesian (Indonesia).
'it', // Italian (Italy).
'ja', // Japanese.
'ko', // Korean.
'nl', // Dutch (Netherlands).
'pt-br', // Portuguese (Brazil).
'ru', // Russian (Russia).
'sv', // Swedish (Sweden).
'tr', // Turkish (Turkey).
'zh-cn', // Simplified, Singapore).
'zh-tw', // Chinese Traditional (Taiwan).
];

// Replace '-' with '_' (used in WordPress).
$locale = str_replace( '_', '-', $locale );

if ( in_array( $locale, $supported, true ) ) {
return $locale;
}

// Remove the country code and try with that.
$base_locale = substr( $locale, 0, 2 );
if ( in_array( $base_locale, $supported, true ) ) {
return $base_locale;
}

// Return 'en_US' to match the default site language.
return 'en_US';
}
}

0 comments on commit e0efe77

Please sign in to comment.