From 983190b9a0b9da9ec94e9f51cdad20c83e8f5ee3 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 19 Apr 2021 16:24:14 +0200 Subject: [PATCH] Add `LocalizedString` --- Source/Engine/Localization/Localization.cpp | 67 ++++++++++ Source/Engine/Localization/LocalizedString.cs | 103 ++++++++++++++++ Source/Engine/Localization/LocalizedString.h | 114 ++++++++++++++++++ Source/Engine/Serialization/JsonConverters.cs | 69 +++++++++++ Source/Engine/Serialization/JsonSerializer.cs | 1 + 5 files changed, 354 insertions(+) create mode 100644 Source/Engine/Localization/LocalizedString.cs create mode 100644 Source/Engine/Localization/LocalizedString.h diff --git a/Source/Engine/Localization/Localization.cpp b/Source/Engine/Localization/Localization.cpp index 950544def..2c5d0bfab 100644 --- a/Source/Engine/Localization/Localization.cpp +++ b/Source/Engine/Localization/Localization.cpp @@ -2,6 +2,7 @@ #include "Localization.h" #include "CultureInfo.h" +#include "LocalizedString.h" #include "LocalizationSettings.h" #include "Engine/Core/Log.h" #include "Engine/Core/Config/GameSettings.h" @@ -46,6 +47,72 @@ void LocalizationSettings::Deserialize(DeserializeStream& stream, ISerializeModi DESERIALIZE(LocalizedStringTables); } +LocalizedString::LocalizedString(const LocalizedString& other) + : Id(other.Id) + , Value(other.Value) +{ +} + +LocalizedString::LocalizedString(LocalizedString&& other) noexcept + : Id(MoveTemp(other.Id)) + , Value(MoveTemp(other.Value)) +{ +} + +LocalizedString::LocalizedString(const StringView& value) + : Value(value) +{ +} + +LocalizedString::LocalizedString(String&& value) noexcept + : Value(MoveTemp(value)) +{ +} + +LocalizedString& LocalizedString::operator=(const LocalizedString& other) +{ + if (this != &other) + { + Id = other.Id; + Value = other.Value; + } + return *this; +} + +LocalizedString& LocalizedString::operator=(LocalizedString&& other) noexcept +{ + if (this != &other) + { + Id = MoveTemp(other.Id); + Value = MoveTemp(other.Value); + } + return *this; +} + +LocalizedString& LocalizedString::operator=(const StringView& value) +{ + Id.Clear(); + Value = value; + return *this; +} + +LocalizedString& LocalizedString::operator=(String&& value) noexcept +{ + Id.Clear(); + Value = MoveTemp(value); + return *this; +} + +String LocalizedString::ToString() const +{ + return Localization::GetString(Id, Value); +} + +String LocalizedString::ToStringPlural(int32 n) const +{ + return Localization::GetPluralString(Id, n, Value); +} + void LocalizationService::OnLocalizationChanged() { PROFILE_CPU(); diff --git a/Source/Engine/Localization/LocalizedString.cs b/Source/Engine/Localization/LocalizedString.cs new file mode 100644 index 000000000..a5cdb13fc --- /dev/null +++ b/Source/Engine/Localization/LocalizedString.cs @@ -0,0 +1,103 @@ +// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. + +using System; + +namespace FlaxEngine +{ + partial class LocalizedString : IEquatable, IEquatable, IComparable, IComparable, IComparable + { + /// + /// Initializes a new instance of the class. + /// + /// The value. + public LocalizedString(string value) + { + Value = value; + } + + /// + /// Gets the localized plural string for the current language by using string id lookup. + /// + /// The value count for plural message selection. + /// The localized text. + public string ToStringPlural(int n) + { + return string.IsNullOrEmpty(Value) ? Localization.GetPluralString(Id, n) : Value; + } + + /// + /// Implicit converter of into . + /// + /// The localized string. + /// The string. + public static implicit operator string(LocalizedString str) + { + return str.ToString(); + } + + /// + /// Implicit converter of into . + /// + /// The string. + /// The localized string. + public static implicit operator LocalizedString(string str) + { + return new LocalizedString(str); + } + + /// + public int CompareTo(object obj) + { + if (obj is string asString) + return CompareTo(asString); + if (obj is LocalizedString asLocalizedString) + return CompareTo(asLocalizedString); + return 0; + } + + /// + public bool Equals(LocalizedString other) + { + return Id == other.Id && Value == other.Value; + } + + /// + public bool Equals(string other) + { + return Value == other || Localization.GetString(Id) == other; + } + + /// + public int CompareTo(LocalizedString other) + { + return string.Compare(ToString(), ToString(), StringComparison.Ordinal); + } + + /// + public int CompareTo(string other) + { + return string.Compare(ToString(), other, StringComparison.Ordinal); + } + + /// + public override bool Equals(object obj) + { + return ReferenceEquals(this, obj) || obj is LocalizedString other && Equals(other); + } + + /// + public override int GetHashCode() + { + unchecked + { + return ((Id != null ? Id.GetHashCode() : 0) * 397) ^ (Value != null ? Value.GetHashCode() : 0); + } + } + + /// + public override string ToString() + { + return string.IsNullOrEmpty(Value) ? Localization.GetString(Id) : Value; + } + } +} diff --git a/Source/Engine/Localization/LocalizedString.h b/Source/Engine/Localization/LocalizedString.h new file mode 100644 index 000000000..7646029df --- /dev/null +++ b/Source/Engine/Localization/LocalizedString.h @@ -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" + +/// +/// The string container that supports using localized text. +/// +API_CLASS(Sealed) class FLAXENGINE_API LocalizedString +{ +DECLARE_SCRIPTING_TYPE_MINIMAL(LocalizedString); +public: + /// + /// The localized string identifier. Used to lookup text value for a current language (via ). + /// + API_FIELD() String Id; + + /// + /// The overriden string value to use. If empty, the localized string will be used. + /// + 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); diff --git a/Source/Engine/Serialization/JsonConverters.cs b/Source/Engine/Serialization/JsonConverters.cs index 4b5192da5..4403487a0 100644 --- a/Source/Engine/Serialization/JsonConverters.cs +++ b/Source/Engine/Serialization/JsonConverters.cs @@ -219,6 +219,75 @@ namespace FlaxEngine.Json public override bool CanWriteDiff => true; } + /// + /// Serialize LocalizedString as inlined text is not using localization (Id member is empty). + /// + /// + internal class LocalizedStringConverter : JsonConverter + { + /// + public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var str = (LocalizedString)value; + if (string.IsNullOrEmpty(str.Id)) + { + writer.WriteValue(str.Value); + } + else + { + writer.WriteStartObject(); + writer.WritePropertyName("Id"); + writer.WriteValue(str.Id); + writer.WritePropertyName("Value"); + writer.WriteValue(str.Value); + writer.WriteEndObject(); + } + } + + /// + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var str = existingValue as LocalizedString ?? new LocalizedString(); + if (reader.TokenType == JsonToken.String) + { + str.Id = null; + str.Value = (string)reader.Value; + } + else if (reader.TokenType == JsonToken.StartObject) + { + while (reader.Read()) + { + switch (reader.TokenType) + { + case JsonToken.PropertyName: + { + var propertyName = (string)reader.Value; + switch (propertyName) + { + case "Id": + str.Id = reader.ReadAsString(); + break; + case "Value": + str.Value = reader.ReadAsString(); + break; + } + break; + } + case JsonToken.Comment: break; + default: return str; + } + } + } + return str; + } + + /// + public override bool CanConvert(Type objectType) + { + return objectType == typeof(LocalizedString); + } + } + /* /// /// Serialize Guid values using `N` format diff --git a/Source/Engine/Serialization/JsonSerializer.cs b/Source/Engine/Serialization/JsonSerializer.cs index 7d17a6ea0..a89ade8ef 100644 --- a/Source/Engine/Serialization/JsonSerializer.cs +++ b/Source/Engine/Serialization/JsonSerializer.cs @@ -83,6 +83,7 @@ namespace FlaxEngine.Json settings.Converters.Add(new SoftObjectReferenceConverter()); settings.Converters.Add(new MarginConverter()); settings.Converters.Add(new VersionConverter()); + settings.Converters.Add(new LocalizedStringConverter()); //settings.Converters.Add(new GuidConverter()); return settings; }