Skip to content

Commit

Permalink
Fix string template arg type
Browse files Browse the repository at this point in the history
  • Loading branch information
feast107 committed Dec 31, 2023
1 parent 446b07f commit 05af0a8
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 25 deletions.
25 changes: 12 additions & 13 deletions src/Antelcat.I18N.Shared/I18NExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,38 +213,37 @@ private class MultiValueLangConverter(
Count
#endif
;
var res = new object?[count - 2];
var args = new object?[count - 2];
var template = isBindingList[0]
? GetValue(source, values[1] as string)
: values[1] as string;
? GetValue(source, values[1]?.ToString())
: values[1]?.ToString();
if (string.IsNullOrEmpty(template) || count <= 2)
return Converter?.Convert(template, targetType, ConverterParameter, culture) ?? template;

for (var i = 1; i < isBindingList.Count; i++)
{
if (values[i + 1] == null)
var curr = values[i + 1];
if (curr == null)
{
res[i - 1] = string.Empty;
args[i - 1] = string.Empty;
continue;
}

res[i - 1] = isBindingList[i]
? GetValue(source, values[i + 1] as string)
: values[i + 1] as string;
args[i - 1] = isBindingList[i]
? GetValue(source, curr.ToString())
: curr.ToString();
}

var val = string.Format(template!, res);
var val = string.Format(template!, args);
return Converter?.Convert(val, targetType, ConverterParameter, culture) ?? val;
}

private static string GetValue(object source, string? key)
{
return key == null
private static string GetValue(object source, string? key) =>
key == null
? string.Empty
: ((IDictionary<string, object?>)source).TryGetValue(key, out var value)
? value as string ?? key
: key;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => throw new NotSupportedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected override void GenerateCode(SourceProductionContext context,
var nameSpace = generateCtx.TargetSymbol.ContainingNamespace.GetFullyQualifiedName();
var className = $"__{targetSymbol.Name}Provider";
var syntaxTriviaList = TriviaList(
Comment("// <auto-generated/>"),
Comment("// <auto-generated/> By Antelcat.I18N.SourceGenerators"),
Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true)),
Trivia(NullableDirectiveTrivia(Token(SyntaxKind.EnableKeyword), true)));
var unit = CompilationUnit()
Expand Down
8 changes: 4 additions & 4 deletions src/Avalonia/Avalonia/Antelcat.I18N.Avalonia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
<IsPackable>true</IsPackable>
<LangVersion>preview</LangVersion>

<Version>1.0.0</Version>
<FileVersion>1.0.0</FileVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
<Version>1.0.1</Version>
<FileVersion>1.0.1</FileVersion>
<AssemblyVersion>1.0.1</AssemblyVersion>

<Authors>Antelcat</Authors>
<Title>Antelcat.I18N.Avalonia</Title>
<PackageId>Antelcat.I18N.Avalonia</PackageId>
<RootNamespace>Antelcat.I18N.Avalonia</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageReleaseNotes>Initial release</PackageReleaseNotes>
<PackageReleaseNotes>Fix string template using args of none string type</PackageReleaseNotes>
<Copyright>Copyright Antelcat. All rights reserved</Copyright>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<PackageTags>dotnet;Avalonia;markup;extension;MVVM;i18n;language;binding;.NET;</PackageTags>
Expand Down
2 changes: 1 addition & 1 deletion src/WPF/Library/Antelcat.I18N.WPF.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<RootNamespace>Antelcat.I18N.WPF.Library</RootNamespace>
<LangVersion>preview</LangVersion>
<LangVersion>10</LangVersion>
</PropertyGroup>


Expand Down
8 changes: 4 additions & 4 deletions src/WPF/WPF/Antelcat.I18N.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
<IsPackable>true</IsPackable>
<LangVersion>preview</LangVersion>

<Version>1.0.0</Version>
<FileVersion>1.0.0</FileVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
<Version>1.0.1</Version>
<FileVersion>1.0.1</FileVersion>
<AssemblyVersion>1.0.1</AssemblyVersion>

<Authors>Antelcat</Authors>
<Title>Antelcat.I18N.WPF</Title>
<PackageId>Antelcat.I18N.WPF</PackageId>
<RootNamespace>Antelcat.I18N.WPF</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageReleaseNotes>Initial release</PackageReleaseNotes>
<PackageReleaseNotes>Fix string template using args of none string type</PackageReleaseNotes>
<Copyright>Copyright Antelcat. All rights reserved</Copyright>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<PackageTags>dotnet;WPF;markup;extension;MVVM;i18n;language;binding;.NET;</PackageTags>
Expand Down
2 changes: 0 additions & 2 deletions src/WPF/WPF/I18NExtension.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Collections.Specialized;
using System.ComponentModel;
using System.Dynamic;
using System.Reflection;
using System.Runtime.Serialization;
using System.Windows.Data;
using System.Windows.Markup;
using Antelcat.I18N.Abstractions;
Expand Down

0 comments on commit 05af0a8

Please sign in to comment.