Parcourir la source

ColorsViewModel localized and added params

Krzysztof Krysiński il y a 2 ans
Parent
commit
62f20addd4

+ 34 - 0
src/PixiEditor/Data/Localization/Languages/en.json

@@ -133,6 +133,40 @@
   "COPY_COLOR_SECONDARY_RGB_DESCRIPTIVE": "Copy secondary color as RGB code",
 
   "PALETTE_COLORS": "Palette Colors",
+  "REPLACE_SECONDARY_BY_PRIMARY": "Replace secondary color by primary",
+  "REPLACE_SECONDARY_BY_PRIMARY_DESCRIPTIVE": "Replace the secondary color by the primary color",
+  "REPLACE_PRIMARY_BY_SECONDARY": "Replace primary color by secondary",
+  "REPLACE_PRIMARY_BY_SECONDARY_DESCRIPTIVE": "Replace the primary color by the secondary color",
+  "OPEN_PALETTE_BROWSER": "Open palette browser",
+  "OVERWRITE_PALETTE_CONSENT": "Palette '{0}' already exists, do you want to overwrite it?",
+  "PALETTE_EXISTS": "Palette already exists",
+  "REPLACE_PALETTE_CONSENT": "Replace current palette with selected one?",
+  "REPLACE_PALETTE": "Replace current palette",
+
+  "SELECT_COLOR_1": "Select color 1",
+  "SELECT_COLOR_2": "Select color 2",
+  "SELECT_COLOR_3": "Select color 3",
+  "SELECT_COLOR_4": "Select color 4",
+  "SELECT_COLOR_5": "Select color 5",
+  "SELECT_COLOR_6": "Select color 6",
+  "SELECT_COLOR_7": "Select color 7",
+  "SELECT_COLOR_8": "Select color 8",
+  "SELECT_COLOR_9": "Select color 9",
+  "SELECT_COLOR_10": "Select color 10",
+
+  "SELECT_COLOR_1_DESCRIPTIVE": "Select the first color in the palette",
+  "SELECT_COLOR_2_DESCRIPTIVE": "Select the second color in the palette",
+  "SELECT_COLOR_3_DESCRIPTIVE": "Select the third color in the palette",
+  "SELECT_COLOR_4_DESCRIPTIVE": "Select the fourth color in the palette",
+  "SELECT_COLOR_5_DESCRIPTIVE": "Select the fifth color in the palette",
+  "SELECT_COLOR_6_DESCRIPTIVE": "Select the sixth color in the palette",
+  "SELECT_COLOR_7_DESCRIPTIVE": "Select the seventh color in the palette",
+  "SELECT_COLOR_8_DESCRIPTIVE": "Select the eighth color in the palette",
+  "SELECT_COLOR_9_DESCRIPTIVE": "Select the ninth color in the palette",
+  "SELECT_COLOR_10_DESCRIPTIVE": "Select the tenth color in the palette",
+
+  "SWAP_COLORS": "Swap colors",
+  "SWAP_COLORS_DESCRIPTIVE": "Swap primary and secondary colors",
 
   "SEARCH": "Search",
 

+ 34 - 9
src/PixiEditor/Localization/LocalizedString.cs

@@ -15,43 +15,68 @@ public struct LocalizedString
     }
     public string Value { get; private set; }
 
+    public object[] Parameters { get; set; }
+
     public LocalizedString(string key)
     {
         Key = key;
     }
 
+    public LocalizedString(string key, params object[] parameters)
+    {
+        Parameters = parameters;
+        Key = key;
+    }
+
     public override string ToString()
     {
         return Value;
     }
 
-    private static string GetValue(string key)
+    private string GetValue(string localizationKey)
     {
-        if (string.IsNullOrEmpty(key))
+        if (string.IsNullOrEmpty(localizationKey))
         {
-            return key;
+            return localizationKey;
         }
         
         ILocalizationProvider localizationProvider = ILocalizationProvider.Current;
         if (localizationProvider?.LocalizationData == null)
         {
-            return key;
+            return localizationKey;
         }
 
-        if (!localizationProvider.CurrentLanguage.Locale.ContainsKey(key))
+        if (!localizationProvider.CurrentLanguage.Locale.ContainsKey(localizationKey))
         {
             Language defaultLanguage = localizationProvider.DefaultLanguage;
 
-            if (localizationProvider.CurrentLanguage == defaultLanguage || !defaultLanguage.Locale.ContainsKey(key))
+            if (localizationProvider.CurrentLanguage == defaultLanguage || !defaultLanguage.Locale.ContainsKey(localizationKey))
             {
-                return key;
+                return localizationKey;
             }
 
-            return defaultLanguage.Locale[key];
+            return ApplyParameters(defaultLanguage.Locale[localizationKey]);
         }
 
 
-        return ILocalizationProvider.Current.CurrentLanguage.Locale[key];
+        return ApplyParameters(ILocalizationProvider.Current.CurrentLanguage.Locale[localizationKey]);
+    }
+
+    private string ApplyParameters(string value)
+    {
+        if (Parameters == null || Parameters.Length == 0)
+        {
+            return value;
+        }
+
+        try
+        {
+            return string.Format(value, Parameters);
+        }
+        catch (FormatException)
+        {
+            return value;
+        }
     }
 
     public static implicit operator LocalizedString(string key) => new(key);

+ 3 - 2
src/PixiEditor/Models/Dialogs/ConfirmationDialog.cs

@@ -1,4 +1,5 @@
-using PixiEditor.Models.Enums;
+using PixiEditor.Localization;
+using PixiEditor.Models.Enums;
 using PixiEditor.Views;
 using PixiEditor.Views.Dialogs;
 
@@ -6,7 +7,7 @@ namespace PixiEditor.Models.Dialogs;
 
 internal static class ConfirmationDialog
 {
-    public static ConfirmationType Show(string message, string title)
+    public static ConfirmationType Show(LocalizedString message, LocalizedString title)
     {
         ConfirmationPopup popup = new ConfirmationPopup
         {

+ 19 - 17
src/PixiEditor/ViewModels/SubViewModels/Main/ColorsViewModel.cs

@@ -3,6 +3,7 @@ using System.Windows.Input;
 using System.Windows.Media;
 using Microsoft.Extensions.DependencyInjection;
 using PixiEditor.Helpers;
+using PixiEditor.Localization;
 using PixiEditor.Models.Commands.Attributes.Commands;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
@@ -82,8 +83,8 @@ internal class ColorsViewModel : SubViewModel<ViewModelMain>
         doc.Operations.ReplaceColor(colors.oldColor, colors.newColor);
     }
 
-    [Command.Basic("PixiEditor.Colors.ReplaceSecondaryByPrimaryColor", false, "Replace secondary color by primary", "Replace the secondary color by the primary color", IconEvaluator = "PixiEditor.Colors.ReplaceColorIcon")]
-    [Command.Basic("PixiEditor.Colors.ReplacePrimaryBySecondaryColor", true, "Replace primary color by secondary", "Replace the primary color by the secondary color", IconEvaluator = "PixiEditor.Colors.ReplaceColorIcon")]
+    [Command.Basic("PixiEditor.Colors.ReplaceSecondaryByPrimaryColor", false, "REPLACE_SECONDARY_BY_PRIMARY", "REPLACE_SECONDARY_BY_PRIMARY", IconEvaluator = "PixiEditor.Colors.ReplaceColorIcon")]
+    [Command.Basic("PixiEditor.Colors.ReplacePrimaryBySecondaryColor", true, "REPLACE_PRIMARY_BY_SECONDARY", "REPLACE_PRIMARY_BY_SECONDARY_DESCRIPTIVE", IconEvaluator = "PixiEditor.Colors.ReplaceColorIcon")]
     public void ReplaceColors(bool replacePrimary)
     {
         var oldColor = replacePrimary ? PrimaryColor : SecondaryColor;
@@ -130,7 +131,7 @@ internal class ColorsViewModel : SubViewModel<ViewModelMain>
         await ImportLospecPalette();
     }
 
-    [Command.Basic("PixiEditor.Colors.OpenPaletteBrowser", "Open Palette Browser", "Open Palette Browser", CanExecute = "PixiEditor.HasDocument", IconPath = "Globe.png")]
+    [Command.Basic("PixiEditor.Colors.OpenPaletteBrowser", "OPEN_PALETTE_BROWSER", "OPEN_PALETTE_BROWSER", CanExecute = "PixiEditor.HasDocument", IconPath = "Globe.png")]
     public void OpenPalettesBrowser() 
     {
         var doc = Owner.DocumentManagerSubViewModel.ActiveDocument;
@@ -155,7 +156,8 @@ internal class ColorsViewModel : SubViewModel<ViewModelMain>
                 if (LocalPalettesFetcher.PaletteExists(palette.Name))
                 {
                     var consent = ConfirmationDialog.Show(
-                        $"Palette '{palette.Name}' already exists, do you want to overwrite it?", "Palette exists");
+                        new LocalizedString("OVERWRITE_PALETTE_CONSENT", palette.Name),
+                        new LocalizedString("PALETTE_EXISTS"));
                     if (consent == ConfirmationType.No)
                     {
                         palette.Name = LocalPalettesFetcher.GetNonExistingName(palette.Name);
@@ -210,14 +212,14 @@ internal class ColorsViewModel : SubViewModel<ViewModelMain>
         if (doc is null)
             return;
 
-        if (ConfirmationDialog.Show("Replace current palette with selected one?", "Replace current palette") == ConfirmationType.Yes)
+        if (ConfirmationDialog.Show(new LocalizedString("REPLACE_PALETTE_CONSENT"), new LocalizedString("REPLACE_PALETTE")) == ConfirmationType.Yes)
         {
             if (doc.Palette is null)
             {
                 doc.Palette = new WpfObservableRangeCollection<DrawingApi.Core.ColorsImpl.Color>();
             }
 
-            doc.Palette.ReplaceRange(palette.Select(x => Color.Parse(x)));
+            doc.Palette.ReplaceRange(palette.Select(Color.Parse));
         }
     }
 
@@ -263,16 +265,16 @@ internal class ColorsViewModel : SubViewModel<ViewModelMain>
         return ColorSearchResult.GetIcon(color);
     }
 
-    [Command.Basic("PixiEditor.Colors.SelectFirstPaletteColor", "Select color 1", "Select the first color in the palette", Key = Key.D1, Parameter = 0, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.FirstPaletteColorIcon")]
-    [Command.Basic("PixiEditor.Colors.SelectSecondPaletteColor", "Select color 2", "Select the second color in the palette", Key = Key.D2, Parameter = 1, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.SecondPaletteColorIcon")]
-    [Command.Basic("PixiEditor.Colors.SelectThirdPaletteColor", "Select color 3", "Select the third color in the palette", Key = Key.D3, Parameter = 2, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.ThirdPaletteColorIcon")]
-    [Command.Basic("PixiEditor.Colors.SelectFourthPaletteColor", "Select color 4", "Select the fourth color in the palette", Key = Key.D4, Parameter = 3, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.FourthPaletteColorIcon")]
-    [Command.Basic("PixiEditor.Colors.SelectFifthPaletteColor", "Select color 5", "Select the fifth color in the palette", Key = Key.D5, Parameter = 4, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.FifthPaletteColorIcon")]
-    [Command.Basic("PixiEditor.Colors.SelectSixthPaletteColor", "Select color 6", "Select the sixth color in the palette", Key = Key.D6, Parameter = 5, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.SixthPaletteColorIcon")]
-    [Command.Basic("PixiEditor.Colors.SelectSeventhPaletteColor", "Select color 7", "Select the seventh color in the palette", Key = Key.D7, Parameter = 6, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.SeventhPaletteColorIcon")]
-    [Command.Basic("PixiEditor.Colors.SelectEighthPaletteColor", "Select color 8", "Select the eighth color in the palette", Key = Key.D8, Parameter = 7, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.EighthPaletteColorIcon")]
-    [Command.Basic("PixiEditor.Colors.SelectNinthPaletteColor", "Select color 9", "Select the ninth color in the palette", Key = Key.D9, Parameter = 8, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.NinthPaletteColorIcon")]
-    [Command.Basic("PixiEditor.Colors.SelectTenthPaletteColor", "Select color 10", "Select the tenth color in the palette", Key = Key.D0, Parameter = 9, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.TenthPaletteColorIcon")]
+    [Command.Basic("PixiEditor.Colors.SelectFirstPaletteColor", "SELECT_COLOR_1", "SELECT_COLOR_1_DESCRIPTIVE", Key = Key.D1, Parameter = 0, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.FirstPaletteColorIcon")]
+    [Command.Basic("PixiEditor.Colors.SelectSecondPaletteColor", "SELECT_COLOR_2", "SELECT_COLOR_2_DESCRIPTIVE", Key = Key.D2, Parameter = 1, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.SecondPaletteColorIcon")]
+    [Command.Basic("PixiEditor.Colors.SelectThirdPaletteColor", "SELECT_COLOR_3", "SELECT_COLOR_3_DESCRIPTIVE", Key = Key.D3, Parameter = 2, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.ThirdPaletteColorIcon")]
+    [Command.Basic("PixiEditor.Colors.SelectFourthPaletteColor", "SELECT_COLOR_4", "SELECT_COLOR_4_DESCRIPTIVE", Key = Key.D4, Parameter = 3, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.FourthPaletteColorIcon")]
+    [Command.Basic("PixiEditor.Colors.SelectFifthPaletteColor", "SELECT_COLOR_5", "SELECT_COLOR_5_DESCRIPTIVE", Key = Key.D5, Parameter = 4, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.FifthPaletteColorIcon")]
+    [Command.Basic("PixiEditor.Colors.SelectSixthPaletteColor", "SELECT_COLOR_6", "SELECT_COLOR_6_DESCRIPTIVE", Key = Key.D6, Parameter = 5, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.SixthPaletteColorIcon")]
+    [Command.Basic("PixiEditor.Colors.SelectSeventhPaletteColor", "SELECT_COLOR_7", "SELECT_COLOR_7_DESCRIPTIVE", Key = Key.D7, Parameter = 6, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.SeventhPaletteColorIcon")]
+    [Command.Basic("PixiEditor.Colors.SelectEighthPaletteColor", "SELECT_COLOR_8", "SELECT_COLOR_8_DESCRIPTIVE", Key = Key.D8, Parameter = 7, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.EighthPaletteColorIcon")]
+    [Command.Basic("PixiEditor.Colors.SelectNinthPaletteColor", "SELECT_COLOR_9", "SELECT_COLOR_9_DESCRIPTIVE", Key = Key.D9, Parameter = 8, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.NinthPaletteColorIcon")]
+    [Command.Basic("PixiEditor.Colors.SelectTenthPaletteColor", "SELECT_COLOR_10", "SELECT_COLOR_10_DESCRIPTIVE", Key = Key.D0, Parameter = 9, CanExecute = "PixiEditor.Colors.CanSelectPaletteColor", IconEvaluator = "PixiEditor.Colors.TenthPaletteColorIcon")]
     public void SelectPaletteColor(int index)
     {
         var document = Owner.DocumentManagerSubViewModel.ActiveDocument;
@@ -282,7 +284,7 @@ internal class ColorsViewModel : SubViewModel<ViewModelMain>
         }
     }
 
-    [Command.Basic("PixiEditor.Colors.Swap", "Swap colors", "Swap primary and secondary colors", Key = Key.X)]
+    [Command.Basic("PixiEditor.Colors.Swap", "SWAP_COLORS", "SWAP_COLORS_DESCRIPTIVE", Key = Key.X)]
     public void SwapColors(object parameter)
     {
         (PrimaryColor, SecondaryColor) = (SecondaryColor, PrimaryColor);