Add Localization.GetLocales to list all languages in a game

This commit is contained in:
2026-04-08 09:46:59 +02:00
parent 737cac559c
commit 79d1d0c109
3 changed files with 65 additions and 43 deletions
@@ -169,6 +169,50 @@ String LocalizedString::ToStringPlural(int32 n) const
return Localization::GetPluralString(Id, n, Value); return Localization::GetPluralString(Id, n, Value);
} }
void Serialization::Serialize(ISerializable::SerializeStream& stream, const LocalizedString& v, const void* otherObj)
{
if (v.Id.IsEmpty())
{
stream.String(v.Value);
}
else
{
stream.StartObject();
stream.JKEY("Id");
stream.String(v.Id);
if (v.Value.HasChars())
{
stream.JKEY("Value");
stream.String(v.Value);
}
stream.EndObject();
}
}
void Serialization::Deserialize(ISerializable::DeserializeStream& stream, LocalizedString& v, ISerializeModifier* modifier)
{
if (stream.IsString())
{
v.Id = String::Empty;
v.Value = stream.GetText();
}
else if (stream.IsObject())
{
auto e = SERIALIZE_FIND_MEMBER(stream, "Id");
if (e != stream.MemberEnd())
v.Id.SetUTF8(e->value.GetString(), e->value.GetStringLength());
e = SERIALIZE_FIND_MEMBER(stream, "Value");
if (e != stream.MemberEnd())
v.Value.SetUTF8(e->value.GetString(), e->value.GetStringLength());
else if (v.Id.HasChars())
v.Value.Clear();
}
else
{
v = LocalizedString();
}
}
void LocalizationService::OnLocalizationChanged() void LocalizationService::OnLocalizationChanged()
{ {
PROFILE_CPU(); PROFILE_CPU();
@@ -346,3 +390,16 @@ String Localization::GetPluralString(const String& id, int32 n, const String& fa
const String& format = Instance.Get(id, n - 1, fallback); const String& format = Instance.Get(id, n - 1, fallback);
return String::Format(format.GetText(), n); return String::Format(format.GetText(), n);
} }
Array<String> Localization::GetLocales()
{
Array<String> result;
auto& settings = *LocalizationSettings::Get();
for (auto& e : settings.LocalizedStringTables)
{
auto table = e.Get();
if (table && !table->WaitForLoaded())
result.AddUnique(table->Locale);
}
return result;
}
@@ -59,4 +59,10 @@ public:
/// <param name="fallback">The optional fallback string value to use if localized string is missing.</param> /// <param name="fallback">The optional fallback string value to use if localized string is missing.</param>
/// <returns>The localized text.</returns> /// <returns>The localized text.</returns>
API_FUNCTION() static String GetPluralString(const String& id, int32 n, const String& fallback = String::Empty); API_FUNCTION() static String GetPluralString(const String& id, int32 n, const String& fallback = String::Empty);
/// <summary>
/// Gets the list of unique languages (locale names such as 'pl-PL') defined in project in Localized String Tables set in Localization Settings. Can be used to display all languages available in game.
/// </summary>
/// <returns>The list of unique languages (locale names such as 'pl-PL').</returns>
API_FUNCTION() static Array<String, HeapAllocation> GetLocales();
}; };
+2 -43
View File
@@ -71,49 +71,8 @@ namespace Serialization
return !otherObj || v != *(LocalizedString*)otherObj; return !otherObj || v != *(LocalizedString*)otherObj;
} }
inline void Serialize(ISerializable::SerializeStream& stream, const LocalizedString& v, const void* otherObj) FLAXENGINE_API void Serialize(ISerializable::SerializeStream& stream, const LocalizedString& v, const void* otherObj);
{ FLAXENGINE_API void Deserialize(ISerializable::DeserializeStream& stream, LocalizedString& v, ISerializeModifier* modifier);
if (v.Id.IsEmpty())
{
stream.String(v.Value);
}
else
{
stream.StartObject();
stream.JKEY("Id");
stream.String(v.Id);
if (v.Value.HasChars())
{
stream.JKEY("Value");
stream.String(v.Value);
}
stream.EndObject();
}
}
inline void Deserialize(ISerializable::DeserializeStream& stream, LocalizedString& v, ISerializeModifier* modifier)
{
if (stream.IsString())
{
v.Id = String::Empty;
v.Value = stream.GetText();
}
else if (stream.IsObject())
{
auto e = SERIALIZE_FIND_MEMBER(stream, "Id");
if (e != stream.MemberEnd())
v.Id.SetUTF8(e->value.GetString(), e->value.GetStringLength());
e = SERIALIZE_FIND_MEMBER(stream, "Value");
if (e != stream.MemberEnd())
v.Value.SetUTF8(e->value.GetString(), e->value.GetStringLength());
else if (v.Id.HasChars())
v.Value.Clear();
}
else
{
v = LocalizedString();
}
}
} }
DEFINE_DEFAULT_FORMATTING_VIA_TO_STRING(LocalizedString); DEFINE_DEFAULT_FORMATTING_VIA_TO_STRING(LocalizedString);