Przeglądaj źródła

Localized most of the settings (except bindings)

Krzysztof Krysiński 2 lat temu
rodzic
commit
ae0d629ab9

+ 24 - 1
src/PixiEditor/Data/Localization/Languages/en.json

@@ -7,5 +7,28 @@
   "LANGUAGE": "Language",
   "GENERAL": "General",
   "DISCORD": "Discord",
-  "KEY_BINDINGS": "Key Bindings"
+  "KEY_BINDINGS": "Key Bindings",
+  "MISC": "Misc",
+  "SHOW_STARTUP_WINDOW": "Show Startup Window",
+  "SHOW_IMAGE_PREVIEW_TASKBAR": "Show image preview in taskbar",
+  "RECENT_FILE_LENGTH": "Recent file list length",
+  "RECENT_FILE_LENGTH_TOOLTIP": "How many documents are shown under File > Recent. Default: 8",
+  "DEFAULT_NEW_SIZE": "Default new file size",
+  "WIDTH": "Width",
+  "HEIGHT": "Height",
+  "TOOLS": "Tools",
+  "ENABLE_SHARED_TOOLBAR": "Enable shared toolbar",
+  "AUTOMATIC_UPDATES": "Automatic Updates",
+  "CHECK_FOR_UPDATES": "Check updates on startup",
+  "UPDATE_STREAM": "Update stream",
+  "UPDATE_CHANNEL_HELP_TOOLTIP": "Update channels can only be changed in standalone version (downloaded from https://pixieditor.net).
Steam and Microsoft Store versions handle updates separately.",
+  "DEBUG": "Debug",
+  "ENABLE_DEBUG_MODE": "Enable Debug mode",
+  "OPEN_CRASH_REPORTS_DIR": "Open crash reports directory",
+  
+  "DISCORD_RICH_PRESENCE": "Rich Presence",
+  "ENABLED": "Enabled",
+  "SHOW_IMAGE_NAME": "Show image name",
+  "SHOW_IMAGE_SIZE": "Show image size",
+  "SHOW_LAYER_COUNT": "Show layer count"
 }

+ 2 - 1
src/PixiEditor/Data/Localization/Languages/pl.json

@@ -4,5 +4,6 @@
   "NEW_FILE": "Nowy",
   "RECENT_EMPTY_TEXT": "Ale tu pusto, na co czekasz, stwórz coś!",
 
-  "LANGUAGE": "Język"
+  "LANGUAGE": "Język",
+  "RECENT_FILE_LENGTH_TOOLTIP": "Ile ostatnich plików wyświetlać, domyślnie 8"
 }

+ 17 - 2
src/PixiEditor/Localization/LocalizationProvider.cs

@@ -1,4 +1,5 @@
 using System.IO;
+using PixiEditor.Models.UserPreferences;
 
 namespace PixiEditor.Localization;
 
@@ -31,9 +32,23 @@ internal class LocalizationProvider : ILocalizationProvider
         {
             throw new InvalidDataException("Localization data does not contain any languages.");
         }
+
+        DefaultLanguage = LoadLanguageInternal(LocalizationData.Languages[0]);
+        
+        string currentLanguageCode = IPreferences.Current.GetPreference<string>("LanguageCode");
+
+        int languageIndex = 0;
+        
+        for (int i = 0; i < LocalizationData.Languages.Length; i++)
+        {
+            if (LocalizationData.Languages[i].Code == currentLanguageCode)
+            {
+                languageIndex = i;
+                break;
+            }
+        }
         
-        LoadLanguage(LocalizationData.Languages[0]);
-        DefaultLanguage = CurrentLanguage;
+        LoadLanguage(LocalizationData.Languages[languageIndex]);
     }
 
     public void LoadLanguage(LanguageData languageData)

+ 5 - 0
src/PixiEditor/Localization/LocalizedString.cs

@@ -27,6 +27,11 @@ public struct LocalizedString
 
     private static string GetValue(string key)
     {
+        if (string.IsNullOrEmpty(key))
+        {
+            return key;
+        }
+        
         ILocalizationProvider localizationProvider = ILocalizationProvider.Current;
         if (localizationProvider?.LocalizationData == null)
         {

+ 3 - 1
src/PixiEditor/ViewModels/SubViewModels/UserPreferences/Settings/GeneralSettings.cs

@@ -1,4 +1,5 @@
 using PixiEditor.Localization;
+using PixiEditor.Models.UserPreferences;
 
 namespace PixiEditor.ViewModels.SubViewModels.UserPreferences.Settings;
 
@@ -6,7 +7,7 @@ internal class GeneralSettings : SettingsGroup
 {
     private bool imagePreviewInTaskbar = GetPreference(nameof(ImagePreviewInTaskbar), false);
     private LanguageData selectedLanguage = ILocalizationProvider.Current.CurrentLanguage.LanguageData;
-    private List<LanguageData> availableLanguages = ILocalizationProvider.Current.LocalizationData.Languages.ToList();
+    private List<LanguageData> availableLanguages = ILocalizationProvider.Current.LocalizationData.Languages.OrderBy(x => x.Name).ToList();
 
     public bool ImagePreviewInTaskbar
     {
@@ -35,6 +36,7 @@ internal class GeneralSettings : SettingsGroup
             if (SetProperty(ref selectedLanguage, value))
             {
                 ILocalizationProvider.Current.LoadLanguage(value);
+                IPreferences.Current.UpdatePreference("LanguageCode", value.Code);
             }
         }
     }

+ 4 - 4
src/PixiEditor/ViewModels/ViewModelMain.cs

@@ -88,13 +88,13 @@ internal class ViewModelMain : ViewModelBase
     public void Setup(IServiceProvider services)
     {
         Services = services;
-        
-        LocalizationProvider = services.GetRequiredService<ILocalizationProvider>();
-        LocalizationProvider.LoadData();
 
         Preferences = services.GetRequiredService<IPreferences>();
-
         Preferences.Init();
+        
+        LocalizationProvider = services.GetRequiredService<ILocalizationProvider>();
+        LocalizationProvider.LoadData();
+        
         WindowSubViewModel = services.GetService<WindowViewModel>();
         DocumentManagerSubViewModel = services.GetRequiredService<DocumentManagerViewModel>();
         SelectionSubViewModel = services.GetService<SelectionViewModel>();

+ 32 - 31
src/PixiEditor/Views/Dialogs/SettingsWindow.xaml

@@ -110,53 +110,54 @@
                     </ComboBox.ItemContainerStyle>
                 </ComboBox>
 
-                <Label Style="{StaticResource SettingsHeader}">Misc</Label>
+                <Label Style="{StaticResource SettingsHeader}" views:Translator.Key="MISC" d:Content="Misc"/>
 
                 <CheckBox Margin="27 0"
-                          VerticalAlignment="Center"
-                          IsChecked="{Binding SettingsSubViewModel.File.ShowStartupWindow}">Show startup window</CheckBox>
+                          VerticalAlignment="Center" views:Translator.Key="SHOW_STARTUP_WINDOW" d:Content="Show startup window"
+                          IsChecked="{Binding SettingsSubViewModel.File.ShowStartupWindow}"/>
 
                 <CheckBox Margin="27 10"
-                          VerticalAlignment="Center"
-                          IsChecked="{Binding SettingsSubViewModel.General.ImagePreviewInTaskbar}">Show image preview in taskbar</CheckBox>
+                          VerticalAlignment="Center" d:Content="Show image preview in taskbar" views:Translator.Key="SHOW_IMAGE_PREVIEW_TASKBAR"
+                          IsChecked="{Binding SettingsSubViewModel.General.ImagePreviewInTaskbar}"/>
 
                 <StackPanel Margin="27 0" Orientation="Horizontal">
                 <Label Style="{StaticResource SettingsText}"
-                       ToolTip="How many documents are shown under File > Recent. Default: 8">Recent file list length</Label>
-                <usercontrols:NumberInput Margin="10 0 0 0"
-                                   Min="0" FontSize="12" HorizontalAlignment="Left"
+                       views:Translator.Key="RECENT_FILE_LENGTH"
+                       views:Translator.TooltipKey="RECENT_FILE_LENGTH_TOOLTIP"/>
+                    <usercontrols:NumberInput Margin="10 0 0 0" 
+                                              Min="0" FontSize="12" HorizontalAlignment="Left"
                                    Value="{Binding SettingsSubViewModel.File.MaxOpenedRecently, Mode=TwoWay}" Height="19" Width="40"/>
                 </StackPanel>
 
-                <Label Style="{StaticResource SettingsHeader}">Default new file size</Label>
+                <Label Style="{StaticResource SettingsHeader}" d:Content="Default new file size" views:Translator.Key="DEFAULT_NEW_SIZE"/>
 
                 <StackPanel Orientation="Horizontal" Margin="27 5">
-                <Label Style="{StaticResource SettingsText}">Width</Label>
-                <usercontrols:SizeInput Margin="10 0 0 0" 
+                    <Label Style="{StaticResource SettingsText}" d:Content="Width" views:Translator.Key="WIDTH"/>
+                    <usercontrols:SizeInput Margin="10 0 0 0" 
                                  Size="{Binding SettingsSubViewModel.File.DefaultNewFileWidth, Mode=TwoWay}" 
                                  Width="70" Height="21" MaxSize="9999" HorizontalAlignment="Left"/>
                 </StackPanel>
 
                 <StackPanel Orientation="Horizontal" Margin="27 5">
-                    <Label Style="{StaticResource SettingsText}">Height</Label>
-                <usercontrols:SizeInput Margin="7 0 0 0"
+                    <Label Style="{StaticResource SettingsText}" d:Content="Height" views:Translator.Key="HEIGHT"/> 
+                    <usercontrols:SizeInput Margin="7 0 0 0"
                                  Size="{Binding SettingsSubViewModel.File.DefaultNewFileHeight, Mode=TwoWay}" 
                                  Width="70" Height="21" MaxSize="9999" HorizontalAlignment="Left"/>
                 </StackPanel>
 
-                <Label Style="{StaticResource SettingsHeader}">Tools</Label>
+                <Label Style="{StaticResource SettingsHeader}" d:Content="Tools" views:Translator.Key="TOOLS"/>
 
                 <CheckBox VerticalAlignment="Center" Margin="27 5"
-                    IsChecked="{Binding SettingsSubViewModel.Tools.EnableSharedToolbar}">Enable shared toolbar</CheckBox>
+                    IsChecked="{Binding SettingsSubViewModel.Tools.EnableSharedToolbar}" d:Content="Enable shared toolbar" views:Translator.Key="ENABLE_SHARED_TOOLBAR"/>
 
-                <Label Style="{StaticResource SettingsHeader}">Automatic updates</Label>
+                <Label Style="{StaticResource SettingsHeader}" d:Content="Automatic updates" views:Translator.Key="AUTOMATIC_UPDATES"/>
 
                 <CheckBox Margin="27 5" VerticalAlignment="Center" IsEnabled="{Binding Path=ShowUpdateTab}"
-                    IsChecked="{Binding SettingsSubViewModel.Update.CheckUpdatesOnStartup}">Check updates on startup</CheckBox>
+                    IsChecked="{Binding SettingsSubViewModel.Update.CheckUpdatesOnStartup}" views:Translator.Key="CHECK_FOR_UPDATES" d:Content="Check updates on startup"/>
 
                 <StackPanel Orientation="Horizontal" Margin="27 5">
-                <Label Grid.Row="11" Grid.Column="1" Style="{StaticResource SettingsText}">Update stream</Label>
-                <StackPanel Margin="5 0" Orientation="Horizontal" VerticalAlignment="Center"
+                    <Label Grid.Row="11" Grid.Column="1" Style="{StaticResource SettingsText}" d:Content="Update stream" views:Translator.Key="UPDATE_STREAM"/>
+                    <StackPanel Margin="5 0" Orientation="Horizontal" VerticalAlignment="Center"
                             Height="21.96" HorizontalAlignment="Left">
                 <ComboBox Width="110" IsEnabled="{Binding Path=ShowUpdateTab}"
                     ItemsSource="{Binding SettingsSubViewModel.Update.UpdateChannels}"
@@ -164,16 +165,16 @@
                 <Image Cursor="Help" Margin="10 0 0 0" Source="/Images/Commands/PixiEditor/Links/OpenDocumentation.png"
                        ToolTipService.InitialShowDelay="0"
                        Visibility="{Binding Path=ShowUpdateTab, Converter={converters:InverseBoolToVisibilityConverter}}"
-                       ToolTip="Update channels can only be changed in standalone version (downloaded from https://pixieditor.net).&#x0a;Steam and Microsoft Store versions handle updates separately."/>
+                       views:Translator.TooltipKey="UPDATE_CHANNEL_HELP_TOOLTIP"/>
                 </StackPanel>
                 </StackPanel>
 
-                <Label Style="{StaticResource SettingsHeader}">Debug</Label>
+                <Label Style="{StaticResource SettingsHeader}" d:Content="Debug" views:Translator.Key="DEBUG"/>
                 <CheckBox Margin="27 5" VerticalAlignment="Center"
-                    IsChecked="{Binding SettingsSubViewModel.General.IsDebugModeEnabled}">Enable Debug Mode</CheckBox>
+                    IsChecked="{Binding SettingsSubViewModel.General.IsDebugModeEnabled}" views:Translator.Key="ENABLE_DEBUG_MODE" d:Content="Enable Debug Mode"/>
                 <Label Margin="0 5" Style="{StaticResource SettingsText}" VerticalAlignment="Center">
                     <Hyperlink Command="{cmds:Command PixiEditor.Debug.OpenCrashReportsDirectory}" Style="{StaticResource SettingsLink}">
-                        <Run Text="Open Crash Reports Directory"/>
+                        <Run views:Translator.Key="OPEN_CRASH_REPORTS_DIR" d:Text="Open crash reports directory"/>
                         <Run Text="" FontFamily="{StaticResource Feather}"/>
                     </Hyperlink>
                 </Label>
@@ -188,19 +189,19 @@
                     </Binding>
                 </StackPanel.Visibility>
                 <StackPanel Orientation="Vertical">
-                    <Label Style="{StaticResource SettingsHeader}">Rich Presence</Label>
-                    
+                    <Label Style="{StaticResource SettingsHeader}" d:Content="Rich Presence" views:Translator.Key="DISCORD_RICH_PRESENCE"/>
+
                     <CheckBox Margin="27 5" VerticalAlignment="Center"
-                              IsChecked="{Binding SettingsSubViewModel.Discord.EnableRichPresence}">Enabled</CheckBox>
+                              IsChecked="{Binding SettingsSubViewModel.Discord.EnableRichPresence}" d:Content="Enabled" views:Translator.Key="ENABLED"/>
                     <CheckBox Margin="27 5" VerticalAlignment="Center"
                               IsEnabled="{Binding SettingsSubViewModel.Discord.EnableRichPresence}" 
-                              IsChecked="{Binding SettingsSubViewModel.Discord.ShowDocumentName}">Show image name</CheckBox>
+                              IsChecked="{Binding SettingsSubViewModel.Discord.ShowDocumentName}" d:Content="Show image name" views:Translator.Key="SHOW_IMAGE_NAME"/>
                     <CheckBox Margin="27 5" VerticalAlignment="Center"
-                              IsEnabled="{Binding SettingsSubViewModel.Discord.EnableRichPresence}" 
-                              IsChecked="{Binding SettingsSubViewModel.Discord.ShowDocumentSize}">Show image size</CheckBox>
+                              IsEnabled="{Binding SettingsSubViewModel.Discord.EnableRichPresence}" d:Content="Show image size" views:Translator.Key="SHOW_IMAGE_SIZE"
+                              IsChecked="{Binding SettingsSubViewModel.Discord.ShowDocumentSize}"/>
                     <CheckBox Margin="27 5" VerticalAlignment="Center"
-                              IsEnabled="{Binding SettingsSubViewModel.Discord.EnableRichPresence}" 
-                              IsChecked="{Binding SettingsSubViewModel.Discord.ShowLayerCount}">Show layer count</CheckBox>
+                              IsEnabled="{Binding SettingsSubViewModel.Discord.EnableRichPresence}" views:Translator.Key="SHOW_LAYER_COUNT" d:Content="Show layer count"
+                              IsChecked="{Binding SettingsSubViewModel.Discord.ShowLayerCount}"/>
                 </StackPanel>
                 <usercontrols:DiscordRPPreview 
                     Margin="15"

+ 41 - 3
src/PixiEditor/Views/Translator.cs

@@ -1,6 +1,7 @@
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Data;
+using System.Windows.Documents;
 using PixiEditor.Localization;
 
 namespace PixiEditor.Views;
@@ -9,16 +10,45 @@ public class Translator : UIElement
 {
     private static void OnLanguageChanged(DependencyObject obj, Language newLanguage)
     {
-        obj.SetValue(ValueProperty, new LocalizedString(GetKey(obj)).Value);
+        string key = GetKey(obj);
+        if (key != null)
+        {
+            obj.SetValue(ValueProperty, new LocalizedString(GetKey(obj)).Value);
+        }
+        
+        string tooltipKey = GetTooltipKey(obj);
+        if (tooltipKey != null)
+        {
+            obj.SetValue(FrameworkElement.ToolTipProperty, new LocalizedString(GetTooltipKey(obj)).Value);
+        }
     }
 
     public static readonly DependencyProperty KeyProperty = DependencyProperty.RegisterAttached(
         "Key",
         typeof(string),
         typeof(Translator),
-        new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.AffectsRender, PropertyChangedCallback));
+        new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.AffectsRender, KeyPropertyChangedCallback));
+
+    public static readonly DependencyProperty TooltipKeyProperty = DependencyProperty.RegisterAttached(
+        "TooltipKey", typeof(string), typeof(Translator), 
+        new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.AffectsRender, TooltipKeyPropertyChangedCallback));
+
+    private static void TooltipKeyPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
+    {
+        d.SetValue(FrameworkElement.ToolTipProperty, new LocalizedString(GetTooltipKey(d)).Value);
+    }
+
+    public static void SetTooltipKey(DependencyObject element, string value)
+    {
+        element.SetValue(TooltipKeyProperty, value);
+    }
 
-    private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
+    public static string GetTooltipKey(DependencyObject element)
+    {
+        return (string)element.GetValue(TooltipKeyProperty);
+    }
+
+    private static void KeyPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
     {
         if (e.NewValue is string key)
         {
@@ -39,6 +69,14 @@ public class Translator : UIElement
                     RelativeSource = new RelativeSource(RelativeSourceMode.Self) 
                 });
             }
+            else if (d is Run run)
+            {
+                run.SetBinding(Run.TextProperty, new Binding()
+                { 
+                    Path = new PropertyPath("(views:Translator.Value)"),
+                    RelativeSource = new RelativeSource(RelativeSourceMode.Self) 
+                });
+            }
             else if (d is ContentControl contentControl)
             {
                 contentControl.SetBinding(ContentControl.ContentProperty, new Binding()