Simple fast-track text API?

One use case that pops up from time to time has been building text in code from bits and pieces. Smart strings can handle some cases, but sometimes it’s just easier to use StringBuilder. Would it be possible to have a convenient and efficient way of getting a translation when the assets are guaranteed to exist (i.e. preloading has been completed), or some documentation on how to achieve that?

As far as I understand, this is the closest way to do it at the moment:

    private LocalizedString _helper;

    public string GetText(string key)
    {
        if (string.IsNullOrEmpty(key)) return null;

        if (_helper == null) _helper = new LocalizedString();
        _helper.TableReference = "Strings";
        _helper.TableEntryReference = key;
        return _helper.GetLocalizedString().Result;
    }

Other than that, I’m happy to see that the feature set of this package is looking good, at least when export/import gets implemented.

Hey.

You have a few options at the moment.

// Get the string from source in an async way:
var operation = LocalizationSettings.StringDatabase.GetLocalizedStringAsync("Strings", key);
// If the table is already loaded then this will be immediately available.
return operation.Result;

or what you would likely need is this:

// get the table
var tableOp = LocalizationSettings.StringDatabase.GetTableAsync("Strings");
// Wait for the table

// Once ready you can hold a reference to the table and use it directly.
var table = tableOp.Result;
var entry = table.GetEntry(key);
return entry.GetLocalizedString();

Ill make a note to add some examples and further documentation on this
Accessing the table entry is also good if you want to embed any metadata for later use.

1 Like