@@ -117,6 +117,26 @@ void TranslationServer::init_locale_info() {
117117 }
118118 idx++;
119119 }
120+
121+ // Init number systems.
122+ num_system_map.clear ();
123+ idx = 0 ;
124+ while (num_system_data[idx].locales != nullptr ) {
125+ const NumSystemData &nsd = num_system_data[idx];
126+
127+ // These fields must not be empty.
128+ DEV_ASSERT (nsd.percent_sign && nsd.percent_sign [0 ] != ' \0 ' );
129+ DEV_ASSERT (nsd.digits && nsd.digits [0 ] != ' \0 ' );
130+ DEV_ASSERT (nsd.exp_l && nsd.exp_l [0 ] != ' \0 ' );
131+ DEV_ASSERT (nsd.exp_u && nsd.exp_u [0 ] != ' \0 ' );
132+ DEV_ASSERT (strlen (nsd.digits ) == 11 );
133+
134+ const Vector<String> locales = String (nsd.locales ).split (" " );
135+ for (const String &l : locales) {
136+ num_system_map[l] = idx;
137+ }
138+ idx++;
139+ }
120140}
121141
122142TranslationServer::Locale::operator String () const {
@@ -219,6 +239,66 @@ TranslationServer::Locale::Locale(const TranslationServer &p_server, const Strin
219239 }
220240}
221241
242+ String TranslationServer::format_number (const String &p_string, const String &p_locale) const {
243+ ERR_FAIL_COND_V (p_locale.is_empty (), p_string);
244+ if (!num_system_map.has (p_locale)) {
245+ return p_string;
246+ }
247+
248+ int index = num_system_map[p_locale];
249+ const NumSystemData &nsd = num_system_data[index];
250+
251+ String res = p_string;
252+ res = res.replace (" e" , nsd.exp_l );
253+ res = res.replace (" E" , nsd.exp_u );
254+ char32_t *data = res.ptrw ();
255+ for (int j = 0 ; j < res.length (); j++) {
256+ if (data[j] >= 0x30 && data[j] <= 0x39 ) {
257+ data[j] = nsd.digits [data[j] - 0x30 ];
258+ } else if (data[j] == ' .' || data[j] == ' ,' ) {
259+ data[j] = nsd.digits [10 ];
260+ }
261+ }
262+ return res;
263+ }
264+
265+ String TranslationServer::parse_number (const String &p_string, const String &p_locale) const {
266+ ERR_FAIL_COND_V (p_locale.is_empty (), p_string);
267+ if (!num_system_map.has (p_locale)) {
268+ return p_string;
269+ }
270+
271+ int index = num_system_map[p_locale];
272+ const NumSystemData &nsd = num_system_data[index];
273+
274+ String res = p_string;
275+ res = res.replace (nsd.exp_l , " e" );
276+ res = res.replace (nsd.exp_u , " E" );
277+ char32_t *data = res.ptrw ();
278+ for (int j = 0 ; j < res.length (); j++) {
279+ if (data[j] == nsd.digits [10 ]) {
280+ data[j] = ' .' ;
281+ } else {
282+ for (int k = 0 ; k < 10 ; k++) {
283+ if (data[j] == nsd.digits [k]) {
284+ data[j] = 0x30 + k;
285+ }
286+ }
287+ }
288+ }
289+ return res;
290+ }
291+
292+ String TranslationServer::get_percent_sign (const String &p_locale) const {
293+ ERR_FAIL_COND_V (p_locale.is_empty (), " %" );
294+ if (!num_system_map.has (p_locale)) {
295+ return " %" ;
296+ }
297+
298+ int index = num_system_map[p_locale];
299+ return num_system_data[index].percent_sign ;
300+ }
301+
222302String TranslationServer::standardize_locale (const String &p_locale, bool p_add_defaults) const {
223303 return Locale (*this , p_locale, p_add_defaults).operator String ();
224304}
@@ -604,6 +684,10 @@ void TranslationServer::_bind_methods() {
604684
605685 ClassDB::bind_method (D_METHOD (" get_loaded_locales" ), &TranslationServer::get_loaded_locales);
606686
687+ ClassDB::bind_method (D_METHOD (" format_number" , " number" , " locale" ), &TranslationServer::format_number);
688+ ClassDB::bind_method (D_METHOD (" get_percent_sign" , " locale" ), &TranslationServer::get_percent_sign);
689+ ClassDB::bind_method (D_METHOD (" parse_number" , " number" , " locale" ), &TranslationServer::parse_number);
690+
607691 ClassDB::bind_method (D_METHOD (" is_pseudolocalization_enabled" ), &TranslationServer::is_pseudolocalization_enabled);
608692 ClassDB::bind_method (D_METHOD (" set_pseudolocalization_enabled" , " enabled" ), &TranslationServer::set_pseudolocalization_enabled);
609693 ClassDB::bind_method (D_METHOD (" reload_pseudolocalization" ), &TranslationServer::reload_pseudolocalization);
0 commit comments