Skip to content

Commit

Permalink
Merge pull request chocolatey#844 from gep13/dynamic-translation-gep13
Browse files Browse the repository at this point in the history
(chocolatey#533) Ability to change language dynamically
  • Loading branch information
gep13 authored Jan 27, 2022
2 parents e704b2d + ba4ef46 commit 85ab14b
Show file tree
Hide file tree
Showing 55 changed files with 1,836 additions and 505 deletions.
33 changes: 23 additions & 10 deletions Source/ChocolateyGui.Common.Windows/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
using ChocolateyGui.Common.Startup;
using ChocolateyGui.Common.Utilities;
using ChocolateyGui.Common.ViewModels.Items;
using ChocolateyGui.Common.Windows.Startup;
using ChocolateyGui.Common.Windows.Utilities;
using ChocolateyGui.Common.Windows.ViewModels;
using LiteDB;
Expand Down Expand Up @@ -105,8 +104,6 @@ protected override void Configure()
Logger = Log.Logger = logConfig.CreateLogger();

Container = AutoFacConfiguration.RegisterAutoFac(LicensedChocolateyGuiAssemblySimpleName, LicensedGuiAssemblyLocation);

Internationalization.Initialize();
}

protected override async void OnStartup(object sender, StartupEventArgs e)
Expand Down Expand Up @@ -136,8 +133,10 @@ protected override async void OnStartup(object sender, StartupEventArgs e)
}
catch (Exception ex)
{
ChocolateyMessageBox.Show(string.Format(Resources.Fatal_Startup_Error_Formatted, ex.Message));
Logger.Fatal(ex, Resources.Fatal_Startup_Error);
var messageFormat = L(nameof(Resources.Fatal_Startup_Error_Formatted), ex.Message);

ChocolateyMessageBox.Show(messageFormat);
Logger.Fatal(ex, L(nameof(Resources.Fatal_Startup_Error)));
await OnExitAsync();
}
}
Expand All @@ -164,7 +163,9 @@ protected override object GetInstance(Type service, string key)
}
}

throw new Exception(string.Format(Resources.Application_ContainerError, key ?? service.Name));
throw new Exception(L(
nameof(Resources.Application_ContainerError),
key ?? service.Name));
}

protected override IEnumerable<object> GetAllInstances(Type service)
Expand All @@ -179,31 +180,33 @@ protected override void BuildUp(object instance)

protected override void OnExit(object sender, EventArgs e)
{
Logger.Information(Resources.Application_Exiting);
Logger.Information(L(nameof(Resources.Application_Exiting)));
}

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
FinalizeDatabaseTransaction();
if (e.IsTerminating)
{
Logger.Fatal(Resources.Application_UnhandledException, e.ExceptionObject as Exception);
Logger.Fatal(L(nameof(Resources.Application_UnhandledException)), e.ExceptionObject as Exception);
if (IsExiting)
{
return;
}

var message = L(nameof(Resources.Bootstrapper_UnhandledException));

ChocolateyMessageBox.Show(
e.ExceptionObject.ToString(),
Resources.Bootstrapper_UnhandledException,
message,
MessageBoxButton.OK,
MessageBoxImage.Error,
MessageBoxResult.OK,
MessageBoxOptions.ServiceNotification);
}
else
{
Logger.Error(Resources.Application_UnhandledException, e.ExceptionObject as Exception);
Logger.Error(L(nameof(Resources.Application_UnhandledException)), e.ExceptionObject as Exception);
}
}

Expand All @@ -224,5 +227,15 @@ private static void FinalizeDatabaseTransaction()
}
}
}

private static string L(string key)
{
return TranslationSource.Instance[key];
}

private static string L(string key, params object[] parameters)
{
return TranslationSource.Instance[key, parameters];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Deterministic>true</Deterministic>
<BuildToolsFxCopVersion>1.0.1</BuildToolsFxCopVersion>
<TargetFrameworkProfile />
<LangVersion>7.1</LangVersion>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
Expand Down Expand Up @@ -232,6 +233,8 @@
<Compile Include="Utilities\ChocolateyMessageBox.cs" />
<Compile Include="Services\IPackageArgumentsService.cs" />
<Compile Include="Services\PackageArgumentsService.cs" />
<Compile Include="Utilities\Converters\LocalizationConverter.cs" />
<Compile Include="Utilities\Extensions\LocalizeExtension.cs" />
<Compile Include="Utilities\ToolTipBehavior.cs" />
<Compile Include="Bootstrapper.cs" />
<Compile Include="Commands\CommandExecutionManager.cs" />
Expand Down Expand Up @@ -296,6 +299,7 @@
<Compile Include="Utilities\ResourceReader.cs" />
<Compile Include="ViewModels\AboutViewModel.cs" />
<Compile Include="ViewModels\AdvancedInstallViewModel.cs" />
<Compile Include="ViewModels\ViewModelScreen.cs" />
<Compile Include="ViewModels\Items\PackageViewModel.cs" />
<Compile Include="ViewModels\Items\SourceViewModel.cs" />
<Compile Include="ViewModels\LocalSourceViewModel.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
xmlns:mahDialogs="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:properties="clr-namespace:ChocolateyGui.Common.Properties;assembly=ChocolateyGui.Common"
xmlns:controls="clr-namespace:ChocolateyGui.Common.Windows.Controls"
xmlns:dialogs="clr-namespace:ChocolateyGui.Common.Windows.Controls.Dialogs"
xmlns:theming="clr-namespace:ChocolateyGui.Common.Windows.Theming"
xmlns:lang="clr-namespace:ChocolateyGui.Common.Windows.Utilities.Extensions"
mc:Ignorable="d"
x:Class="ChocolateyGui.Common.Windows.Controls.Dialogs.ChocolateyDialog"
x:Name="PART_Dialog"
Expand All @@ -23,7 +23,7 @@
<Expander Grid.Row="0"
Margin="0 10 0 0"
Padding="0"
Header="{x:Static properties:Resources.ChocolateyDialog_ConsoleOutput}"
Header="{lang:Localize ChocolateyDialog_ConsoleOutput}"
IsExpanded="{Binding Path=ShowOutputConsole, ElementName=PART_Dialog}">
<controls:FauxPowerShellConsole x:Name="PART_Console"
BorderThickness="0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Windows.Media;
using ChocolateyGui.Common.Controls;
using ChocolateyGui.Common.Models;
using ChocolateyGui.Common.Utilities;
using ChocolateyGui.Common.Windows.Theming;
using ControlzEx.Theming;
using MahApps.Metro.Controls;
Expand Down Expand Up @@ -70,7 +71,7 @@ internal ChocolateyDialog(MetroWindow parentWindow, bool showConsoleOutput)
ProgressBarForeground = Brushes.White;
}

NegativeButtonText = Properties.Resources.ChocolateyDialog_Cancel;
NegativeButtonText = TranslationSource.Instance[nameof(Properties.Resources.ChocolateyDialog_Cancel)];
}

public bool IsCancelable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
<Setter Property="LineHeight" Value="28"/>
<Setter Property="FontWeight" Value="Light"/>
</Style>

<Style x:Key="TitleComboboxBlockStyle" TargetType="{x:Type ComboBox}">
<!--<Setter Property="FontSize" Value="24" />-->
<Setter Property="FontWeight" Value="Light" />

<Style.Resources>
<Style TargetType="{x:Type ComboBoxItem}">
<!--<Setter Property="FontSize" Value="18" />-->
<Setter Property="FontWeight" Value="Light" />
</Style>
</Style.Resources>
</Style>

<Style x:Key="SubtitleTextBlockStyle" TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="20"/>
Expand Down
40 changes: 20 additions & 20 deletions Source/ChocolateyGui.Common.Windows/Resources/Controls.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:custom="http://metro.mahapps.com/winfx/xaml/shared"
xmlns:properties="clr-namespace:ChocolateyGui.Common.Properties;assembly=ChocolateyGui.Common"
xmlns:converters="clr-namespace:ChocolateyGui.Common.Windows.Utilities.Converters"
xmlns:items="clr-namespace:ChocolateyGui.Common.Windows.ViewModels.Items"
xmlns:commands="clr-namespace:ChocolateyGui.Common.Windows.Commands"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:theming="clr-namespace:ChocolateyGui.Common.Windows.Theming"
xmlns:lang="clr-namespace:ChocolateyGui.Common.Windows.Utilities.Extensions"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:utilities="clr-namespace:ChocolateyGui.Common.Windows.Utilities"
mc:Ignorable="d">
Expand Down Expand Up @@ -450,74 +450,74 @@
<ContextMenu x:Key="PackagesContextMenu"
DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"
d:DataContext="{d:DesignInstance Type=items:PackageViewModel}">
<MenuItem Header="{x:Static properties:Resources.Controls_PackagesContextMenuPin}"
<MenuItem Header="{lang:Localize Controls_PackagesContextMenuPin}"
Icon="{iconPacks:Modern Kind=Pin}"
Command="{commands:DataContextCommandAdapter Pin}"
IsEnabled="{Binding IsPinAllowed}"
Visibility="{Binding CanPin, Converter={StaticResource BooleanToVisibility}}">
<b:Interaction.Behaviors>
<utilities:ToolTipBehavior EnabledToolTip="{x:Static properties:Resources.Application_OperationPin}"
<utilities:ToolTipBehavior EnabledToolTip="{lang:Localize Application_OperationPin}"
IsFeatureEnabled="{Binding IsPinAllowed, Mode=OneWay}"
DisabledFeatureToolTip="{x:Static properties:Resources.Application_OperationNotAllowed}" />
DisabledFeatureToolTip="{lang:Localize Application_OperationNotAllowed}" />
</b:Interaction.Behaviors>
</MenuItem>
<MenuItem Header="{x:Static properties:Resources.Controls_PackagesContextMenuUnpin}"
<MenuItem Header="{lang:Localize Controls_PackagesContextMenuUnpin}"
Icon="{iconPacks:Modern Kind=PinRemove}"
Command="{commands:DataContextCommandAdapter Unpin}"
IsEnabled="{Binding IsUnpinAllowed}"
Visibility="{Binding CanUnpin, Converter={StaticResource BooleanToVisibility}}">
<b:Interaction.Behaviors>
<utilities:ToolTipBehavior EnabledToolTip="{x:Static properties:Resources.Application_OperationUnpin}"
<utilities:ToolTipBehavior EnabledToolTip="{lang:Localize Application_OperationUnpin}"
IsFeatureEnabled="{Binding IsUnpinAllowed, Mode=OneWay}"
DisabledFeatureToolTip="{x:Static properties:Resources.Application_OperationNotAllowed}" />
DisabledFeatureToolTip="{lang:Localize Application_OperationNotAllowed}" />
</b:Interaction.Behaviors>
</MenuItem>
<MenuItem Header="{x:Static properties:Resources.Controls_PackagesContextMenuDetails}"
<MenuItem Header="{lang:Localize Controls_PackagesContextMenuDetails}"
Icon="{iconPacks:BoxIcons Kind=RegularInfoCircle}"
Command="{commands:DataContextCommandAdapter ViewDetails}" />
<MenuItem Header="{x:Static properties:Resources.Controls_PackagesContextMenuUpdate}"
<MenuItem Header="{lang:Localize Controls_PackagesContextMenuUpdate}"
Icon="{iconPacks:Entypo Kind=Cycle}"
IsEnabled="{Binding IsUpgradeAllowed}"
Visibility="{Binding CanUpdate, Converter={StaticResource BooleanToVisibility}}"
Command="{commands:DataContextCommandAdapter Update}">
<b:Interaction.Behaviors>
<utilities:ToolTipBehavior EnabledToolTip="{x:Static properties:Resources.Application_OperationUpgrade}"
<utilities:ToolTipBehavior EnabledToolTip="{lang:Localize Application_OperationUpgrade}"
IsFeatureEnabled="{Binding IsUpgradeAllowed, Mode=OneWay}"
DisabledFeatureToolTip="{x:Static properties:Resources.Application_OperationNotAllowed}" />
DisabledFeatureToolTip="{lang:Localize Application_OperationNotAllowed}" />
</b:Interaction.Behaviors>
</MenuItem>
<Separator />
<MenuItem Header="{x:Static properties:Resources.Controls_PackagesContextMenuInstall}"
<MenuItem Header="{lang:Localize Controls_PackagesContextMenuInstall}"
Icon="{iconPacks:Entypo Kind=Install}"
IsEnabled="{Binding IsInstallAllowed}"
Visibility="{Binding CanInstall, Converter={StaticResource BooleanToVisibility}}"
Command="{commands:DataContextCommandAdapter Install}">
<b:Interaction.Behaviors>
<utilities:ToolTipBehavior EnabledToolTip="{x:Static properties:Resources.Application_OperationInstall}"
<utilities:ToolTipBehavior EnabledToolTip="{lang:Localize Application_OperationInstall}"
IsFeatureEnabled="{Binding IsInstallAllowed, Mode=OneWay}"
DisabledFeatureToolTip="{x:Static properties:Resources.Application_OperationNotAllowed}" />
DisabledFeatureToolTip="{lang:Localize Application_OperationNotAllowed}" />
</b:Interaction.Behaviors>
</MenuItem>
<MenuItem Header="{x:Static properties:Resources.Controls_PackagesContextMenuReinstall}"
<MenuItem Header="{lang:Localize Controls_PackagesContextMenuReinstall}"
Icon="{iconPacks:Entypo Kind=Cw}"
IsEnabled="{Binding IsReinstallAllowed}"
Visibility="{Binding CanReinstall, Converter={StaticResource BooleanToVisibility}}"
Command="{commands:DataContextCommandAdapter Reinstall}">
<b:Interaction.Behaviors>
<utilities:ToolTipBehavior EnabledToolTip="{x:Static properties:Resources.Application_OperationReinstall}"
<utilities:ToolTipBehavior EnabledToolTip="{lang:Localize Application_OperationReinstall}"
IsFeatureEnabled="{Binding IsReinstallAllowed, Mode=OneWay}"
DisabledFeatureToolTip="{x:Static properties:Resources.Application_OperationNotAllowed}" />
DisabledFeatureToolTip="{lang:Localize Application_OperationNotAllowed}" />
</b:Interaction.Behaviors>
</MenuItem>
<MenuItem Header="{x:Static properties:Resources.Controls_PackagesContextMenuUninstall}"
<MenuItem Header="{lang:Localize Controls_PackagesContextMenuUninstall}"
Icon="{iconPacks:Entypo Kind=Uninstall}"
IsEnabled="{Binding IsUninstallAllowed}"
Visibility="{Binding CanUninstall, Converter={StaticResource BooleanToVisibility}}"
Command="{commands:DataContextCommandAdapter Uninstall}">
<b:Interaction.Behaviors>
<utilities:ToolTipBehavior EnabledToolTip="{x:Static properties:Resources.Application_OperationUninstall}"
<utilities:ToolTipBehavior EnabledToolTip="{lang:Localize Application_OperationUninstall}"
IsFeatureEnabled="{Binding IsUninstallAllowed, Mode=OneWay}"
DisabledFeatureToolTip="{x:Static properties:Resources.Application_OperationNotAllowed}" />
DisabledFeatureToolTip="{lang:Localize Application_OperationNotAllowed}" />
</b:Interaction.Behaviors>
</MenuItem>
</ContextMenu>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using ChocolateyGui.Common.Models;
using ChocolateyGui.Common.Properties;
using ChocolateyGui.Common.Services;
using ChocolateyGui.Common.Utilities;
using Microsoft.VisualStudio.Threading;
using NuGet;
using ChocolateySource = ChocolateyGui.Common.Models.ChocolateySource;
Expand Down Expand Up @@ -149,7 +150,7 @@ public async Task<IReadOnlyList<OutdatedPackage>> GetOutdatedPackages(bool inclu
}
catch (Exception ex)
{
Logger.Error(ex, Resources.Application_OutdatedPackagesError);
Logger.Error(ex, L(nameof(Resources.Application_OutdatedPackagesError)));
}

return results.ToList();
Expand Down Expand Up @@ -645,6 +646,16 @@ private static List<string> GetErrors(out Action<LogMessage> grabErrors)
return errors;
}

private static string L(string key)
{
return TranslationSource.Instance[key];
}

private static string L(string key, params object[] parameters)
{
return TranslationSource.Instance[key, parameters];
}

private async Task<PackageOperationResult> RunCommand(GetChocolatey choco, SerilogLogger logger)
{
Action<LogMessage> grabErrors;
Expand Down
Loading

0 comments on commit 85ab14b

Please sign in to comment.