@@ -117,6 +117,25 @@ 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+
133+ const Vector<String> locales = String (nsd.locales ).split (" " );
134+ for (const String &l : locales) {
135+ num_system_map[l] = idx;
136+ }
137+ idx++;
138+ }
120139}
121140
122141TranslationServer::Locale::operator String () const {
@@ -219,6 +238,66 @@ TranslationServer::Locale::Locale(const TranslationServer &p_server, const Strin
219238 }
220239}
221240
241+ String TranslationServer::format_number (const String &p_string, const String &p_locale) const {
242+ ERR_FAIL_COND_V (p_locale.is_empty (), p_string);
243+ if (!num_system_map.has (p_locale)) {
244+ return p_string;
245+ }
246+
247+ int index = num_system_map[p_locale];
248+ const NumSystemData &nsd = num_system_data[index];
249+
250+ String res = p_string;
251+ res = res.replace (" e" , nsd.exp_l );
252+ res = res.replace (" E" , nsd.exp_u );
253+ char32_t *data = res.ptrw ();
254+ for (int j = 0 ; j < res.length (); j++) {
255+ if (data[j] >= 0x30 && data[j] <= 0x39 ) {
256+ data[j] = nsd.digits [data[j] - 0x30 ];
257+ } else if (data[j] == ' .' || data[j] == ' ,' ) {
258+ data[j] = nsd.digits [10 ];
259+ }
260+ }
261+ return res;
262+ }
263+
264+ String TranslationServer::parse_number (const String &p_string, const String &p_locale) const {
265+ ERR_FAIL_COND_V (p_locale.is_empty (), p_string);
266+ if (!num_system_map.has (p_locale)) {
267+ return p_string;
268+ }
269+
270+ int index = num_system_map[p_locale];
271+ const NumSystemData &nsd = num_system_data[index];
272+
273+ String res = p_string;
274+ res = res.replace (nsd.exp_l , " e" );
275+ res = res.replace (nsd.exp_u , " E" );
276+ char32_t *data = res.ptrw ();
277+ for (int j = 0 ; j < res.length (); j++) {
278+ if (data[j] == nsd.digits [10 ]) {
279+ data[j] = ' .' ;
280+ } else {
281+ for (int k = 0 ; k < 10 ; k++) {
282+ if (data[j] == nsd.digits [k]) {
283+ data[j] = 0x30 + k;
284+ }
285+ }
286+ }
287+ }
288+ return res;
289+ }
290+
291+ String TranslationServer::get_percent_sign (const String &p_locale) const {
292+ ERR_FAIL_COND_V (p_locale.is_empty (), " %" );
293+ if (!num_system_map.has (p_locale)) {
294+ return " %" ;
295+ }
296+
297+ int index = num_system_map[p_locale];
298+ return num_system_data[index].percent_sign ;
299+ }
300+
222301String TranslationServer::standardize_locale (const String &p_locale, bool p_add_defaults) const {
223302 return Locale (*this , p_locale, p_add_defaults).operator String ();
224303}
@@ -604,6 +683,10 @@ void TranslationServer::_bind_methods() {
604683
605684 ClassDB::bind_method (D_METHOD (" get_loaded_locales" ), &TranslationServer::get_loaded_locales);
606685
686+ ClassDB::bind_method (D_METHOD (" format_number" , " number" , " locale" ), &TranslationServer::format_number);
687+ ClassDB::bind_method (D_METHOD (" get_percent_sign" , " locale" ), &TranslationServer::get_percent_sign);
688+ ClassDB::bind_method (D_METHOD (" parse_number" , " number" , " locale" ), &TranslationServer::parse_number);
689+
607690 ClassDB::bind_method (D_METHOD (" is_pseudolocalization_enabled" ), &TranslationServer::is_pseudolocalization_enabled);
608691 ClassDB::bind_method (D_METHOD (" set_pseudolocalization_enabled" , " enabled" ), &TranslationServer::set_pseudolocalization_enabled);
609692 ClassDB::bind_method (D_METHOD (" reload_pseudolocalization" ), &TranslationServer::reload_pseudolocalization);
0 commit comments