Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resx in other dll cant be read by SourceGenerators (#4) #5

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions src/Antelcat.I18N.Shared/Antelcat.I18N.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Abstractions\ResourceProviderBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Attributes\ResourceKeysOfAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)I18NAssemblyProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)I18NExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LanguageBinding.cs" />
</ItemGroup>
Expand Down
24 changes: 24 additions & 0 deletions src/Antelcat.I18N.Shared/I18NAssemblyProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Reflection;

namespace Antelcat.Shared.I18N;

public static class I18NAssemblyProvider
{
public static Assembly? Assembly { get; private set; } = null;

public static void SetAssembly(Assembly assembly)
{
Assembly = assembly;
}

public static void SetAssembly(Type type)
{
_ = type ?? throw new ArgumentNullException(nameof(type));
Assembly = type.Assembly;
}

public static void SetAssembly<TLangKeys>() where TLangKeys : class
{
SetAssembly(typeof(TLangKeys));
}
}
68 changes: 35 additions & 33 deletions src/Antelcat.I18N.Shared/I18NExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Reflection;
using System.Runtime.Serialization;
using System.Diagnostics;
using Antelcat.Shared.I18N;

#if WPF
using MicrosoftPleaseFixBindingCollection = System.Collections.ObjectModel.Collection<System.Windows.Data.BindingBase>;
using System.Collections.Generic;
Expand Down Expand Up @@ -40,8 +42,8 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions;
public class I18NExtension : MarkupExtension, IAddChild
{
private static readonly IDictionary<string, object?> Target;
private static readonly ResourceChangedNotifier Notifier;
private static event CultureChangedHandler? CultureChanged;
private static readonly ResourceChangedNotifier Notifier;
private static event CultureChangedHandler? CultureChanged;

private delegate void CultureChangedHandler(CultureInfo culture);

Expand All @@ -56,13 +58,13 @@ public static CultureInfo Culture
static I18NExtension()
{
var target = new ExpandoObject();
Target = target;
Target = target;
Notifier = new ResourceChangedNotifier(target);
#if AVALONIA
ExpandoObjectPropertyAccessorPlugin.Register(target); //Register accessor plugin for ExpandoObject
var updateActions = new List<Action>();
#endif
foreach (var type in Assembly.GetEntryAssembly()?.GetTypes() ?? Type.EmptyTypes)
foreach (var type in (I18NAssemblyProvider.Assembly ?? Assembly.GetEntryAssembly())?.GetTypes() ?? Type.EmptyTypes)
{
if (!type.IsSubclassOf(typeof(ResourceProviderBase))) continue;
#if WPF
Expand Down Expand Up @@ -104,7 +106,6 @@ static I18NExtension()
#endif
);


private static readonly DependencyProperty TargetPropertyProperty = DependencyProperty.RegisterAttached
#if AVALONIA
<I18NExtension, DependencyObject, DependencyProperty>
Expand All @@ -125,7 +126,7 @@ private static void SetTargetProperty(DependencyObject element, DependencyProper
private static DependencyProperty GetTargetProperty(DependencyObject element)
=> (DependencyProperty)element.GetValue(TargetPropertyProperty)!;

#endregion
#endregion Target

public static string? Translate(string key, string? fallbackValue = null)
{
Expand Down Expand Up @@ -176,7 +177,7 @@ public I18NExtension()
private readonly DependencyObject proxy = new();

/// <summary>
/// Resource key, accepts <see cref="string"/> or <see cref="Binding"/>.
/// Resource key, accepts <see cref="string"/> or <see cref="Binding"/>.
/// If Keys not null, Key will be the template of <see cref="string.Format(string,object[])"/>
/// </summary>
[DefaultValue(null)]
Expand All @@ -200,7 +201,8 @@ public object? Key
#elif AVALONIA
Collection<IBinding>
#endif
Keys { get; } = new();
Keys
{ get; } = new();

/// <summary>
/// Same as <see cref="Binding"/>.<see cref="Binding.Converter"/>
Expand All @@ -227,8 +229,8 @@ public object? Key
{
keyBinding = new Binding(key)
{
Source = Target,
Mode = BindingMode.OneWay,
Source = Target,
Mode = BindingMode.OneWay,
FallbackValue = key,
#if AVALONIA
Priority = BindingPriority.LocalValue
Expand All @@ -239,15 +241,15 @@ public object? Key
#if WPF
keyBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
#endif
keyBinding.Converter = Converter;
keyBinding.Converter = Converter;
keyBinding.ConverterParameter = ConverterParameter;
return keyBinding;
}
}

var ret = new MultiBinding
{
Mode = BindingMode.OneWay,
Mode = BindingMode.OneWay,
ConverterParameter = ConverterParameter,
#if WPF
UpdateSourceTrigger = UpdateSourceTrigger.Explicit,
Expand All @@ -260,7 +262,7 @@ public object? Key
var source = new Binding(nameof(Notifier.Source))
{
Source = Notifier,
Mode = BindingMode.OneWay
Mode = BindingMode.OneWay
};
var isBindingList = new List<bool>();
ret.Bindings.Add(source);
Expand All @@ -275,14 +277,16 @@ public object? Key
throw new ArgumentNullException($"Language key should be specified");
ret.Bindings.Add(new Binding(languageBinding.Key)
{
Source = Target,
Mode = BindingMode.OneWay,
Source = Target,
Mode = BindingMode.OneWay,
FallbackValue = languageBinding.Key
});
break;

case BindingBase propBinding:
ret.Bindings.Add(propBinding);
break;

default:
throw new ArgumentException(
$"{nameof(Keys)} only accept {typeof(LanguageBinding)} or {typeof(Binding)} current type is {bindingBase.GetType()}");
Expand All @@ -293,7 +297,7 @@ public object? Key

ret.Converter = new MultiValueLangConverter(isBindingList.ToArray())
{
Converter = Converter,
Converter = Converter,
ConverterParameter = ConverterParameter
};
return ret;
Expand All @@ -311,7 +315,7 @@ public override object ProvideValue(IServiceProvider serviceProvider)
#if WPF
return this;
#elif AVALONIA
{
{
// Avalonia please fix TargetObject not match
if (provideValueTarget.TargetObject is not I18NExtension) return this;
var reflect = provideValueTarget.GetType().GetField("ParentsStack");
Expand Down Expand Up @@ -348,24 +352,23 @@ public override object ProvideValue(IServiceProvider serviceProvider)
;
}


private void I18NExtension_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
#if WPF
switch (sender)
{
case FrameworkElement element:
{
element.DataContextChanged -= I18NExtension_DataContextChanged;
ResetBinding(element);
break;
}
{
element.DataContextChanged -= I18NExtension_DataContextChanged;
ResetBinding(element);
break;
}
case FrameworkContentElement element:
{
element.DataContextChanged -= I18NExtension_DataContextChanged;
ResetBinding(element);
break;
}
{
element.DataContextChanged -= I18NExtension_DataContextChanged;
ResetBinding(element);
break;
}
}
#elif AVALONIA
if (sender is not StyledElement element) return;
Expand All @@ -383,6 +386,7 @@ private void SetTarget(DependencyObject targetObject, DependencyProperty targetP
SetTargetProperty(element, targetProperty);
element.DataContextChanged += I18NExtension_DataContextChanged;
return;

case FrameworkElement element:
SetTargetProperty(element, targetProperty);
element.DataContextChanged += I18NExtension_DataContextChanged;
Expand All @@ -408,7 +412,6 @@ private void ResetBinding(DependencyObject element)
#endif
}


public void AddChild(object value)
{
if (value is not Binding binding) return;
Expand All @@ -420,7 +423,6 @@ public void AddText(string key)
Keys.Add(new LanguageBinding(key));
}


/// <summary>
/// use <see cref="string.Format(string,object[])"/> to generate final text
/// </summary>
Expand All @@ -432,8 +434,8 @@ private class MultiValueLangConverter(
#endif
isBindingList) : IMultiValueConverter
{
public IValueConverter? Converter { get; set; }
public object? ConverterParameter { get; set; }
public IValueConverter? Converter { get; set; }
public object? ConverterParameter { get; set; }

public object? Convert(
#if WPF
Expand Down Expand Up @@ -519,4 +521,4 @@ public void RegisterProvider(ResourceProviderBase provider)
public void ForceUpdate() => OnPropertyChanged(nameof(Source));
#endif
}
}
}