diff --git a/Shared/SharedUI/Shared.Ui/Common/LocExtension.cs b/Shared/SharedUI/Shared.Ui/Common/LocExtension.cs index 53e13341c..b9b558c01 100644 --- a/Shared/SharedUI/Shared.Ui/Common/LocExtension.cs +++ b/Shared/SharedUI/Shared.Ui/Common/LocExtension.cs @@ -1,4 +1,9 @@ using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.IO; +using System.Text.Json; using System.Windows; using System.Windows.Markup; using Microsoft.Extensions.DependencyInjection; @@ -8,6 +13,10 @@ namespace Shared.Ui.Common { public class LocExtension : MarkupExtension { + private static readonly IReadOnlyDictionary s_emptyDesignerStrings = + new ReadOnlyDictionary(new Dictionary()); + private static readonly Lazy> s_designerStrings = new(LoadDesignerStrings); + public string Key { get; set; } public LocExtension(string key) @@ -17,9 +26,42 @@ public LocExtension(string key) public override object ProvideValue(IServiceProvider serviceProvider) { - var applicationServiceProivder = ((IAssetEditorMain)Application.Current).ServiceProvider; - var service = applicationServiceProivder.GetRequiredService(); - return service.Get(Key); + if (Application.Current is IAssetEditorMain appMain) + { + var applicationServiceProivder = appMain.ServiceProvider; + var service = applicationServiceProivder.GetRequiredService(); + return service.Get(Key); + } + + if (DesignerProperties.GetIsInDesignMode(new DependencyObject()) && + s_designerStrings.Value.TryGetValue(Key, out var value)) + return value; + + return Key; + } + + private static IReadOnlyDictionary LoadDesignerStrings() + { + try + { + var currentDirectory = Directory.GetCurrentDirectory(); + var directPath = Path.Combine(currentDirectory, "Language_En.json"); + var assetEditorPath = Path.Combine(currentDirectory, "AssetEditor", "Language_En.json"); + var filePath = File.Exists(directPath) ? directPath : assetEditorPath; + if (!File.Exists(filePath)) + return s_emptyDesignerStrings; + + var json = File.ReadAllText(filePath); + var strings = JsonSerializer.Deserialize>(json); + if (strings != null) + return new ReadOnlyDictionary(strings); + else + return s_emptyDesignerStrings; + } + catch + { + return s_emptyDesignerStrings; + } } } }