From 3d44a59c3dfd4e356cb36eb0c1fae44ca28e1e27 Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Sat, 16 Nov 2024 01:33:13 +0300 Subject: [PATCH] Use new features & add docs --- .../LocalizableStringExtractor.cs | 21 ++-- .../CSharpProjectProcessor.cs | 3 +- .../CSharpMetadataProvider.cs | 5 +- .../PluralStringExtractor.cs | 95 +++++++++---------- .../SingularStringExtractor.cs | 13 +-- .../VisualBasicMetadataProvider.cs | 5 +- .../PluralStringExtractor.cs | 14 +-- .../SingularStringExtractor.cs | 15 +-- .../VisualBasicProjectProcessor.cs | 3 +- ...playAttributeDescriptionStringExtractor.cs | 15 ++- ...isplayAttributeGroupNameStringExtractor.cs | 15 ++- .../DisplayAttributeNameStringExtractor.cs | 26 +++-- ...isplayAttributeShortNameStringExtractor.cs | 26 +++-- .../DisplayAttributeStringExtractor.cs | 59 +++++------- .../ErrorMessageAnnotationStringExtractor.cs | 61 +++++------- .../ExtractingCodeWalker.cs | 52 ++++------ .../LocalizerAccessors.cs | 51 +++++----- .../ExtractingLiquidWalker.cs | 23 ++--- .../LiquidProjectProcessor.cs | 14 +-- .../LiquidStringExtractor.cs | 15 ++- .../LiquidMetadataProvider.cs | 74 +++++++-------- 21 files changed, 252 insertions(+), 353 deletions(-) diff --git a/src/OrchardCoreContrib.PoExtractor.Abstractions/LocalizableStringExtractor.cs b/src/OrchardCoreContrib.PoExtractor.Abstractions/LocalizableStringExtractor.cs index 6933b71..a314798 100644 --- a/src/OrchardCoreContrib.PoExtractor.Abstractions/LocalizableStringExtractor.cs +++ b/src/OrchardCoreContrib.PoExtractor.Abstractions/LocalizableStringExtractor.cs @@ -6,18 +6,13 @@ namespace OrchardCoreContrib.PoExtractor; /// Represents a base class for extracting a localizable strings. /// /// The type of the node. -public abstract class LocalizableStringExtractor : IStringExtractor +/// +/// Creates a new instance of a . +/// +/// The . +public abstract class LocalizableStringExtractor(IMetadataProvider metadataProvider) : IStringExtractor { - /// - /// Creates a new instance of a . - /// - /// The . - public LocalizableStringExtractor(IMetadataProvider metadataProvider) - { - MetadataProvider = metadataProvider ?? throw new ArgumentNullException(nameof(metadataProvider)); - } - - protected IMetadataProvider MetadataProvider { get; } + protected IMetadataProvider MetadataProvider { get; } = metadataProvider ?? throw new ArgumentNullException(nameof(metadataProvider)); /// public abstract bool TryExtract(TNode node, out LocalizableStringOccurence result); @@ -39,8 +34,8 @@ protected LocalizableStringOccurence CreateLocalizedString(string text, string t { Text = text, TextPlural = textPlural, - Location = MetadataProvider.GetLocation(node), - Context = MetadataProvider.GetContext(node) + Location = metadataProvider.GetLocation(node), + Context = metadataProvider.GetContext(node) }; return result; diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet.CS/CSharpProjectProcessor.cs b/src/OrchardCoreContrib.PoExtractor.DotNet.CS/CSharpProjectProcessor.cs index 58bcb6d..72e9a0c 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet.CS/CSharpProjectProcessor.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet.CS/CSharpProjectProcessor.cs @@ -1,5 +1,4 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp; using OrchardCoreContrib.PoExtractor.DotNet.CS.MetadataProviders; using System; using System.IO; diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet.CS/MetadataProviders/CSharpMetadataProvider.cs b/src/OrchardCoreContrib.PoExtractor.DotNet.CS/MetadataProviders/CSharpMetadataProvider.cs index 7106612..a5b4d97 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet.CS/MetadataProviders/CSharpMetadataProvider.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet.CS/MetadataProviders/CSharpMetadataProvider.cs @@ -18,10 +18,7 @@ public class CSharpMetadataProvider : IMetadataProvider /// The base path. public CSharpMetadataProvider(string basePath) { - if (string.IsNullOrEmpty(basePath)) - { - throw new ArgumentException($"'{nameof(basePath)}' cannot be null or empty.", nameof(basePath)); - } + ArgumentException.ThrowIfNullOrEmpty(basePath, nameof(basePath)); _basePath = basePath; } diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet.CS/PluralStringExtractor.cs b/src/OrchardCoreContrib.PoExtractor.DotNet.CS/PluralStringExtractor.cs index 411c8e6..17bf3ca 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet.CS/PluralStringExtractor.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet.CS/PluralStringExtractor.cs @@ -4,72 +4,65 @@ using System; using System.Linq; -namespace OrchardCoreContrib.PoExtractor.DotNet.CS +namespace OrchardCoreContrib.PoExtractor.DotNet.CS; + +/// +/// Extracts with the singular text from the C# AST node +/// +/// +/// The localizable string is identified by the name convention - T.Plural(count, "1 book", "{0} books") +/// +/// +/// Creates a new instance of a . +/// +/// The . +public class PluralStringExtractor(IMetadataProvider metadataProvider) : LocalizableStringExtractor(metadataProvider) { - /// - /// Extracts with the singular text from the C# AST node - /// - /// - /// The localizable string is identified by the name convention - T.Plural(count, "1 book", "{0} books") - /// - public class PluralStringExtractor : LocalizableStringExtractor + + /// + public override bool TryExtract(SyntaxNode node, out LocalizableStringOccurence result) { - /// - /// Creates a new instance of a . - /// - /// The . - public PluralStringExtractor(IMetadataProvider metadataProvider) : base(metadataProvider) - { - } + ArgumentNullException.ThrowIfNull(nameof(node)); - /// - public override bool TryExtract(SyntaxNode node, out LocalizableStringOccurence result) - { - if (node is null) - { - throw new ArgumentNullException(nameof(node)); - } + result = null; - result = null; + if (node is InvocationExpressionSyntax invocation && + invocation.Expression is MemberAccessExpressionSyntax accessor && + accessor.Expression is IdentifierNameSyntax identifierName && + LocalizerAccessors.LocalizerIdentifiers.Contains(identifierName.Identifier.Text) && + accessor.Name.Identifier.Text == "Plural") + { - if (node is InvocationExpressionSyntax invocation && - invocation.Expression is MemberAccessExpressionSyntax accessor && - accessor.Expression is IdentifierNameSyntax identifierName && - LocalizerAccessors.LocalizerIdentifiers.Contains(identifierName.Identifier.Text) && - accessor.Name.Identifier.Text == "Plural") + var arguments = invocation.ArgumentList.Arguments; + if (arguments.Count >= 2 && + arguments[1].Expression is ArrayCreationExpressionSyntax array) { - - var arguments = invocation.ArgumentList.Arguments; - if (arguments.Count >= 2 && - arguments[1].Expression is ArrayCreationExpressionSyntax array) + if (array.Type.ElementType is PredefinedTypeSyntax arrayType && + arrayType.Keyword.Text == "string" && + array.Initializer.Expressions.Count >= 2 && + array.Initializer.Expressions[0] is LiteralExpressionSyntax singularLiteral && singularLiteral.IsKind(SyntaxKind.StringLiteralExpression) && + array.Initializer.Expressions[1] is LiteralExpressionSyntax pluralLiteral && pluralLiteral.IsKind(SyntaxKind.StringLiteralExpression)) { - if (array.Type.ElementType is PredefinedTypeSyntax arrayType && - arrayType.Keyword.Text == "string" && - array.Initializer.Expressions.Count >= 2 && - array.Initializer.Expressions[0] is LiteralExpressionSyntax singularLiteral && singularLiteral.IsKind(SyntaxKind.StringLiteralExpression) && - array.Initializer.Expressions[1] is LiteralExpressionSyntax pluralLiteral && pluralLiteral.IsKind(SyntaxKind.StringLiteralExpression)) - { - result = CreateLocalizedString(singularLiteral.Token.ValueText, pluralLiteral.Token.ValueText, node); + result = CreateLocalizedString(singularLiteral.Token.ValueText, pluralLiteral.Token.ValueText, node); - return true; - } + return true; } - else + } + else + { + if (arguments.Count >= 3 && + arguments[1].Expression is LiteralExpressionSyntax singularLiteral && singularLiteral.IsKind(SyntaxKind.StringLiteralExpression) && + arguments[2].Expression is LiteralExpressionSyntax pluralLiteral && pluralLiteral.IsKind(SyntaxKind.StringLiteralExpression)) { - if (arguments.Count >= 3 && - arguments[1].Expression is LiteralExpressionSyntax singularLiteral && singularLiteral.IsKind(SyntaxKind.StringLiteralExpression) && - arguments[2].Expression is LiteralExpressionSyntax pluralLiteral && pluralLiteral.IsKind(SyntaxKind.StringLiteralExpression)) - { - result = CreateLocalizedString(singularLiteral.Token.ValueText, pluralLiteral.Token.ValueText, node); + result = CreateLocalizedString(singularLiteral.Token.ValueText, pluralLiteral.Token.ValueText, node); - return true; - } + return true; } } - - return false; } + + return false; } } diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet.CS/SingularStringExtractor.cs b/src/OrchardCoreContrib.PoExtractor.DotNet.CS/SingularStringExtractor.cs index a43ea72..1653d3c 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet.CS/SingularStringExtractor.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet.CS/SingularStringExtractor.cs @@ -12,15 +12,12 @@ namespace OrchardCoreContrib.PoExtractor.DotNet.CS; /// /// The localizable string is identified by the name convention - T["TEXT TO TRANSLATE"] /// -public class SingularStringExtractor : LocalizableStringExtractor +/// +/// Creates a new instance of a . +/// +/// The . +public class SingularStringExtractor(IMetadataProvider metadataProvider) : LocalizableStringExtractor(metadataProvider) { - /// - /// Creates a new instance of a . - /// - /// The . - public SingularStringExtractor(IMetadataProvider metadataProvider) : base(metadataProvider) - { - } /// public override bool TryExtract(SyntaxNode node, out LocalizableStringOccurence result) diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet.VB/MetadataProviders/VisualBasicMetadataProvider.cs b/src/OrchardCoreContrib.PoExtractor.DotNet.VB/MetadataProviders/VisualBasicMetadataProvider.cs index 11ed87e..3c87497 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet.VB/MetadataProviders/VisualBasicMetadataProvider.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet.VB/MetadataProviders/VisualBasicMetadataProvider.cs @@ -18,10 +18,7 @@ public class VisualBasicMetadataProvider : IMetadataProvider /// The base path. public VisualBasicMetadataProvider(string basePath) { - if (string.IsNullOrEmpty(basePath)) - { - throw new ArgumentException($"'{nameof(basePath)}' cannot be null or empty.", nameof(basePath)); - } + ArgumentException.ThrowIfNullOrEmpty(basePath, nameof(basePath)); _basePath = basePath; } diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet.VB/PluralStringExtractor.cs b/src/OrchardCoreContrib.PoExtractor.DotNet.VB/PluralStringExtractor.cs index 9e5eadb..b07a039 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet.VB/PluralStringExtractor.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet.VB/PluralStringExtractor.cs @@ -12,16 +12,12 @@ namespace OrchardCoreContrib.PoExtractor.DotNet.VB; /// /// The localizable string is identified by the name convention - T.Plural(count, "1 book", "{0} books"). /// -public class PluralStringExtractor : LocalizableStringExtractor +/// +/// Creates a new instance of a . +/// +/// The . +public class PluralStringExtractor(IMetadataProvider metadataProvider) : LocalizableStringExtractor(metadataProvider) { - /// - /// Creates a new instance of a . - /// - /// The . - public PluralStringExtractor(IMetadataProvider metadataProvider) : base(metadataProvider) - { - - } /// public override bool TryExtract(SyntaxNode node, out LocalizableStringOccurence result) diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet.VB/SingularStringExtractor.cs b/src/OrchardCoreContrib.PoExtractor.DotNet.VB/SingularStringExtractor.cs index 91fe4fa..d0961e7 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet.VB/SingularStringExtractor.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet.VB/SingularStringExtractor.cs @@ -12,17 +12,12 @@ namespace OrchardCoreContrib.PoExtractor.DotNet.VB; /// /// The localizable string is identified by the name convention - T["TEXT TO TRANSLATE"] /// -public class SingularStringExtractor : LocalizableStringExtractor +/// +/// Creates a new instance of a . +/// +/// The . +public class SingularStringExtractor(IMetadataProvider metadataProvider) : LocalizableStringExtractor(metadataProvider) { - /// - /// Creates a new instance of a . - /// - /// The . - public SingularStringExtractor(IMetadataProvider metadataProvider) : base(metadataProvider) - { - - } - /// public override bool TryExtract(SyntaxNode node, out LocalizableStringOccurence result) { diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet.VB/VisualBasicProjectProcessor.cs b/src/OrchardCoreContrib.PoExtractor.DotNet.VB/VisualBasicProjectProcessor.cs index 8d7a150..dc7c97f 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet.VB/VisualBasicProjectProcessor.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet.VB/VisualBasicProjectProcessor.cs @@ -1,5 +1,4 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.VisualBasic; +using Microsoft.CodeAnalysis.VisualBasic; using OrchardCoreContrib.PoExtractor.DotNet.VB.MetadataProviders; using System; using System.IO; diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeDescriptionStringExtractor.cs b/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeDescriptionStringExtractor.cs index 7b876e3..46470bf 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeDescriptionStringExtractor.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeDescriptionStringExtractor.cs @@ -6,14 +6,11 @@ namespace OrchardCoreContrib.PoExtractor.DotNet; /// /// Extracts localizable string from Description property. /// -public class DisplayAttributeDescriptionStringExtractor : DisplayAttributeStringExtractor +/// +/// Creates a new instance of a . +/// +/// The . +public class DisplayAttributeDescriptionStringExtractor(IMetadataProvider metadataProvider) + : DisplayAttributeStringExtractor("Description", metadataProvider) { - /// - /// Creates a new instance of a . - /// - /// The . - public DisplayAttributeDescriptionStringExtractor(IMetadataProvider metadataProvider) - : base("Description", metadataProvider) - { - } } diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeGroupNameStringExtractor.cs b/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeGroupNameStringExtractor.cs index 56b8dae..f94f037 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeGroupNameStringExtractor.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeGroupNameStringExtractor.cs @@ -6,14 +6,11 @@ namespace OrchardCoreContrib.PoExtractor.DotNet; /// /// Extracts localizable string from GroupName property. /// -public class DisplayAttributeGroupNameStringExtractor : DisplayAttributeStringExtractor +/// +/// Creates a new instance of a . +/// +/// The . +public class DisplayAttributeGroupNameStringExtractor(IMetadataProvider metadataProvider) + : DisplayAttributeStringExtractor("GroupName", metadataProvider) { - /// - /// Creates a new instance of a . - /// - /// The . - public DisplayAttributeGroupNameStringExtractor(IMetadataProvider metadataProvider) - : base("GroupName", metadataProvider) - { - } } diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeNameStringExtractor.cs b/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeNameStringExtractor.cs index 2583a27..9236fab 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeNameStringExtractor.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeNameStringExtractor.cs @@ -1,20 +1,16 @@ using Microsoft.CodeAnalysis; using System.ComponentModel.DataAnnotations; -namespace OrchardCoreContrib.PoExtractor.DotNet +namespace OrchardCoreContrib.PoExtractor.DotNet; + +/// +/// Extracts localizable string from Name property. +/// +/// +/// Creates a new instanceof a . +/// +/// The . +public class DisplayAttributeNameStringExtractor(IMetadataProvider metadataProvider) + : DisplayAttributeStringExtractor("Name", metadataProvider) { - /// - /// Extracts localizable string from Name property. - /// - public class DisplayAttributeNameStringExtractor : DisplayAttributeStringExtractor - { - /// - /// Creates a new instanceof a . - /// - /// The . - public DisplayAttributeNameStringExtractor(IMetadataProvider metadataProvider) - : base("Name", metadataProvider) - { - } - } } diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeShortNameStringExtractor.cs b/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeShortNameStringExtractor.cs index b139a06..4693dc3 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeShortNameStringExtractor.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeShortNameStringExtractor.cs @@ -1,20 +1,16 @@ using Microsoft.CodeAnalysis; using System.ComponentModel.DataAnnotations; -namespace OrchardCoreContrib.PoExtractor.DotNet +namespace OrchardCoreContrib.PoExtractor.DotNet; + +/// +/// Extracts localizable string from ShortName property. +/// +/// +/// Creates a new instance of a . +/// +/// The . +public class DisplayAttributeShortNameStringExtractor(IMetadataProvider metadataProvider) + : DisplayAttributeStringExtractor("ShortName", metadataProvider) { - /// - /// Extracts localizable string from ShortName property. - /// - public class DisplayAttributeShortNameStringExtractor : DisplayAttributeStringExtractor - { - /// - /// Creates a new instance of a . - /// - /// The . - public DisplayAttributeShortNameStringExtractor(IMetadataProvider metadataProvider) - : base("ShortName", metadataProvider) - { - } - } } diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeStringExtractor.cs b/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeStringExtractor.cs index cc8b3af..ad502f1 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeStringExtractor.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet/DisplayAttributeStringExtractor.cs @@ -3,44 +3,37 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using System.ComponentModel.DataAnnotations; -namespace OrchardCoreContrib.PoExtractor.DotNet +namespace OrchardCoreContrib.PoExtractor.DotNet; + +/// +/// Extracts localizable string from . +/// +/// +/// Creates a new instance of a . +/// +/// The argument name. +/// The . +public abstract class DisplayAttributeStringExtractor(string argumentName, IMetadataProvider metadataProvider) + : LocalizableStringExtractor(metadataProvider) { - /// - /// Extracts localizable string from . - /// - public abstract class DisplayAttributeStringExtractor : LocalizableStringExtractor + private const string DisplayAttributeName = "Display"; + + /// + public override bool TryExtract(SyntaxNode node, out LocalizableStringOccurence result) { - private const string DisplayAttributeName = "Display"; - private readonly string _argumentName; + result = null; - /// - /// Creates a new instance of a . - /// - /// The argument name. - /// The . - protected DisplayAttributeStringExtractor(string argumentName, IMetadataProvider metadataProvider) - : base(metadataProvider) + if (node is AttributeArgumentSyntax argument + && argument.Expression.Parent.ToFullString().StartsWith(argumentName) + && node.Parent?.Parent is AttributeSyntax accessor + && accessor.Name.ToString() == DisplayAttributeName + && argument.Expression is LiteralExpressionSyntax literal + && literal.IsKind(SyntaxKind.StringLiteralExpression)) { - _argumentName = argumentName; + result = CreateLocalizedString(literal.Token.ValueText, null, node); + return true; } - /// - public override bool TryExtract(SyntaxNode node, out LocalizableStringOccurence result) - { - result = null; - - if (node is AttributeArgumentSyntax argument - && argument.Expression.Parent.ToFullString().StartsWith(_argumentName) - && node.Parent?.Parent is AttributeSyntax accessor - && accessor.Name.ToString() == DisplayAttributeName - && argument.Expression is LiteralExpressionSyntax literal - && literal.IsKind(SyntaxKind.StringLiteralExpression)) - { - result = CreateLocalizedString(literal.Token.ValueText, null, node); - return true; - } - - return false; - } + return false; } } diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet/ErrorMessageAnnotationStringExtractor.cs b/src/OrchardCoreContrib.PoExtractor.DotNet/ErrorMessageAnnotationStringExtractor.cs index bfb8fb6..4361200 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet/ErrorMessageAnnotationStringExtractor.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet/ErrorMessageAnnotationStringExtractor.cs @@ -4,49 +4,40 @@ using System; using System.Linq; -namespace OrchardCoreContrib.PoExtractor.DotNet +namespace OrchardCoreContrib.PoExtractor.DotNet; + +/// +/// Extracts localizable string from data annotations error messages. +/// +/// +/// Creates a new instance of a . +/// +/// The . +public class ErrorMessageAnnotationStringExtractor(IMetadataProvider metadataProvider) + : LocalizableStringExtractor(metadataProvider) { - /// - /// Extracts localizable string from data annotations error messages. - /// - public class ErrorMessageAnnotationStringExtractor : LocalizableStringExtractor + private const string ErrorMessageAttributeName = "ErrorMessage"; + + /// + public override bool TryExtract(SyntaxNode node, out LocalizableStringOccurence result) { - private const string ErrorMessageAttributeName = "ErrorMessage"; + ArgumentNullException.ThrowIfNull(node, nameof(node)); - /// - /// Creates a new instance of a . - /// - /// The . - public ErrorMessageAnnotationStringExtractor(IMetadataProvider metadataProvider) - : base(metadataProvider) - { + result = null; - } - - /// - public override bool TryExtract(SyntaxNode node, out LocalizableStringOccurence result) + if (node is AttributeSyntax accessor && accessor.ArgumentList != null) { - if (node is null) - { - throw new ArgumentNullException(nameof(node)); - } - - result = null; + var argument = accessor.ArgumentList.Arguments + .Where(a => a.Expression.Parent.ToFullString().StartsWith(ErrorMessageAttributeName)) + .FirstOrDefault(); - if (node is AttributeSyntax accessor && accessor.ArgumentList != null) + if (argument != null && argument.Expression is LiteralExpressionSyntax literal && literal.IsKind(SyntaxKind.StringLiteralExpression)) { - var argument = accessor.ArgumentList.Arguments - .Where(a => a.Expression.Parent.ToFullString().StartsWith(ErrorMessageAttributeName)) - .FirstOrDefault(); - - if (argument != null && argument.Expression is LiteralExpressionSyntax literal && literal.IsKind(SyntaxKind.StringLiteralExpression)) - { - result = CreateLocalizedString(literal.Token.ValueText, null, node); - return true; - } + result = CreateLocalizedString(literal.Token.ValueText, null, node); + return true; } - - return false; } + + return false; } } diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet/ExtractingCodeWalker.cs b/src/OrchardCoreContrib.PoExtractor.DotNet/ExtractingCodeWalker.cs index 57515bb..b3defeb 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet/ExtractingCodeWalker.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet/ExtractingCodeWalker.cs @@ -2,43 +2,33 @@ using System; using System.Collections.Generic; -namespace OrchardCoreContrib.PoExtractor.DotNet +namespace OrchardCoreContrib.PoExtractor.DotNet; + +/// +/// Traverses C# & VB AST and extracts localizable strings using provided collection of +/// +/// +/// Initializes a new instance of the class. +/// +/// the collection of extractors to use +/// The where the results are saved. +public class ExtractingCodeWalker(IEnumerable> extractors, LocalizableStringCollection strings) : SyntaxWalker { - /// - /// Traverses C# & VB AST and extracts localizable strings using provided collection of - /// - public class ExtractingCodeWalker : SyntaxWalker + private readonly LocalizableStringCollection _strings = strings ?? throw new ArgumentNullException(nameof(strings)); + private readonly IEnumerable> _extractors = extractors ?? throw new ArgumentNullException(nameof(extractors)); + + /// + public override void Visit(SyntaxNode node) { - private readonly LocalizableStringCollection _strings; - private readonly IEnumerable> _extractors; + ArgumentNullException.ThrowIfNull(node, nameof(node)); - /// - /// Initializes a new instance of the class. - /// - /// the collection of extractors to use - /// The where the results are saved. - public ExtractingCodeWalker(IEnumerable> extractors, LocalizableStringCollection strings) - { - _extractors = extractors ?? throw new ArgumentNullException(nameof(extractors)); - _strings = strings ?? throw new ArgumentNullException(nameof(strings)); - } + base.Visit(node); - /// - public override void Visit(SyntaxNode node) + foreach (var extractor in _extractors) { - if (node is null) - { - throw new ArgumentNullException(nameof(node)); - } - - base.Visit(node); - - foreach (var extractor in _extractors) + if (extractor.TryExtract(node, out var result)) { - if (extractor.TryExtract(node, out var result)) - { - _strings.Add(result); - } + _strings.Add(result); } } } diff --git a/src/OrchardCoreContrib.PoExtractor.DotNet/LocalizerAccessors.cs b/src/OrchardCoreContrib.PoExtractor.DotNet/LocalizerAccessors.cs index 95c05b5..f64d841 100644 --- a/src/OrchardCoreContrib.PoExtractor.DotNet/LocalizerAccessors.cs +++ b/src/OrchardCoreContrib.PoExtractor.DotNet/LocalizerAccessors.cs @@ -1,33 +1,32 @@ -namespace OrchardCoreContrib.PoExtractor.DotNet +namespace OrchardCoreContrib.PoExtractor.DotNet; + +/// +/// Represents a class that contains a set of localizer identifier accessors. +/// +public static class LocalizerAccessors { /// - /// Represents a class that contains a set of localizer identifier accessors. + /// Gets the localizer identifier for IStringLocalizer or IHtmlStringLocalizer in views. /// - public static class LocalizerAccessors - { - /// - /// Gets the localizer identifier for IStringLocalizer or IHtmlStringLocalizer in views. - /// - public static readonly string DefaultLocalizerIdentifier = "T"; + public static readonly string DefaultLocalizerIdentifier = "T"; - /// - /// Gets the localizer identifier for IStringLocalizer. - /// - public static readonly string StringLocalizerIdentifier = "S"; + /// + /// Gets the localizer identifier for IStringLocalizer. + /// + public static readonly string StringLocalizerIdentifier = "S"; - /// - /// Gets the localizer identifier for IHtmlStringLocalizer. - /// - public static readonly string HtmlLocalizerIdentifier = "H"; + /// + /// Gets the localizer identifier for IHtmlStringLocalizer. + /// + public static readonly string HtmlLocalizerIdentifier = "H"; - /// - /// Gets the localizer identifiers. - /// - public static string[] LocalizerIdentifiers = - [ - DefaultLocalizerIdentifier, - StringLocalizerIdentifier, - HtmlLocalizerIdentifier - ]; - } + /// + /// Gets the localizer identifiers. + /// + public static string[] LocalizerIdentifiers = + [ + DefaultLocalizerIdentifier, + StringLocalizerIdentifier, + HtmlLocalizerIdentifier + ]; } diff --git a/src/OrchardCoreContrib.PoExtractor.Liquid/ExtractingLiquidWalker.cs b/src/OrchardCoreContrib.PoExtractor.Liquid/ExtractingLiquidWalker.cs index efd6051..e8caa9f 100644 --- a/src/OrchardCoreContrib.PoExtractor.Liquid/ExtractingLiquidWalker.cs +++ b/src/OrchardCoreContrib.PoExtractor.Liquid/ExtractingLiquidWalker.cs @@ -1,5 +1,4 @@ using Fluid.Ast; -using Microsoft.CodeAnalysis; using System; using System.Collections.Generic; @@ -8,23 +7,17 @@ namespace OrchardCoreContrib.PoExtractor.Liquid; /// /// Traverses Fluid AST and extracts localizable strings using provided collection of /// -public class ExtractingLiquidWalker +/// +/// Initializes a new instance of the class +/// +/// the collection of extractors to use +/// the where the results are saved +public class ExtractingLiquidWalker(IEnumerable> extractors, LocalizableStringCollection localizableStrings) { private string _filePath; - private readonly LocalizableStringCollection _localizableStrings; - private readonly IEnumerable> _extractors; - - /// - /// Initializes a new instance of the class - /// - /// the collection of extractors to use - /// the where the results are saved - public ExtractingLiquidWalker(IEnumerable> extractors, LocalizableStringCollection localizableStrings) - { - _extractors = extractors ?? throw new ArgumentNullException(nameof(extractors)); - _localizableStrings = localizableStrings ?? throw new ArgumentNullException(nameof(localizableStrings)); - } + private readonly LocalizableStringCollection _localizableStrings = localizableStrings ?? throw new ArgumentNullException(nameof(localizableStrings)); + private readonly IEnumerable> _extractors = extractors ?? throw new ArgumentNullException(nameof(extractors)); /// /// Visits liquid statement. diff --git a/src/OrchardCoreContrib.PoExtractor.Liquid/LiquidProjectProcessor.cs b/src/OrchardCoreContrib.PoExtractor.Liquid/LiquidProjectProcessor.cs index 26a6848..c447f62 100644 --- a/src/OrchardCoreContrib.PoExtractor.Liquid/LiquidProjectProcessor.cs +++ b/src/OrchardCoreContrib.PoExtractor.Liquid/LiquidProjectProcessor.cs @@ -31,20 +31,12 @@ public LiquidProjectProcessor() /// public void Process(string path, string basePath, LocalizableStringCollection localizableStrings) { - if (string.IsNullOrEmpty(path)) - { - throw new ArgumentException($"'{nameof(path)}' cannot be null or empty.", nameof(path)); - } - - if (string.IsNullOrEmpty(basePath)) - { - throw new ArgumentException($"'{nameof(basePath)}' cannot be null or empty.", nameof(basePath)); - } - + ArgumentException.ThrowIfNullOrEmpty(path, nameof(path)); + ArgumentException.ThrowIfNullOrEmpty(basePath, nameof(basePath)); ArgumentNullException.ThrowIfNull(localizableStrings); var liquidMetadataProvider = new LiquidMetadataProvider(basePath); - var liquidVisitor = new ExtractingLiquidWalker(new[] { new LiquidStringExtractor(liquidMetadataProvider) }, localizableStrings); + var liquidVisitor = new ExtractingLiquidWalker([new LiquidStringExtractor(liquidMetadataProvider)], localizableStrings); foreach (var file in Directory.EnumerateFiles(path, $"*{_liquidExtension}", SearchOption.AllDirectories).OrderBy(file => file)) { diff --git a/src/OrchardCoreContrib.PoExtractor.Liquid/LiquidStringExtractor.cs b/src/OrchardCoreContrib.PoExtractor.Liquid/LiquidStringExtractor.cs index e167a04..2eba8c4 100644 --- a/src/OrchardCoreContrib.PoExtractor.Liquid/LiquidStringExtractor.cs +++ b/src/OrchardCoreContrib.PoExtractor.Liquid/LiquidStringExtractor.cs @@ -9,18 +9,15 @@ namespace OrchardCoreContrib.PoExtractor.Liquid; /// /// The localizable string is identified by the name convention of the filter - "TEXT TO TRANSLATE" | t /// -public class LiquidStringExtractor : LocalizableStringExtractor +/// +/// Creates a new instance of a . +/// +/// The . +public class LiquidStringExtractor(IMetadataProvider metadataProvider) + : LocalizableStringExtractor(metadataProvider) { private static readonly string _localizationFilterName = "t"; - /// - /// Creates a new instance of a . - /// - /// The . - public LiquidStringExtractor(IMetadataProvider metadataProvider) : base(metadataProvider) - { - } - /// public override bool TryExtract(LiquidExpressionContext expressionContext, out LocalizableStringOccurence result) { diff --git a/src/OrchardCoreContrib.PoExtractor.Liquid/MetadataProvider/LiquidMetadataProvider.cs b/src/OrchardCoreContrib.PoExtractor.Liquid/MetadataProvider/LiquidMetadataProvider.cs index 50c2ecf..3771d85 100644 --- a/src/OrchardCoreContrib.PoExtractor.Liquid/MetadataProvider/LiquidMetadataProvider.cs +++ b/src/OrchardCoreContrib.PoExtractor.Liquid/MetadataProvider/LiquidMetadataProvider.cs @@ -1,51 +1,41 @@ using System; using System.IO; -namespace OrchardCoreContrib.PoExtractor.Liquid.MetadataProviders +namespace OrchardCoreContrib.PoExtractor.Liquid.MetadataProviders; + +/// +/// Provides metadata for .liquid files. +/// +public class LiquidMetadataProvider : IMetadataProvider { + private readonly string _basePath; + /// - /// Provides metadata for .liquid files. + /// Creates a new instance of a . /// - public class LiquidMetadataProvider : IMetadataProvider + /// The base path. + public LiquidMetadataProvider(string basePath) + { + ArgumentException.ThrowIfNullOrEmpty(basePath, nameof(basePath)); + + _basePath = basePath; + } + + /// + public string GetContext(LiquidExpressionContext expressionContext) { - private readonly string _basePath; - - /// - /// Creates a new instance of a . - /// - /// The base path. - public LiquidMetadataProvider(string basePath) - { - if (string.IsNullOrEmpty(basePath)) - { - throw new ArgumentException($"'{nameof(basePath)}' cannot be null or empty.", nameof(basePath)); - } - - _basePath = basePath; - } - - /// - public string GetContext(LiquidExpressionContext expressionContext) - { - if (expressionContext is null) - { - throw new ArgumentNullException(nameof(expressionContext)); - } - - var path = expressionContext.FilePath.TrimStart(_basePath); - - return path.Replace(Path.DirectorySeparatorChar, '.').Replace(".liquid", string.Empty); - } - - /// - public LocalizableStringLocation GetLocation(LiquidExpressionContext expressionContext) - { - if (expressionContext is null) - { - throw new ArgumentNullException(nameof(expressionContext)); - } - - return new() { SourceFile = expressionContext.FilePath.TrimStart(_basePath) }; - } + ArgumentNullException.ThrowIfNull(expressionContext, nameof(expressionContext)); + + var path = expressionContext.FilePath.TrimStart(_basePath); + + return path.Replace(Path.DirectorySeparatorChar, '.').Replace(".liquid", string.Empty); + } + + /// + public LocalizableStringLocation GetLocation(LiquidExpressionContext expressionContext) + { + ArgumentNullException.ThrowIfNull(expressionContext, nameof(expressionContext)); + + return new() { SourceFile = expressionContext.FilePath.TrimStart(_basePath) }; } }