Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
feast107 committed Oct 30, 2023
1 parent 8396020 commit 39df6ce
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 53 deletions.
4 changes: 2 additions & 2 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ in your `.resx` file, you can use it like this:
Then you can use the key to bind the language source using `I18N`

```xaml
<TextBlock Text="{I18N Source={x:Static myProject:LangKeys.Language}}"/>
<TextBlock Text="{I18N {x:Static myProject:LangKeys.Language}}"/>
```

When you want to change the language, just call
Expand Down Expand Up @@ -82,7 +82,7 @@ then you put the json into a `Message` property in your view model, you can bind

```xaml
<!--whose DataContext is your view model-->
<TextBlock Text="{I18N Key={Binding Message}}"/>
<TextBlock Text="{I18N {Binding Message}}"/>
```

Each time when the `Message` property is changed or the language source is changed, the text will be updated automatically.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public partial class LangKeys
然后你可以使用这个键来绑定语言源

```xaml
<TextBlock Text="{I18N Source={x:Static myProject:LangKeys.Language}}"/>
<TextBlock Text="{I18N {x:Static myProject:LangKeys.Language}}"/>
```

当你想要改变语言时,只需要调用
Expand Down Expand Up @@ -85,7 +85,7 @@ I18NExtension.Culture = new CultureInfo("language code");

```xaml
<!--他的DataContext就是你的ViewModel-->
<TextBlock Text="{I18N Key={Binding Message}}"/>
<TextBlock Text="{I18N {Binding Message}}"/>
```

每当`Message`属性被改变或者语言源被改变时,文本都会自动更新。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Antelcat.Wpf.I18N" Version="1.0.1"/>

<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2"/>
<PackageReference Include="WPF-UI" Version="2.1.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Antelcat.Wpf.I18N.SourceGenerators\Antelcat.Wpf.I18N.SourceGenerators.csproj" OutputItemType="Analyzer"/>
<ProjectReference Include="..\Antelcat.Wpf.I18N\Antelcat.Wpf.I18N.csproj"/>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="..\Antelcat.Wpf.I18N.Demo.Shared\Properties\Resources.resx">
Expand Down
26 changes: 13 additions & 13 deletions src/Antelcat.Wpf.I18N.Demo.Shared/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,57 +55,57 @@
<MenuItem>
<MenuItem.Header>
<TextBlock
Text="{I18N Key={x:Static m:LangKeys.Select_your_text_to_translate}}" />
Text="{I18N {x:Static m:LangKeys.Select_your_text_to_translate}}" />
</MenuItem.Header>
<ui:CodeBlock>
&lt;TextBlock Text="{I18N Key={x:Static m:LangKeys.Select_your_text_to_translate}}"/&gt;
&lt;TextBlock Text="{I18N {x:Static m:LangKeys.Select_your_text_to_translate}}"/&gt;
</ui:CodeBlock>
</MenuItem>
</Menu>
<ComboBox
Margin="12,0"
Margin="12,0"
SelectedItem="{Binding SelectedKey}"
ItemsSource="{Binding AvailableKeys}" />
<Menu Background="Transparent">
<MenuItem>
<MenuItem.Header>
<TextBlock Text="{I18N Key={x:Static m:LangKeys.Translated_text}}" />
<TextBlock Text="{I18N {x:Static m:LangKeys.Translated_text}}" />
</MenuItem.Header>
<ui:CodeBlock>
&lt;TextBlock Text="{I18N Key={x:Static m:LangKeys.Translated_text}}"/&gt;
&lt;TextBlock Text="{I18N {x:Static m:LangKeys.Translated_text}}"/&gt;
</ui:CodeBlock>
</MenuItem>
</Menu>

<TextBox Margin="12,0" Text="{I18N Key={Binding SelectedKey}}"></TextBox>
<TextBox Text="{I18N {Binding SelectedKey}}" Margin="12,0" />
<ui:CodeBlock Margin="12">
&lt;TextBlock Text="{I18N Key={Binding SelectedKey}}"/&gt;
&lt;TextBlock Text="{I18N {Binding SelectedKey}}"/&gt;
</ui:CodeBlock>
</StackPanel>
</ui:Card>
<ui:Card Grid.Row="1" Margin="0,12,10,0">
<StackPanel>
<TextBlock Text="{I18N Key={x:Static m:LangKeys.Input_your_text_to_translate}}"></TextBlock>
<TextBlock Text="{I18N {x:Static m:LangKeys.Input_your_text_to_translate}}"></TextBlock>
<TextBox Margin="0,10"
Text="{Binding InputText,UpdateSourceTrigger=PropertyChanged}">
</TextBox>
<TextBlock Text="{I18N Key={x:Static m:LangKeys.Translated_text}}"></TextBlock>
<TextBlock Margin="0,10" Text="{I18N Key={Binding InputText}}"></TextBlock>
<TextBlock Text="{I18N {x:Static m:LangKeys.Translated_text}}"/>
<TextBlock Text="{I18N {Binding InputText}}" Margin="0,10" />
<ui:CodeBlock>
&lt;TextBlock Text="{I18N Key={Binding InputText}}"/&gt;
&lt;TextBlock Text="{I18N {Binding InputText}}"/&gt;
</ui:CodeBlock>
</StackPanel>
</ui:Card>
</StackPanel>
<Grid Grid.Column="1">
<StackPanel>
<TextBlock Text="{I18N Key={x:Static m:LangKeys.Available_languages}}"></TextBlock>
<TextBlock Text="{I18N {x:Static m:LangKeys.Available_languages}}"></TextBlock>
<ListBox Margin="0,10"
ItemsSource="{Binding AvailableCultures}"
SelectedItem="{Binding Culture}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{I18N Key={Binding EnglishName}}"></TextBlock>
<TextBlock Text="{I18N {Binding EnglishName}}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Expand Down
1 change: 0 additions & 1 deletion src/Antelcat.Wpf.I18N.Demo/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Windows;
using System.Windows.Navigation;
using Antelcat.Wpf.I18N.Demo.Windows;

namespace Antelcat.Wpf.I18N.Demo
Expand Down
6 changes: 6 additions & 0 deletions src/Antelcat.Wpf.I18N.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Antelcat.Wpf.I18N.Demo.Shared", "Antelcat.Wpf.I18N.Demo.Shared\Antelcat.Wpf.I18N.Demo.Shared.shproj", "{50BA267B-188A-46D3-B957-878790AF1E76}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "__", "__", "{DAC60261-079A-44B4-8872-61FC02A4AE9F}"
ProjectSection(SolutionItems) = preProject
..\Icon.png = ..\Icon.png
..\LICENSE = ..\LICENSE
..\README.en.md = ..\README.en.md
..\README.md = ..\README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Feast.CodeAnalysis.Extensions.SourceGenerators", "..\extern\CodeAnalysis.Extensions\src\Feast.CodeAnalysis.Extensions.SourceGenerators\Feast.CodeAnalysis.Extensions.SourceGenerators.csproj", "{190293FC-3F2C-4FEE-8090-62A09713DD8D}"
EndProject
Expand Down
75 changes: 44 additions & 31 deletions src/Antelcat.Wpf.I18N/I18NExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@ static I18NExtension()
}
}

#region Target

private static readonly DependencyProperty KeyProperty = DependencyProperty.RegisterAttached(
nameof(Key),
typeof(object),
typeof(I18NExtension),
new PropertyMetadata(default));


private static readonly DependencyProperty TargetPropertyProperty = DependencyProperty.RegisterAttached(
"TargetProperty",
typeof(DependencyProperty),
typeof(I18NExtension),
new PropertyMetadata(default(DependencyProperty)));

private static void SetTargetProperty(DependencyObject element, DependencyProperty value)
=> element.SetValue(TargetPropertyProperty, value);

private static DependencyProperty GetTargetProperty(DependencyObject element)
=> (DependencyProperty)element.GetValue(TargetPropertyProperty);

#endregion

public static string? Translate(string key, string? fallbackValue = null)
{
return Target.TryGetValue(key, out var value)
? value as string ?? fallbackValue
: fallbackValue;
}

private static void RegisterLanguageSource(ResourceProviderBase? provider)
{
if (provider is null) return;
Expand All @@ -66,6 +96,10 @@ void Update(object source, string propertyName)
}
}

public I18NExtension() { }
public I18NExtension(string key) => Key = key;
public I18NExtension(Binding binding) => Key = binding;

private readonly DependencyObject proxy = new();

/// <summary>
Expand Down Expand Up @@ -97,7 +131,7 @@ public object? Key
/// Same as <see cref="Binding"/>.<see cref="Binding.ConverterParameter"/>
/// </summary>
[DefaultValue(null)] public object? ConverterParameter { get; set; }

private BindingBase CreateBinding()
{
Binding? keyBinding = null;
Expand Down Expand Up @@ -174,7 +208,13 @@ public override object ProvideValue(IServiceProvider serviceProvider)
if (provideValueTarget.TargetObject is not DependencyObject targetObject) return this;
if (provideValueTarget.TargetProperty is not DependencyProperty targetProperty) return this;

if (Key is null) throw new ArgumentNullException($"{nameof(Key)} cannot be null");
if (Key is null && (keys is null || keys.Count == 0) )
throw new ArgumentNullException($"{nameof(Key)} or {nameof(Keys)} cannot both be null");
if (Key is null && Keys is { Count: 1 })
{
Key = Keys[0];
keys = null;
}
var bindingBase = CreateBinding();
BindingOperations.SetBinding(targetObject, targetProperty, bindingBase);
if (bindingBase is MultiBinding)
Expand All @@ -185,27 +225,6 @@ public override object ProvideValue(IServiceProvider serviceProvider)
return bindingBase.ProvideValue(serviceProvider);
}

#region Target

private static readonly DependencyProperty KeyProperty = DependencyProperty.RegisterAttached(
nameof(Key),
typeof(object),
typeof(I18NExtension),
new PropertyMetadata(default));


private static readonly DependencyProperty TargetPropertyProperty = DependencyProperty.RegisterAttached(
"TargetProperty",
typeof(DependencyProperty),
typeof(I18NExtension),
new PropertyMetadata(default(DependencyProperty)));

private static void SetTargetProperty(DependencyObject element, DependencyProperty value)
=> element.SetValue(TargetPropertyProperty, value);

private static DependencyProperty GetTargetProperty(DependencyObject element)
=> (DependencyProperty)element.GetValue(TargetPropertyProperty);

private void LangExtension_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
switch (sender)
Expand All @@ -225,9 +244,6 @@ private void LangExtension_DataContextChanged(object sender, DependencyPropertyC
}
}

#endregion


private void SetTarget(object targetObject, DependencyProperty targetProperty)
{
switch (targetObject)
Expand All @@ -243,8 +259,6 @@ private void SetTarget(object targetObject, DependencyProperty targetProperty)
}
}

#region Binding

private void ResetBinding(
DependencyObject element)
{
Expand All @@ -255,9 +269,6 @@ private void ResetBinding(
var binding = CreateBinding();
BindingOperations.SetBinding(element, targetProperty, binding);
}


#endregion

public void AddChild(object value)
{
Expand All @@ -267,8 +278,10 @@ public void AddChild(object value)

public void AddText(string key)
{
Keys.Add(new LanguageBinding(key));
}


/// <summary>
/// use <see cref="string.Format(string,object[])"/> to generate final text
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Antelcat.Wpf.I18N/LanguageBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ namespace System.Windows;
/// </summary>
public class LanguageBinding : Binding
{
public LanguageBinding() { }

public LanguageBinding(string key)
{
Key = key;
}

[DefaultValue("")]
public string? Key
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<RootNamespace>Antelcat.Wpf.I18N.SourceGenerators</RootNamespace>
<PackageId>Antelcat.Wpf.I18N.SourceGenerators</PackageId>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>1.0.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
14 changes: 11 additions & 3 deletions src/Antelcat.Wpf.I18n/Antelcat.Wpf.I18n.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
<PackageProjectUrl>https://github.com/Antelcat/Antelcat.Wpf.I18N</PackageProjectUrl>
<PackageIcon>Icon.png</PackageIcon>
<RepositoryUrl>https://github.com/Antelcat/Antelcat.Wpf.I18N.git</RepositoryUrl>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
<AssemblyVersion>1.1.0</AssemblyVersion>
<FileVersion>1.1.0</FileVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Authors>Antelcat</Authors>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>Initial release.</PackageReleaseNotes>
<PackageReleaseNotes>Reduce xaml declaration length; Support cs code translate.</PackageReleaseNotes>
<PackageTags>dotnet;wpf;markup;extension;MVVM;i18n;language;binding;.NET;</PackageTags>
<Version>1.1.0</Version>
</PropertyGroup>

<ItemGroup>
Expand All @@ -49,4 +50,11 @@
<ItemGroup Label="Package">
<None Include="..\Antelcat.Wpf.I18N.SourceGenerators\bin\$(Configuration)\netstandard2.0\Antelcat.Wpf.I18N.SourceGenerators.dll" PackagePath="analyzers\dotnet\cs" Pack="true" Visible="false" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net45" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

0 comments on commit 39df6ce

Please sign in to comment.