Add string localization API
This commit is contained in:
@@ -153,3 +153,41 @@ void Localization::SetCurrentLanguageCulture(const CultureInfo& value)
|
|||||||
Instance.CurrentLanguage = value;
|
Instance.CurrentLanguage = value;
|
||||||
Instance.OnLocalizationChanged();
|
Instance.OnLocalizationChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String Localization::GetString(const String& id, const String& fallback)
|
||||||
|
{
|
||||||
|
String result;
|
||||||
|
for (auto& e : Instance.LocalizedStringTables)
|
||||||
|
{
|
||||||
|
const auto table = e.Get();
|
||||||
|
const auto messages = table ? table->Entries.TryGet(id) : nullptr;
|
||||||
|
if (messages && messages->Count() != 0)
|
||||||
|
{
|
||||||
|
result = messages->At(0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (result.IsEmpty())
|
||||||
|
result = fallback;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
String Localization::GetPluralString(const String& id, int32 n, const String& fallback)
|
||||||
|
{
|
||||||
|
CHECK_RETURN(n >= 1, fallback);
|
||||||
|
n--;
|
||||||
|
String result;
|
||||||
|
for (auto& e : Instance.LocalizedStringTables)
|
||||||
|
{
|
||||||
|
const auto table = e.Get();
|
||||||
|
const auto messages = table ? table->Entries.TryGet(id) : nullptr;
|
||||||
|
if (messages && messages->Count() > n)
|
||||||
|
{
|
||||||
|
result = messages->At(n);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (result.IsEmpty())
|
||||||
|
result = fallback;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|||||||
@@ -41,4 +41,22 @@ public:
|
|||||||
/// Occurs when current culture or language gets changed. Can be used to refresh UI to reflect language changes.
|
/// Occurs when current culture or language gets changed. Can be used to refresh UI to reflect language changes.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
API_EVENT() static Delegate<> LocalizationChanged;
|
API_EVENT() static Delegate<> LocalizationChanged;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the localized string for the current language by using string id lookup.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The message identifier.</param>
|
||||||
|
/// <param name="fallback">The optional fallback string value to use if localized string is missing.</param>
|
||||||
|
/// <returns>The localized text.</returns>
|
||||||
|
API_FUNCTION() static String GetString(const String& id, const String& fallback = String::Empty);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the localized plural string for the current language by using string id lookup.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The message identifier.</param>
|
||||||
|
/// <param name="n">The value count for plural message selection.</param>
|
||||||
|
/// <param name="fallback">The optional fallback string value to use if localized string is missing.</param>
|
||||||
|
/// <returns>The localized text.</returns>
|
||||||
|
API_FUNCTION() static String GetPluralString(const String& id, int32 n, const String& fallback = String::Empty);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user