Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<AssemblyName>AKSoftware.Localization.MultiLanguages.UWP</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.18362.0</TargetPlatformVersion>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.19041.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.17134.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\AKSoftware.Localization.MultiLanguages\AKSoftware.Localization.MultiLanguages.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using AKSoftware.Localization.MultiLanguages;
using AKSoftware.Localization.MultiLanguages.Providers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AKSoftware.Localization.MultiLanguages.WPF
{
public class WpfLanguageContainer: LanguageContainer, INotifyPropertyChanged
{
public WpfLanguageContainer(IKeysProvider keysProvider) : base(keysProvider) { }

public WpfLanguageContainer(CultureInfo culture, IKeysProvider keysProvider) : base(culture, keysProvider)
{ }

public override void SetLanguage(CultureInfo culture)
{
base.SetLanguage(culture);
OnPropertyChanged("");
}

#region INotifyPropertyChanged
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
#endregion
}
}
47 changes: 46 additions & 1 deletion src/AKSoftware.Localization.MultiLanguages.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Benchmarks", "Benchmarks",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AKSoftware.Localization.MultiLanguages.Benchmarks", "AKSoftware.Localization.MultiLanguages.Benchmarks\AKSoftware.Localization.MultiLanguages.Benchmarks.csproj", "{F5614460-91AC-4F56-A431-C3916066E045}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleAppSample", "ConsoleAppSample\ConsoleAppSample.csproj", "{FDD40575-C8FA-4C55-B157-C15045E1CC59}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleAppSample", "ConsoleAppSample\ConsoleAppSample.csproj", "{FDD40575-C8FA-4C55-B157-C15045E1CC59}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AKSoftware.Localization.MultiLanguages.WPF", "AKSoftware.Localization.MultiLanguages.WPF\AKSoftware.Localization.MultiLanguages.WPF.csproj", "{45FB19ED-6455-49D9-954C-F51F30D890D5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfSampleApp", "WpfSampleApp\WpfSampleApp.csproj", "{3A39FD7F-5649-4255-9E91-BC7D4C25C291}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -255,6 +259,46 @@ Global
{FDD40575-C8FA-4C55-B157-C15045E1CC59}.Release|x64.Build.0 = Release|Any CPU
{FDD40575-C8FA-4C55-B157-C15045E1CC59}.Release|x86.ActiveCfg = Release|Any CPU
{FDD40575-C8FA-4C55-B157-C15045E1CC59}.Release|x86.Build.0 = Release|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Debug|ARM.ActiveCfg = Debug|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Debug|ARM.Build.0 = Debug|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Debug|ARM64.Build.0 = Debug|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Debug|x64.ActiveCfg = Debug|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Debug|x64.Build.0 = Debug|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Debug|x86.ActiveCfg = Debug|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Debug|x86.Build.0 = Debug|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Release|Any CPU.Build.0 = Release|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Release|ARM.ActiveCfg = Release|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Release|ARM.Build.0 = Release|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Release|ARM64.ActiveCfg = Release|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Release|ARM64.Build.0 = Release|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Release|x64.ActiveCfg = Release|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Release|x64.Build.0 = Release|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Release|x86.ActiveCfg = Release|Any CPU
{45FB19ED-6455-49D9-954C-F51F30D890D5}.Release|x86.Build.0 = Release|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Debug|ARM.ActiveCfg = Debug|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Debug|ARM.Build.0 = Debug|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Debug|ARM64.Build.0 = Debug|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Debug|x64.ActiveCfg = Debug|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Debug|x64.Build.0 = Debug|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Debug|x86.ActiveCfg = Debug|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Debug|x86.Build.0 = Debug|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Release|Any CPU.Build.0 = Release|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Release|ARM.ActiveCfg = Release|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Release|ARM.Build.0 = Release|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Release|ARM64.ActiveCfg = Release|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Release|ARM64.Build.0 = Release|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Release|x64.ActiveCfg = Release|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Release|x64.Build.0 = Release|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Release|x86.ActiveCfg = Release|Any CPU
{3A39FD7F-5649-4255-9E91-BC7D4C25C291}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -267,6 +311,7 @@ Global
{8EA5EEEA-B317-4A54-8ACC-6CE0968E13EB} = {250EFD8C-4FB7-419C-8155-B81CFB193975}
{F5614460-91AC-4F56-A431-C3916066E045} = {5270185F-FF5C-49EE-AAE3-11BAF2388341}
{FDD40575-C8FA-4C55-B157-C15045E1CC59} = {250EFD8C-4FB7-419C-8155-B81CFB193975}
{3A39FD7F-5649-4255-9E91-BC7D4C25C291} = {250EFD8C-4FB7-419C-8155-B81CFB193975}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C0B8EEB9-C64F-4B59-8C57-872A2D48659E}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<PackageReference Include="Microsoft.csharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="YamlDotNet" Version="9.1.0" />
<PackageReference Include="YamlDotNet" Version="13.7.1" />
</ItemGroup>

<ItemGroup>
Expand All @@ -37,7 +37,7 @@
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="docs\README.md" Pack="true" PackagePath="\"/>
<None Include="docs\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public LanguageContainer(IKeysProvider keysProvider) : this(CultureInfo.CurrentC
/// </summary>
/// <param name="culture">The required culture</param>
/// <param name="isDefault">To indicates if this is the initial function</param>
private void SetLanguage(CultureInfo culture, bool isDefault)
protected virtual void SetLanguage(CultureInfo culture, bool isDefault)
{
CurrentCulture = culture;
Keys = _keysProvider.GetKeys(culture);
Expand All @@ -66,7 +66,7 @@ private void SetLanguage(CultureInfo culture, bool isDefault)
/// Set language manually based on a specific culture
/// </summary>
/// <param name="culture">The required culture</param>
public void SetLanguage(CultureInfo culture)
public virtual void SetLanguage(CultureInfo culture)
{
CurrentCulture = culture;
Keys = _keysProvider.GetKeys(culture.Name);
Expand Down
2 changes: 1 addition & 1 deletion src/UwpAkLocalization/UwpAkLocalization.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<AssemblyName>UwpAkLocalization</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.18362.0</TargetPlatformVersion>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.19041.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.17134.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
Expand Down
9 changes: 9 additions & 0 deletions src/WpfSampleApp/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="WpfSampleApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfSampleApp"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
21 changes: 21 additions & 0 deletions src/WpfSampleApp/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using AKSoftware.Localization.MultiLanguages.Providers;
using AKSoftware.Localization.MultiLanguages.WPF;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfSampleApp
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App: Application
{
public static readonly WpfLanguageContainer LanguageContainer
= new(new("en-us"), new FolderResourceKeysProvider("Resources"));
}
}
10 changes: 10 additions & 0 deletions src/WpfSampleApp/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
43 changes: 43 additions & 0 deletions src/WpfSampleApp/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<Window x:Class="WpfSampleApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfSampleApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="600">
<Grid>
<StackPanel>
<ComboBox
x:Name="LanguageSelector"
SelectionChanged="ComboBox_SelectionChanged"
/>
<WrapPanel>
<TextBlock
Text="Current culture: "
/>
<TextBlock
d:Text="xx-xx"
Text="{Binding LanguageContainer.CurrentCulture, RelativeSource={RelativeSource AncestorType=Window}}"
/>
</WrapPanel>
<TextBlock
FontSize="28"
d:Text="hello"
Text="{Binding LanguageContainer[hello], RelativeSource={RelativeSource AncestorType=Window}}"
/>
<TextBlock
FontSize="16"
d:Text="desc:1"
Text="{Binding LanguageContainer[desc:1], RelativeSource={RelativeSource AncestorType=Window}}"
/>
<Button
d:Content="desc:2"
Margin="10"
Content="{Binding LanguageContainer[desc:2], RelativeSource={RelativeSource AncestorType=Window}}"
Click="Button_Click"
/>
</StackPanel>

</Grid>
</Window>
101 changes: 101 additions & 0 deletions src/WpfSampleApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using AKSoftware.Localization.MultiLanguages;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace WpfSampleApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow: Window
{
public MainWindow()
{
InitializeComponent();

// getting language files list from embedded resources
var assembly = Assembly.GetExecutingAssembly();
var languageFiles =
assembly.GetManifestResourceNames()
.Where(t => Regex.IsMatch(t, @"\.([A-Za-z]{2}-[A-Za-z]{2}).ya?ml$"))
.ToDictionary(
t =>
{
// just for proof-of-concept, there might be a better solution for this
var m = Regex.Match(t, @"\.([A-Za-z]{2}-[A-Za-z]{2}).ya?ml$");
return m.Groups[1].Value;
},
t => t
);
YamlDotNet.Serialization.Deserializer deserializer = new();

foreach (var languageFile in languageFiles)
{
using var stream = assembly.GetManifestResourceStream(languageFile.Value);
if (stream is null) continue;

byte[] s = new byte[stream.Length];
stream.Read(s);
string st = Encoding.UTF8.GetString(s);

var obj = deserializer.Deserialize<Dictionary<object, object>>(st);
var names = ((Dictionary<object, object>)obj["languageName"]);
LanguageSelector.Items.Add(
new ComboBoxItem()
{
Tag = languageFile.Key,
Content = $"{names["english"]} / {names["native"]}"
}
);

}
}

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var combobox = sender as ComboBox;
var selected = combobox?.SelectedItem as ComboBoxItem;
string languageCode = selected?.Tag as string ?? "ko";

App.LanguageContainer.SetLanguage(new(languageCode));/*
LanguageContainer = App.DummyLanguageContainer;
LanguageContainer = App.LanguageContainer;*/
}

public ILanguageContainerService LanguageContainer
{
get => (ILanguageContainerService)GetValue(LanguageContainerProperty);
set => SetValue(LanguageContainerProperty, value);
}

public static readonly DependencyProperty LanguageContainerProperty
= DependencyProperty.RegisterReadOnly(
nameof(LanguageContainer),
typeof(ILanguageContainerService),
typeof(MainWindow),
new FrameworkPropertyMetadata(
App.LanguageContainer,
FrameworkPropertyMetadataOptions.AffectsRender,
null
)
).DependencyProperty;

private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(App.LanguageContainer["desc:3"]);
}
}
}
8 changes: 8 additions & 0 deletions src/WpfSampleApp/Resources/en-us.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
languageName:
english: English (US)
native: English (US)
hello: Hello!
desc:
1: Now AKSoftware.Localization.MultiLanguages is also available for WPF.
2: Click me
3: It works!
8 changes: 8 additions & 0 deletions src/WpfSampleApp/Resources/ja-jp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
languageName:
english: Japanese
native: 日本語
hello: こんにちは!
desc:
1: 今からWPFでもAKSoftware.Localization.MultiLanguagesがご利用いただけます。
2: クリック
3: 正常動作します!
8 changes: 8 additions & 0 deletions src/WpfSampleApp/Resources/ko-kr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
languageName:
english: Korean
native: 한국어
hello: 안녕하세요!
desc:
1: 이제 WPF에서도 AKSoftware.Localization.MultiLanguages를 사용할 수 있습니다.
2: 클릭하세요
3: 잘 작동하네요!
Loading