Add LocalizedString
This commit is contained in:
114
Source/Engine/Localization/LocalizedString.h
Normal file
114
Source/Engine/Localization/LocalizedString.h
Normal file
@@ -0,0 +1,114 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Engine/Core/Types/String.h"
|
||||
#include "Engine/Serialization/SerializationFwd.h"
|
||||
|
||||
/// <summary>
|
||||
/// The string container that supports using localized text.
|
||||
/// </summary>
|
||||
API_CLASS(Sealed) class FLAXENGINE_API LocalizedString
|
||||
{
|
||||
DECLARE_SCRIPTING_TYPE_MINIMAL(LocalizedString);
|
||||
public:
|
||||
/// <summary>
|
||||
/// The localized string identifier. Used to lookup text value for a current language (via <see cref="Localization::GetString"/>).
|
||||
/// </summary>
|
||||
API_FIELD() String Id;
|
||||
|
||||
/// <summary>
|
||||
/// The overriden string value to use. If empty, the localized string will be used.
|
||||
/// </summary>
|
||||
API_FIELD() String Value;
|
||||
|
||||
public:
|
||||
LocalizedString() = default;
|
||||
LocalizedString(const LocalizedString& other);
|
||||
LocalizedString(LocalizedString&& other) noexcept;
|
||||
LocalizedString(const StringView& value);
|
||||
LocalizedString(String&& value) noexcept;
|
||||
|
||||
LocalizedString& operator=(const LocalizedString& other);
|
||||
LocalizedString& operator=(LocalizedString&& other) noexcept;
|
||||
LocalizedString& operator=(const StringView& value);
|
||||
LocalizedString& operator=(String&& value) noexcept;
|
||||
|
||||
friend bool operator==(const LocalizedString& a, const LocalizedString& b)
|
||||
{
|
||||
return a.Id == b.Id && a.Value == b.Value;
|
||||
}
|
||||
|
||||
friend bool operator!=(const LocalizedString& a, const LocalizedString& b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
friend bool operator==(const LocalizedString& a, const StringView& b)
|
||||
{
|
||||
return a.Value == b || a.ToString() == b;
|
||||
}
|
||||
|
||||
friend bool operator!=(const LocalizedString& a, const StringView& b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
public:
|
||||
String ToString() const;
|
||||
String ToStringPlural(int32 n) const;
|
||||
};
|
||||
|
||||
inline uint32 GetHash(const LocalizedString& key)
|
||||
{
|
||||
return GetHash(key.ToString());
|
||||
}
|
||||
|
||||
namespace Serialization
|
||||
{
|
||||
inline bool ShouldSerialize(const LocalizedString& v, const void* otherObj)
|
||||
{
|
||||
return !otherObj || v != *(LocalizedString*)otherObj;
|
||||
}
|
||||
|
||||
inline void 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);
|
||||
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 = e->value.GetString();
|
||||
e = SERIALIZE_FIND_MEMBER(stream, "Value");
|
||||
if (e != stream.MemberEnd())
|
||||
v.Value = e->value.GetString();
|
||||
}
|
||||
else
|
||||
{
|
||||
v = LocalizedString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DEFINE_DEFAULT_FORMATTING_VIA_TO_STRING(LocalizedString);
|
||||
Reference in New Issue
Block a user