Skip to content

04. TLocalized.Get()

tetherscript edited this page Jan 31, 2025 · 1 revision

How do we translate this status message:

Loading {0}, please wait...

where we may use a String.Format() to fill in the {0} with a value such as a filename. We need to translate everything but the filename.

Solution

TLocalized.Get(string name, string hint, string value);

Dynamic text can't be found in a scan since the text to be translated is generated at runtime . If the debugger is attached to the target app, the TLocalized class loads \Translator\LocalizedGets.json at target app startup, adds any requested translations via .Get()'s, and saves the text when the app shuts down. This file is later used in the Translation process. Once the translation is complete, this same .Get() call will return the translated text.

Example

string translatedText = TLocalized.Get("LoadingFile", "@", "Loading {0}, please wait..."); // In en-DE (German), this call would return "Lade {0}, bitte warten..."

string displayText = String.Format(translatedText, "myFile.text"); // here we fill in the placeholder with a filename

The {0} is a placeholder for a parameter in a String.Format() call. OpenAI returns the placeholder in the correct location, though you may need to adjust the hint to let it know what to do with placeholders. You can use the same hint tokens as with .xaml elements.

The .Get() name parameter has a strict format definition as described in TLocalizedDef.IsValidXamlIdentifier(). So you can't use periods in the name, for example.

Using the class in the target project

You can find \Common\TLocalized.cs in this Translator app and copy it or link to it in your target app. It has a TeeLocalized namespace.

Note

The names are weird to hopefully avoid naming collisions when you include this class in your target project. It's named TLocalized instead of Localized because that's how it should be—at least in the old Delphi days, which ironically was written by the same guy who made C#. Small world.

More examples

In the sample apps, there are calls to TLocalized.Get() that dump the results to the log.

Is TLocalized Trimming-compatible?

Yes, it is compatible with trimming since it does not use reflection for its JSON calls.

Clone this wiki locally