DateTime, casing and CultureInfo questions

I’m implementing localization on a project retroactively, so I’m looking for ways to use the package with the least amount of friction on the existing codebase. I’m running into a couple of things that I’m unsure about after reading through the documentation and browsing the web.

Is it valid to use the default system DateTime formatter to format date times with Smart Strings?
I have tested this and it seems to work. It also changes to the correct formatting when I switch the locale in the Unity Editor.

However, what happens when you supply a locale to your game for which the .NET culture info is not present on the user’s system, and the system DateTime formatter doesn’t know what to do? (Or is Unity automatically installing the correct CultureInfo with the locale? Or am I completely misunderstanding how this works?)

I wonder if this is a viable way to format dates and times, since the docs say that the SmartString TimeFormatter only supports english this is not an option (our game heavily depends on dates and times being communicated clearly to the player).

Anoher question, IF I can rely on the system’s CultureInfo to work correctly alongside the selected Locale from the localization package. Say that I want to convert some string to upper case in code. Would something like this work?

string someUpperCaseString = someLowerCaseString.ToUpper(LocalizationSettings.SelectedLocale.Identifier.CultureInfo);

This would also depend on if the .NET culture info works in tandem with Unity.
I know I can use SmartStrings reflection ToUpper, but this would require separate table entries for variants of the same text in upper case.

Any help would be greatly appreciated!

You may be overthinking this. .Net supports a lot of languages, unless you plan to support some very niche languages its unlikely you will encounter a missing locale and missing date time format support.
I’m not actually sure what would happen if the CultureInfo was missing, I believe it would use the default system culture but cant say for certain.

You could check if the CultureInfo is null in the locale and provide a fallback formatter:

var currentLocale = LocalizationSettings.SelectedLocale;
if (currentLocale.Identifier.CultureInfo  == null)
{
    // Fallback to English
    currentLocale.CustomFormatterCode = "en";
    currentLocale.UseCustomFormatter= true;
}

You can also directly apply a formatter to a locale.

Yes this will work, just check that CultureInfo is not null to be safe.

1 Like