From 7448383079d5b4567b103b2c708272a3083e3573 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 16 Apr 2021 15:39:31 +0200 Subject: [PATCH] Add LocalizedStringTable --- .../Proxy/LocalizedStringTableProxy.cs | 24 +++ .../Editor/Modules/ContentDatabaseModule.cs | 1 + .../Assets/LocalizedStringTableWindow.cs | 102 +++++++++++++ .../Engine/Localization/Localization.Build.cs | 5 + .../Localization/LocalizedStringTable.cpp | 138 ++++++++++++++++++ .../Localization/LocalizedStringTable.h | 57 ++++++++ 6 files changed, 327 insertions(+) create mode 100644 Source/Editor/Content/Proxy/LocalizedStringTableProxy.cs create mode 100644 Source/Editor/Windows/Assets/LocalizedStringTableWindow.cs create mode 100644 Source/Engine/Localization/LocalizedStringTable.cpp create mode 100644 Source/Engine/Localization/LocalizedStringTable.h diff --git a/Source/Editor/Content/Proxy/LocalizedStringTableProxy.cs b/Source/Editor/Content/Proxy/LocalizedStringTableProxy.cs new file mode 100644 index 000000000..2e15f9543 --- /dev/null +++ b/Source/Editor/Content/Proxy/LocalizedStringTableProxy.cs @@ -0,0 +1,24 @@ +// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. + +using FlaxEditor.Windows; +using FlaxEditor.Windows.Assets; +using FlaxEngine; + +namespace FlaxEditor.Content +{ + /// + /// proxy. + /// + /// + public class LocalizedStringTableProxy : JsonAssetProxy + { + /// + public override EditorWindow Open(Editor editor, ContentItem item) + { + return new LocalizedStringTableWindow(editor, (JsonAssetItem)item); + } + + /// + public override string TypeName => "FlaxEngine.LocalizedStringTable"; + } +} diff --git a/Source/Editor/Modules/ContentDatabaseModule.cs b/Source/Editor/Modules/ContentDatabaseModule.cs index 2d6b179f9..e55501e4b 100644 --- a/Source/Editor/Modules/ContentDatabaseModule.cs +++ b/Source/Editor/Modules/ContentDatabaseModule.cs @@ -923,6 +923,7 @@ namespace FlaxEditor.Modules Proxy.Add(new SkeletonMaskProxy()); Proxy.Add(new GameplayGlobalsProxy()); Proxy.Add(new VisualScriptProxy()); + Proxy.Add(new LocalizedStringTableProxy()); Proxy.Add(new FileProxy()); Proxy.Add(new SpawnableJsonAssetProxy()); diff --git a/Source/Editor/Windows/Assets/LocalizedStringTableWindow.cs b/Source/Editor/Windows/Assets/LocalizedStringTableWindow.cs new file mode 100644 index 000000000..7cd2472fb --- /dev/null +++ b/Source/Editor/Windows/Assets/LocalizedStringTableWindow.cs @@ -0,0 +1,102 @@ +// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. + +using System.Collections.Generic; +using FlaxEditor.Content; +using FlaxEditor.CustomEditors; +using FlaxEditor.GUI; +using FlaxEngine; +using FlaxEngine.GUI; + +namespace FlaxEditor.Windows.Assets +{ + /// + /// Editor window to view/modify asset. + /// + /// + /// + public sealed class LocalizedStringTableWindow : AssetEditorWindowBase + { + private readonly CustomEditorPresenter _presenter; + private readonly ToolStripButton _saveButton; + private Proxy _proxy; + + private class Proxy + { + [EditorOrder(0), Tooltip("The locale of the localized string table (eg. pl-PL).")] + public string Locale; + + [EditorOrder(10), Tooltip("The string table. Maps the message id into the localized text. For plural messages the list contains separate items for value numbers.")] + public Dictionary Entries; + } + + /// + public LocalizedStringTableWindow(Editor editor, AssetItem item) + : base(editor, item) + { + // Toolstrip + _saveButton = (ToolStripButton)_toolstrip.AddButton(editor.Icons.Save32, Save).LinkTooltip("Save"); + + // Panel + var panel = new Panel(ScrollBars.Vertical) + { + AnchorPreset = AnchorPresets.StretchAll, + Offsets = new Margin(0, 0, _toolstrip.Bottom, 0), + Parent = this + }; + + // Properties + _presenter = new CustomEditorPresenter(null, "Loading..."); + _presenter.Panel.Parent = panel; + _presenter.Modified += MarkAsEdited; + } + + /// + public override void Save() + { + if (!IsEdited) + return; + + _asset.Locale = _proxy.Locale; + _asset.Entries = _proxy.Entries; + if (_asset.Save(_item.Path)) + { + Editor.LogError("Cannot save asset."); + return; + } + + ClearEditedFlag(); + } + + /// + protected override void UpdateToolstrip() + { + _saveButton.Enabled = IsEdited; + + base.UpdateToolstrip(); + } + + /// + protected override void OnAssetLoaded() + { + _proxy = new Proxy + { + Locale = _asset.Locale, + Entries = _asset.Entries, + }; + _presenter.Select(_proxy); + ClearEditedFlag(); + + base.OnAssetLoaded(); + } + + /// + public override void OnItemReimported(ContentItem item) + { + // Refresh the properties (will get new data in OnAssetLoaded) + _presenter.Deselect(); + ClearEditedFlag(); + + base.OnItemReimported(item); + } + } +} diff --git a/Source/Engine/Localization/Localization.Build.cs b/Source/Engine/Localization/Localization.Build.cs index b7b839bcc..7f730dcab 100644 --- a/Source/Engine/Localization/Localization.Build.cs +++ b/Source/Engine/Localization/Localization.Build.cs @@ -14,5 +14,10 @@ public class Localization : EngineModule base.Setup(options); options.PublicDependencies.Add("Scripting"); + + if (options.Target.IsEditor) + { + options.PrivateDependencies.Add("ContentImporters"); + } } } diff --git a/Source/Engine/Localization/LocalizedStringTable.cpp b/Source/Engine/Localization/LocalizedStringTable.cpp new file mode 100644 index 000000000..bb2b1e8a7 --- /dev/null +++ b/Source/Engine/Localization/LocalizedStringTable.cpp @@ -0,0 +1,138 @@ +// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. + +#include "LocalizedStringTable.h" +#include "Engine/Serialization/JsonTools.h" +#include "Engine/Serialization/SerializationFwd.h" +#include "Engine/Content/Factories/JsonAssetFactory.h" +#if USE_EDITOR +#include "Engine/ContentImporters/CreateJson.h" +#include "Engine/Serialization/JsonWriters.h" +#include "Engine/Threading/Threading.h" +#include "Engine/Core/Log.h" +#endif + +REGISTER_JSON_ASSET(LocalizedStringTable, "FlaxEngine.LocalizedStringTable", true); + +LocalizedStringTable::LocalizedStringTable(const SpawnParams& params, const AssetInfo* info) + : JsonAssetBase(params, info) +{ +} + +void LocalizedStringTable::AddString(const StringView& id, const StringView& value) +{ + auto& values = Entries[id]; + values.Resize(1); + values[0] = value; +} + +void LocalizedStringTable::AddPluralString(const StringView& id, const StringView& value, int32 n) +{ + CHECK(n >= 0 && n < 1024); + auto& values = Entries[id]; + values.Resize(Math::Max(values.Count(), n + 1)); + values[n] = value; +} + +#if USE_EDITOR + +bool LocalizedStringTable::Save(const StringView& path) +{ + // Validate state + if (WaitForLoaded()) + { + LOG(Error, "Asset loading failed. Cannot save it."); + return true; + } + if (IsVirtual() && path.IsEmpty()) + { + LOG(Error, "To save virtual asset asset you need to specify the target asset path location."); + return true; + } + + ScopeLock lock(Locker); + + // Serialize data + rapidjson_flax::StringBuffer outputData; + PrettyJsonWriter writerObj(outputData); + JsonWriter& writer = writerObj; + writer.StartObject(); + { + writer.JKEY("Locale"); + writer.String(Locale); + + writer.JKEY("Entries"); + writer.StartObject(); + for (auto& e : Entries) + { + writer.Key(e.Key); + if (e.Value.Count() == 1) + { + writer.String(e.Value[0]); + } + else + { + writer.StartArray(); + for (auto& q : e.Value) + writer.String(q); + writer.EndArray(); + } + } + writer.EndObject(); + } + writer.EndObject(); + + // Save asset + const bool saveResult = CreateJson::Create(path.HasChars() ? path : GetPath(), outputData, TypeName); + if (saveResult) + { + LOG(Error, "Cannot save \'{0}\'", ToString()); + return true; + } + + return false; +} + +#endif + +Asset::LoadResult LocalizedStringTable::loadAsset() +{ + // Base + auto result = JsonAssetBase::loadAsset(); + if (result != LoadResult::Ok || IsInternalType()) + return result; + + JsonTools::GetString(Locale, *Data, "Locale"); + const auto entriesMember = SERIALIZE_FIND_MEMBER((*Data), "Entries"); + if (entriesMember != Data->MemberEnd() && entriesMember->value.IsObject()) + { + Entries.EnsureCapacity(entriesMember->value.MemberCount()); + for (auto i = entriesMember->value.MemberBegin(); i != entriesMember->value.MemberEnd(); ++i) + { + const String key(i->name.GetText()); + auto& e = Entries[key]; + auto& value = i->value; + if (value.IsString()) + { + e.Resize(1); + e[0] = value.GetText(); + } + else if (value.IsArray()) + { + e.Resize(value.Size()); + for (int32 q = 0; q < e.Count(); q++) + e[q] = value[q].GetString(); + } + } + } + + return result; +} + +void LocalizedStringTable::unload(bool isReloading) +{ + // Base + JsonAssetBase::unload(isReloading); + + Locale.Clear(); + Entries.Clear(); +} diff --git a/Source/Engine/Localization/LocalizedStringTable.h b/Source/Engine/Localization/LocalizedStringTable.h new file mode 100644 index 000000000..53e5bd6a4 --- /dev/null +++ b/Source/Engine/Localization/LocalizedStringTable.h @@ -0,0 +1,57 @@ +// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. + +#pragma once + +#include "Engine/Content/JsonAsset.h" +#include "Engine/Core/Collections/Dictionary.h" + +/// +/// Contains localized strings table for a given culture. +/// +/// +API_CLASS(NoSpawn) class FLAXENGINE_API LocalizedStringTable : public JsonAssetBase +{ +DECLARE_ASSET_HEADER(LocalizedStringTable); +public: + /// + /// The locale of the localized string table (eg. pl-PL). + /// + API_FIELD() String Locale; + + /// + /// The string table. Maps the message id into the localized text. For plural messages the list contains separate items for value numbers. + /// + API_FIELD() Dictionary> Entries; + +public: + /// + /// Adds the localized string to the table. + /// + /// The message id. Used for lookups. + /// The localized text. + API_FUNCTION() void AddString(const StringView& id, const StringView& value); + + /// + /// Adds the localized plural string to the table. + /// + /// The message id. Used for lookups. + /// The localized text. + /// The plural value (0, 1, 2..). + API_FUNCTION() void AddPluralString(const StringView& id, const StringView& value, int32 n); + +#if USE_EDITOR + + /// + /// Saves this asset to the file. Supported only in Editor. + /// + /// The custom asset path to use for the saving. Use empty value to save this asset to its own storage location. Can be used to duplicate asset. Must be specified when saving virtual asset. + /// True if cannot save data, otherwise false. + API_FUNCTION() bool Save(const StringView& path = StringView::Empty); + +#endif + +protected: + // [JsonAssetBase] + LoadResult loadAsset() override; + void unload(bool isReloading) override; +};