Skip to content

Commit

Permalink
[+] i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
ZjzMisaka committed Jun 27, 2024
1 parent bb52cb6 commit e829d68
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 18 deletions.
5 changes: 5 additions & 0 deletions Powork/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<SolidColorBrush x:Key="TimeTextBrush" Color="LightGreen"/>
<SolidColorBrush x:Key="ErrorTextBrush" Color="Pink"/>
<ResourceDictionary.MergedDictionaries>
<!-- i18n -->
<ResourceDictionary Source="Resources\StringResource.zh-CN.ui.xaml" />
<ResourceDictionary Source="Resources\StringResource.ja-JP.ui.xaml" />
<ResourceDictionary Source="Resources\StringResource.en-US.ui.xaml" />
<!-- Theme -->
<ui:ThemesDictionary Theme="Dark" />
<ui:ControlsDictionary />
</ResourceDictionary.MergedDictionaries>
Expand Down
12 changes: 12 additions & 0 deletions Powork/Powork.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,16 @@
<None Include="F:\_programming\c#\Powork\Powork\.editorconfig" />
</ItemGroup>

<ItemGroup>
<Page Update="Resources\StringResource.en-US.ui.xaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Page>
<Page Update="Resources\StringResource.ja-JP.ui.xaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Page>
<Page Update="Resources\StringResource.zh-CN.ui.xaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Page>
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions Powork/Resources/StringResource.en-US.ui.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<!-- MainWindow -->
<sys:String x:Key="Msg">Msg</sys:String>
<sys:String x:Key="Team">Team</sys:String>
<sys:String x:Key="Share">Share</sys:String>
<sys:String x:Key="Memo">Memo</sys:String>
<sys:String x:Key="Setting">Setting</sys:String>

<!-- MessgaePage -->
<sys:String x:Key="DropHere">DROP HERE</sys:String>

<!-- SharePage -->
<sys:String x:Key="FileName">Name</sys:String>
<sys:String x:Key="Extension">Extension</sys:String>
<sys:String x:Key="Type">Type</sys:String>
<sys:String x:Key="Size">Size</sys:String>
<sys:String x:Key="ShareTime">Share time</sys:String>
<sys:String x:Key="CreateTime">Create time</sys:String>
<sys:String x:Key="ModifiedTime">Modified time</sys:String>

<!-- SettingPage -->
<sys:String x:Key="Name">Name</sys:String>
<sys:String x:Key="Group">Group</sys:String>
<sys:String x:Key="OK">OK</sys:String>
</ResourceDictionary>
28 changes: 28 additions & 0 deletions Powork/Resources/StringResource.ja-JP.ui.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">

<!-- MainWindow -->
<sys:String x:Key="Msg">メッセージ</sys:String>
<sys:String x:Key="Team">チーム</sys:String>
<sys:String x:Key="Share">シェア</sys:String>
<sys:String x:Key="Memo">メモ</sys:String>
<sys:String x:Key="Setting">設定</sys:String>

<!-- MessgaePage -->
<sys:String x:Key="DropHere">ここにドロップ</sys:String>

<!-- SharePage -->
<sys:String x:Key="FileName">ファイルネーム</sys:String>
<sys:String x:Key="Extension">拡張子</sys:String>
<sys:String x:Key="Type">タイプ</sys:String>
<sys:String x:Key="Size">サイズ</sys:String>
<sys:String x:Key="ShareTime">共有日時</sys:String>
<sys:String x:Key="CreateTime">作成日時</sys:String>
<sys:String x:Key="ModifiedTime">更新日時</sys:String>

<!-- SettingPage -->
<sys:String x:Key="Name">名前</sys:String>
<sys:String x:Key="Group">グループ</sys:String>
<sys:String x:Key="OK">確認</sys:String>
</ResourceDictionary>
28 changes: 28 additions & 0 deletions Powork/Resources/StringResource.zh-CN.ui.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">

<!-- MainWindow -->
<sys:String x:Key="Msg">消息</sys:String>
<sys:String x:Key="Team">团队</sys:String>
<sys:String x:Key="Share">分享</sys:String>
<sys:String x:Key="Memo">笔记</sys:String>
<sys:String x:Key="Setting">设置</sys:String>

<!-- MessgaePage -->
<sys:String x:Key="DropHere">拖放至此</sys:String>

<!-- SharePage -->
<sys:String x:Key="FileName">文件名</sys:String>
<sys:String x:Key="Extension">扩展名</sys:String>
<sys:String x:Key="Type">类型</sys:String>
<sys:String x:Key="Size">大小</sys:String>
<sys:String x:Key="ShareTime">分享时间</sys:String>
<sys:String x:Key="CreateTime">创建时间</sys:String>
<sys:String x:Key="ModifiedTime">修改时间</sys:String>

<!-- SettingPage -->
<sys:String x:Key="Name">姓名</sys:String>
<sys:String x:Key="Group">组别</sys:String>
<sys:String x:Key="OK">确定</sys:String>
</ResourceDictionary>
11 changes: 6 additions & 5 deletions Powork/View/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
Icon="{Binding TrayIcon}">
<tray:NotifyIcon.Menu>
<ContextMenu>
<MenuItem Header="Language" Tag="language" ItemsSource="{Binding LanguageMenuItemList}"/>
<MenuItem Header="Theme" Tag="theme">
<MenuItem Header="Light" Tag="light" Command="{Binding SwitchLightThemeCommand}"/>
<MenuItem Header="Dark" Tag="dark" Command="{Binding SwitchDarkThemeCommand}"/>
Expand All @@ -155,29 +156,29 @@

<ui:NavigationView x:Name="RootNavigation" Grid.Row="1" PaneDisplayMode="LeftFluent">
<ui:NavigationView.MenuItems>
<ui:NavigationViewItem Content="Msg" TargetPageType="{x:Type view:MessagePage}">
<ui:NavigationViewItem Content="{DynamicResource Msg}" TargetPageType="{x:Type view:MessagePage}">
<ui:NavigationViewItem.Icon>
<ui:SymbolIcon Symbol="Mail24" />
</ui:NavigationViewItem.Icon>
</ui:NavigationViewItem>
<ui:NavigationViewItem Content="Team" TargetPageType="{x:Type view:TeamPage}">
<ui:NavigationViewItem Content="{DynamicResource Team}" TargetPageType="{x:Type view:TeamPage}">
<ui:NavigationViewItem.Icon>
<ui:SymbolIcon Symbol="PeopleTeam24" />
</ui:NavigationViewItem.Icon>
</ui:NavigationViewItem>
<ui:NavigationViewItem Content="Share" TargetPageType="{x:Type view:SharePage}">
<ui:NavigationViewItem Content="{DynamicResource Share}" TargetPageType="{x:Type view:SharePage}">
<ui:NavigationViewItem.Icon>
<ui:SymbolIcon Symbol="Folder24" />
</ui:NavigationViewItem.Icon>
</ui:NavigationViewItem>
<ui:NavigationViewItem Content="Memo" TargetPageType="{x:Type view:MemoPage}">
<ui:NavigationViewItem Content="{DynamicResource Memo}" TargetPageType="{x:Type view:MemoPage}">
<ui:NavigationViewItem.Icon>
<ui:SymbolIcon Symbol="Pen24" />
</ui:NavigationViewItem.Icon>
</ui:NavigationViewItem>
</ui:NavigationView.MenuItems>
<ui:NavigationView.FooterMenuItems>
<ui:NavigationViewItem Content="Settings" TargetPageType="{x:Type view:SettingsPage}">
<ui:NavigationViewItem Content="{DynamicResource Setting}" TargetPageType="{x:Type view:SettingsPage}">
<ui:NavigationViewItem.Icon>
<ui:SymbolIcon Symbol="Settings24" />
</ui:NavigationViewItem.Icon>
Expand Down
2 changes: 1 addition & 1 deletion Powork/View/MessagePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock AllowDrop="True" Grid.Column="0" Text="DROP HERE" Foreground="{DynamicResource TextFillColorPrimaryBrush}" Block.TextAlignment="Center" VerticalAlignment="Center">
<TextBlock AllowDrop="True" Grid.Column="0" Text="{DynamicResource DropHere}" Foreground="{DynamicResource TextFillColorPrimaryBrush}" Block.TextAlignment="Center" VerticalAlignment="Center">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Drop">
<i:InvokeCommandAction Command="{Binding DropCommand}" PassEventArgsToCommand="True" />
Expand Down
6 changes: 3 additions & 3 deletions Powork/View/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
<TextBlock Grid.Row="0" Grid.Column="0" Text="IP" Foreground="{DynamicResource TextFillColorPrimaryBrush}" VerticalAlignment="Center" Margin="5, 5, 5, 0"></TextBlock>
<TextBox Grid.Row="0" Grid.Column="1" IsReadOnly="True" VerticalAlignment="Center" Width="200" Text="{Binding IP}" Margin="0, 5, 5, 0"></TextBox>

<TextBlock Grid.Row="1" Grid.Column="0" Text="Name" Foreground="{DynamicResource TextFillColorPrimaryBrush}" VerticalAlignment="Center" Margin="5, 5, 5, 0"></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0" Text="{DynamicResource Name}" Foreground="{DynamicResource TextFillColorPrimaryBrush}" VerticalAlignment="Center" Margin="5, 5, 5, 0"></TextBlock>
<TextBox Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" Width="200" Text="{Binding Name}" Margin="0, 5, 5, 0"></TextBox>

<TextBlock Grid.Row="2" Grid.Column="0" Text="Group" Foreground="{DynamicResource TextFillColorPrimaryBrush}" VerticalAlignment="Center" Margin="5, 5, 5, 0"></TextBlock>
<TextBlock Grid.Row="2" Grid.Column="0" Text="{DynamicResource Group}" Foreground="{DynamicResource TextFillColorPrimaryBrush}" VerticalAlignment="Center" Margin="5, 5, 5, 0"></TextBlock>
<TextBox Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" Width="200" Text="{Binding Group}" Margin="0, 5, 5, 0"></TextBox>

<Button Grid.Row="3" Grid.Column="1" Content="OK" HorizontalAlignment="Right" Command="{Binding OKClickCommand}" Margin="5"></Button>
<Button Grid.Row="3" Grid.Column="1" Content="{DynamicResource OK}" HorizontalAlignment="Right" Command="{Binding OKClickCommand}" Margin="5"></Button>
</Grid>
</Page>
14 changes: 7 additions & 7 deletions Powork/View/SharePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@
</ui:ListView.ItemContainerStyle>
<ui:ListView.View>
<ui:GridView>
<GridViewColumn Width="auto" Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Width="auto" Header="Extension" DisplayMemberBinding="{Binding Extension}"/>
<GridViewColumn Width="auto" Header="Type" DisplayMemberBinding="{Binding Type}"/>
<GridViewColumn Width="auto" Header="Size" DisplayMemberBinding="{Binding Size}"/>
<GridViewColumn Width="auto" Header="ShareTime" DisplayMemberBinding="{Binding ShareTime}"/>
<GridViewColumn Width="auto" Header="CreateTime" DisplayMemberBinding="{Binding CreateTime}"/>
<GridViewColumn Width="auto" Header="LastModifiedTime" DisplayMemberBinding="{Binding LastModifiedTime}"/>
<GridViewColumn Width="auto" Header="{DynamicResource FileName}" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Width="auto" Header="{DynamicResource Extension}" DisplayMemberBinding="{Binding Extension}"/>
<GridViewColumn Width="auto" Header="{DynamicResource Type}" DisplayMemberBinding="{Binding Type}"/>
<GridViewColumn Width="auto" Header="{DynamicResource Size}" DisplayMemberBinding="{Binding Size}"/>
<GridViewColumn Width="auto" Header="{DynamicResource ShareTime}" DisplayMemberBinding="{Binding ShareTime}"/>
<GridViewColumn Width="auto" Header="{DynamicResource CreateTime}" DisplayMemberBinding="{Binding CreateTime}"/>
<GridViewColumn Width="auto" Header="{DynamicResource ModifiedTime}" DisplayMemberBinding="{Binding LastModifiedTime}"/>
</ui:GridView>
</ui:ListView.View>
</ui:ListView>
Expand Down
4 changes: 2 additions & 2 deletions Powork/View/TeamPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@
<Button Command="{Binding ManageTeamMemberCommand}" IsEnabled="{Binding SendEnabled}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" Margin="5">
<ui:SymbolIcon Symbol="PeopleSettings24" />
</Button>
<TextBlock AllowDrop="True" Grid.Column="1" Text="DROP HERE" Foreground="{DynamicResource TextFillColorPrimaryBrush}" Block.TextAlignment="Center" VerticalAlignment="Center">
<TextBlock AllowDrop="True" Grid.Column="1" Text="{DynamicResource DropHere}" Foreground="{DynamicResource TextFillColorPrimaryBrush}" Block.TextAlignment="Center" VerticalAlignment="Center">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Drop">
<i:InvokeCommandAction Command="{Binding DropCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
<Button Command="{Binding GetTeamMemberCommand}" IsEnabled="{Binding SendEnabled}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="2" Margin="5">
<Button Command="{Binding GetTeamMemberCommand}" IsEnabled="{Binding SendEnabled}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="2" Margin="5, 5, 0, 5">
<ui:SymbolIcon Symbol="PeopleSearch24" />
</Button>
<Button Command="{Binding SendMessageCommand}" IsEnabled="{Binding SendEnabled}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="3" Margin="5">
Expand Down
61 changes: 61 additions & 0 deletions Powork/ViewModel/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Net.Sockets;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
Expand Down Expand Up @@ -36,6 +37,7 @@ public class MainWindowViewModel : ObservableObject
private bool _isBlinking;
private ConcurrentDictionary<string, NoticeViewModel> _noticeInfoDic;
private ConcurrentDictionary<string, DownloadInfoViewModel> _downloadInfoDic;
private List<ResourceDictionary> _languageResourceDictionaryList;

private string _trayIcon;
public string TrayIcon
Expand All @@ -44,6 +46,13 @@ public string TrayIcon
set => SetProperty<string>(ref _trayIcon, value);
}

private ObservableCollection<MenuItem> _languageMenuItems;
public ObservableCollection<MenuItem> LanguageMenuItemList
{
get => _languageMenuItems;
set => SetProperty<ObservableCollection<MenuItem>>(ref _languageMenuItems, value);
}

private string _applicationTitle;
public string ApplicationTitle
{
Expand Down Expand Up @@ -129,6 +138,7 @@ public bool DownloadPopupScrollToEnd
public ICommand SwitchLightThemeCommand { get; set; }
public ICommand SwitchDarkThemeCommand { get; set; }
public ICommand ViewOnGithubCommand { get; set; }
public ICommand LanguageSelectedCommand { get; set; }
public ICommand ExitCommand { get; set; }
public ICommand WindowLoadedCommand { get; set; }
public ICommand WindowClosingCommand { get; set; }
Expand All @@ -151,10 +161,12 @@ public MainWindowViewModel(INavigationService navigationService)
_blinkTimer.Tick += (s, e) => ToggleIcon();
_noticeInfoDic = new ConcurrentDictionary<string, NoticeViewModel>();
_downloadInfoDic = new ConcurrentDictionary<string, DownloadInfoViewModel>();
_languageResourceDictionaryList = new List<ResourceDictionary>();

CommonRepository.CreateDatabase();
CommonRepository.CreateTable();
SettingRepository.SetDefault("Theme", "Dark");
SettingRepository.SetDefault("Language", CultureInfo.CurrentUICulture.Name);

ApplicationThemeManager.Changed += ThemeChanged;

Expand Down Expand Up @@ -195,12 +207,14 @@ public MainWindowViewModel(INavigationService navigationService)
MessageBox.Show($"Error Occurred: \n{e.Exception.Message}\n{e.Exception.StackTrace}\nfrom: {e.ErrorFrom}");
};

LanguageMenuItemList = new ObservableCollection<MenuItem>();
NoticeList = new ObservableCollection<NoticeViewModel>();
DownloadList = new ObservableCollection<DownloadInfoViewModel>();

SwitchLightThemeCommand = new RelayCommand(SwitchLightTheme);
SwitchDarkThemeCommand = new RelayCommand(SwitchDarkTheme);
ViewOnGithubCommand = new RelayCommand(ViewOnGithub);
LanguageSelectedCommand = new RelayCommand<string>(LanguageSelected);
ExitCommand = new RelayCommand(Exit);
WindowLoadedCommand = new RelayCommand<RoutedEventArgs>(WindowLoaded);
WindowClosingCommand = new RelayCommand<CancelEventArgs>(WindowClosing);
Expand All @@ -214,13 +228,60 @@ public MainWindowViewModel(INavigationService navigationService)
OpenFolderCommand = new RelayCommand(OpenFolder);
RemoveItemCommand = new RelayCommand(RemoveItem);
StopDownloadCommand = new RelayCommand(StopDownload);

foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
{
if (dictionary.Source != null)
{
string originalString = dictionary.Source.OriginalString;
if (!originalString.Contains("StringResource"))
{
continue;
}
string cultureStr = originalString.Remove(0, originalString.IndexOf(".") + 1);
cultureStr = cultureStr.Remove(cultureStr.IndexOf("."));
CultureInfo cultureInfo = new CultureInfo(cultureStr, false);
string nativeName = cultureInfo.NativeName;
_languageResourceDictionaryList.Add(dictionary);

LanguageMenuItemList.Add(new MenuItem {
Header = nativeName,
Tag = nativeName,
Command = LanguageSelectedCommand,
CommandParameter = cultureInfo.Name,
});
}
}

LanguageSelected(SettingRepository.SelectSetting("Language"));
}

public static void Navigate(Type targetType, ObservableObject dataContext)
{
s_navigationService.Navigate(targetType, dataContext);
}

private void LanguageSelected(string language)
{
string requestedCulture = string.Format(@"Resources\StringResource.{0}", language);
IEnumerable<ResourceDictionary> resourceDictionaryList = _languageResourceDictionaryList.Where(d => d.Source != null && d.Source.OriginalString.StartsWith(requestedCulture));
if (resourceDictionaryList == null || !resourceDictionaryList.Any())
{
requestedCulture = @"Resources\StringResource.en-US";
resourceDictionaryList = _languageResourceDictionaryList.Where(d => d.Source != null && d.Source.OriginalString.StartsWith(requestedCulture));
}
if (resourceDictionaryList != null && resourceDictionaryList.Any())
{
foreach (ResourceDictionary resourceDictionary in resourceDictionaryList)
{
Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
}
}

SettingRepository.UpdateSetting("Language", language);
}

private void SwitchLightTheme()
{
ApplicationThemeManager.Apply(ApplicationTheme.Light, Wpf.Ui.Controls.WindowBackdropType.Auto, true);
Expand Down

0 comments on commit e829d68

Please sign in to comment.