| 1 |
<?php |
| 2 |
/** |
| 3 |
* Supported languages for AI Content translation. |
| 4 |
* |
| 5 |
* @package WordPress\AI |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace WordPress\AI\Experiments\Content_Translation; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class providing supported languages for AI content translation. |
| 14 |
* |
| 15 |
* @since 1.3.0 |
| 16 |
*/ |
| 17 |
final class Languages { |
| 18 |
|
| 19 |
/** |
| 20 |
* The default target language for translation. |
| 21 |
* |
| 22 |
* @since 1.3.0 |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private const DEFAULT_TARGET_LANGUAGE = 'en-us'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Returns the default target language for translation. |
| 30 |
* |
| 31 |
* @since 1.3.0 |
| 32 |
* |
| 33 |
* @return string The default target language code. |
| 34 |
*/ |
| 35 |
public static function get_default_target_language(): string { |
| 36 |
return self::DEFAULT_TARGET_LANGUAGE; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns the supported languages for AI content translation. |
| 41 |
* |
| 42 |
* @since 1.3.0 |
| 43 |
* |
| 44 |
* @return array<string, string> Supported languages. |
| 45 |
*/ |
| 46 |
public static function get_supported_languages(): array { |
| 47 |
$languages = array( |
| 48 |
'ar' => __( 'Arabic', 'ai' ), |
| 49 |
'zh-cn' => __( 'Chinese (Simplified)', 'ai' ), |
| 50 |
'zh-tw' => __( 'Chinese (Traditional)', 'ai' ), |
| 51 |
'nl-nl' => __( 'Dutch', 'ai' ), |
| 52 |
'en-gb' => __( 'English (UK)', 'ai' ), |
| 53 |
'en-us' => __( 'English (US)', 'ai' ), |
| 54 |
'fr-fr' => __( 'French', 'ai' ), |
| 55 |
'de-de' => __( 'German', 'ai' ), |
| 56 |
'hi' => __( 'Hindi', 'ai' ), |
| 57 |
'it-it' => __( 'Italian', 'ai' ), |
| 58 |
'ja' => __( 'Japanese', 'ai' ), |
| 59 |
'ko' => __( 'Korean', 'ai' ), |
| 60 |
'pt-br' => __( 'Portuguese (Brazil)', 'ai' ), |
| 61 |
'es-es' => __( 'Spanish', 'ai' ), |
| 62 |
); |
| 63 |
|
| 64 |
/** |
| 65 |
* Filters supported target languages for AI content translation. |
| 66 |
* |
| 67 |
* Codes are normalized with `sanitize_key()` and entries with a |
| 68 |
* non-string or empty label are discarded. |
| 69 |
* |
| 70 |
* @since 1.3.0 |
| 71 |
* |
| 72 |
* @param array<string, string> $languages Supported languages. |
| 73 |
*/ |
| 74 |
$filtered = apply_filters( 'wpai_content_translation_languages', $languages ); |
| 75 |
|
| 76 |
// A filter returning a non-array is ignored so the language picker and |
| 77 |
// the ability schema keep working rather than degrading to junk entries. |
| 78 |
if ( ! is_array( $filtered ) ) { |
| 79 |
$filtered = $languages; |
| 80 |
} |
| 81 |
|
| 82 |
/* |
| 83 |
* Normalize the filtered list. Codes are sanitized the same way incoming |
| 84 |
* `target_language` input is, so lookups agree, and entries that are not |
| 85 |
* usable strings are dropped rather than propagated into the ability |
| 86 |
* schema and the typed helpers below. |
| 87 |
*/ |
| 88 |
$normalized = array(); |
| 89 |
|
| 90 |
foreach ( $filtered as $code => $name ) { |
| 91 |
if ( ! is_string( $name ) || '' === trim( $name ) ) { |
| 92 |
continue; |
| 93 |
} |
| 94 |
|
| 95 |
$code = sanitize_key( (string) $code ); |
| 96 |
|
| 97 |
if ( '' === $code ) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
|
| 101 |
$normalized[ $code ] = $name; |
| 102 |
} |
| 103 |
|
| 104 |
return $normalized; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Returns the supported languages for AI content translation in a format suitable for JavaScript. |
| 109 |
* |
| 110 |
* @since 1.3.0 |
| 111 |
* |
| 112 |
* @return list<array{code: string, name: string}> Supported languages for JavaScript. |
| 113 |
*/ |
| 114 |
public static function get_supported_languages_for_js(): array { |
| 115 |
$languages = array(); |
| 116 |
|
| 117 |
/* |
| 118 |
* Built with a loop rather than array_map() over array_keys(): PHP casts |
| 119 |
* numeric-string array keys to integers, which would fail a typed |
| 120 |
* string parameter under strict_types. |
| 121 |
*/ |
| 122 |
foreach ( self::get_supported_languages() as $code => $name ) { |
| 123 |
$languages[] = array( |
| 124 |
'code' => (string) $code, |
| 125 |
'name' => $name, |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
return $languages; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Returns the name of a language given its code. |
| 134 |
* |
| 135 |
* @since 1.3.0 |
| 136 |
* |
| 137 |
* @param string $language_code The language code. |
| 138 |
* @return string|null The name of the language, or null if not found. |
| 139 |
*/ |
| 140 |
public static function get_language_name( string $language_code ): ?string { |
| 141 |
$languages = self::get_supported_languages(); |
| 142 |
|
| 143 |
if ( array_key_exists( $language_code, $languages ) ) { |
| 144 |
return (string) $languages[ $language_code ]; |
| 145 |
} |
| 146 |
|
| 147 |
return null; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Returns the language codes of supported languages. |
| 152 |
* |
| 153 |
* @since 1.3.0 |
| 154 |
* |
| 155 |
* @return list<string> Array of supported language codes. |
| 156 |
*/ |
| 157 |
public static function get_codes(): array { |
| 158 |
// Cast to string because PHP stores numeric-string array keys as integers. |
| 159 |
return array_map( 'strval', array_keys( self::get_supported_languages() ) ); |
| 160 |
} |
| 161 |
} |
| 162 |
|