Localizing Numbers

Godot Version

4.3 Stable

I’m in the process of localizing my game, and I’ve got various places where I have various numbers I’d like to localize that have decimals in them. If I do:

print(TextServerManager.get_primary_interface().format_number("3.141592", "de"))

I’d expect to get:

3,141592

But I get:

3.141592

Am I doing this wrong? How does one internationalize numbers properly in Godot?

Hi,

Never done that before, and after doing a few researches and testing, it seems that it’s not working in gdscript? I’m definitely not sure about it, hopefully someone will correct me, but if that can be done, I don’t see how. :sweat_smile:


I guess that’s not an ideal solution, but if you don’t want to spend time on implementing a custom function, you could use a C# method and call it from gdscript.

public static string Test(double number)
{
    string result = number.ToString("n", System.Globalization.CultureInfo.GetCultureInfo("de"));
    Console.WriteLine(result);
    return result;
}

// Output:
// Test(3.1415); --> 3,142
// Test(4500.34); --> 4.500,340

Never done a method call from gdscript to C#, only the opposite, but from what I’ve read, it’s doable fairly easily. No idea of the performance though.

Hope that helps, despite not being a perfect answer!

I’m actually generating my level data in an external C program, so I could potentially do it there if i had to. If I can somehow convince Godot to do it for me, though, I’d prefer that. :slight_smile:

2 Likes

Couldn’t you just use a replace on the string?

As the TextServer.format_number() documentation says:

Converts a number from the Western Arabic (0..9) to the numeral systems used in language.

It converts a number like 123 to the numeral system in another language. More info about numeral systems here Numeral system - Wikipedia

Here’s the implementation of that function godot/modules/text_server_adv/text_server_adv.cpp at 09fcbb86459498985a910cdd1684844a1968dc96 · godotengine/godot · GitHub

I don’t think the functionality you need is implemented. You could open a proposal for that.

Sadly it’s more complicated.
Take the number 5877.34, for instance. In some languages, it would localized as 5.877,34. In mine, it would be 5 877,34. Depending on the language, the result can vary and a simple replace would not be enough.

1 Like

Oddly, if I use "zh" or "ja" as language arguments, I still get out the same string I fed in. I wonder if I’m calling it wrong.

I think, format_number() only deals with completely different numeral systems (Arabic, Persian etc, but not as different as Japanese :slight_smile: ) and that’s why it returns the same string. I don’t see “de”, “zh” or “ja” in the description provided by mrcdk

I guess I’ll build my own functionality for this. I might be able to farm it off to an external executable, or I might just write it inline. I may put up a proposal for it, but at the moment I’m keeping the game on Godot 4.3.stable because I’m close to shipping.

Edit: Localizing numeric strings/values (decimal and separators). · Issue #12429 · godotengine/godot-proposals · GitHub

1 Like