Skip to content

Commit

Permalink
Use new features & add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco committed Nov 15, 2024
1 parent e790c92 commit 3d44a59
Show file tree
Hide file tree
Showing 21 changed files with 252 additions and 353 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@ namespace OrchardCoreContrib.PoExtractor;
/// Represents a base class for extracting a localizable strings.
/// </summary>
/// <typeparam name="TNode">The type of the node.</typeparam>
public abstract class LocalizableStringExtractor<TNode> : IStringExtractor<TNode>
/// <remarks>
/// Creates a new instance of a <see cref="LocalizableStringExtractor{T}"/>.
/// </remarks>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{T}"/>.</param>
public abstract class LocalizableStringExtractor<TNode>(IMetadataProvider<TNode> metadataProvider) : IStringExtractor<TNode>
{
/// <summary>
/// Creates a new instance of a <see cref="LocalizableStringExtractor{T}"/>.
/// </summary>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{T}"/>.</param>
public LocalizableStringExtractor(IMetadataProvider<TNode> metadataProvider)
{
MetadataProvider = metadataProvider ?? throw new ArgumentNullException(nameof(metadataProvider));
}

protected IMetadataProvider<TNode> MetadataProvider { get; }
protected IMetadataProvider<TNode> MetadataProvider { get; } = metadataProvider ?? throw new ArgumentNullException(nameof(metadataProvider));

/// <inheritdoc/>
public abstract bool TryExtract(TNode node, out LocalizableStringOccurence result);
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ public class CSharpMetadataProvider : IMetadataProvider<SyntaxNode>
/// <param name="basePath">The base path.</param>
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,65 @@
using System;
using System.Linq;

namespace OrchardCoreContrib.PoExtractor.DotNet.CS
namespace OrchardCoreContrib.PoExtractor.DotNet.CS;

/// <summary>
/// Extracts <see cref="LocalizableStringOccurence"/> with the singular text from the C# AST node
/// </summary>
/// <remarks>
/// The localizable string is identified by the name convention - T.Plural(count, "1 book", "{0} books")
/// </remarks>
/// <remarks>
/// Creates a new instance of a <see cref="PluralStringExtractor"/>.
/// </remarks>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public class PluralStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider) : LocalizableStringExtractor<SyntaxNode>(metadataProvider)
{
/// <summary>
/// Extracts <see cref="LocalizableStringOccurence"/> with the singular text from the C# AST node
/// </summary>
/// <remarks>
/// The localizable string is identified by the name convention - T.Plural(count, "1 book", "{0} books")
/// </remarks>
public class PluralStringExtractor : LocalizableStringExtractor<SyntaxNode>

/// <inheritdoc/>
public override bool TryExtract(SyntaxNode node, out LocalizableStringOccurence result)
{
/// <summary>
/// Creates a new instance of a <see cref="PluralStringExtractor"/>.
/// </summary>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public PluralStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider) : base(metadataProvider)
{
}
ArgumentNullException.ThrowIfNull(nameof(node));

/// <inheritdoc/>
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ namespace OrchardCoreContrib.PoExtractor.DotNet.CS;
/// <remarks>
/// The localizable string is identified by the name convention - T["TEXT TO TRANSLATE"]
/// </remarks>
public class SingularStringExtractor : LocalizableStringExtractor<SyntaxNode>
/// <remarks>
/// Creates a new instance of a <see cref="SingularStringExtractor"/>.
/// </remarks>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public class SingularStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider) : LocalizableStringExtractor<SyntaxNode>(metadataProvider)
{
/// <summary>
/// Creates a new instance of a <see cref="SingularStringExtractor"/>.
/// </summary>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public SingularStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider) : base(metadataProvider)
{
}

/// <inheritdoc/>
public override bool TryExtract(SyntaxNode node, out LocalizableStringOccurence result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ public class VisualBasicMetadataProvider : IMetadataProvider<SyntaxNode>
/// <param name="basePath">The base path.</param>
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ namespace OrchardCoreContrib.PoExtractor.DotNet.VB;
/// <remarks>
/// The localizable string is identified by the name convention - T.Plural(count, "1 book", "{0} books").
/// </remarks>
public class PluralStringExtractor : LocalizableStringExtractor<SyntaxNode>
/// <remarks>
/// Creates a new instance of a <see cref="PluralStringExtractor"/>.
/// </remarks>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public class PluralStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider) : LocalizableStringExtractor<SyntaxNode>(metadataProvider)
{
/// <summary>
/// Creates a new instance of a <see cref="PluralStringExtractor"/>.
/// </summary>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public PluralStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider) : base(metadataProvider)
{

}

/// <inheritdoc/>
public override bool TryExtract(SyntaxNode node, out LocalizableStringOccurence result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@ namespace OrchardCoreContrib.PoExtractor.DotNet.VB;
/// <remarks>
/// The localizable string is identified by the name convention - T["TEXT TO TRANSLATE"]
/// </remarks>
public class SingularStringExtractor : LocalizableStringExtractor<SyntaxNode>
/// <remarks>
/// Creates a new instance of a <see cref="SingularStringExtractor"/>.
/// </remarks>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public class SingularStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider) : LocalizableStringExtractor<SyntaxNode>(metadataProvider)
{
/// <summary>
/// Creates a new instance of a <see cref="SingularStringExtractor"/>.
/// </summary>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public SingularStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider) : base(metadataProvider)
{

}

/// <inheritdoc/>
public override bool TryExtract(SyntaxNode node, out LocalizableStringOccurence result)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ namespace OrchardCoreContrib.PoExtractor.DotNet;
/// <summary>
/// Extracts localizable string from <see cref="DisplayAttribute"/> Description property.
/// </summary>
public class DisplayAttributeDescriptionStringExtractor : DisplayAttributeStringExtractor
/// <remarks>
/// Creates a new instance of a <see cref="DisplayAttributeDescriptionStringExtractor"/>.
/// </remarks>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public class DisplayAttributeDescriptionStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider)
: DisplayAttributeStringExtractor("Description", metadataProvider)
{
/// <summary>
/// Creates a new instance of a <see cref="DisplayAttributeDescriptionStringExtractor"/>.
/// </summary>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public DisplayAttributeDescriptionStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider)
: base("Description", metadataProvider)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ namespace OrchardCoreContrib.PoExtractor.DotNet;
/// <summary>
/// Extracts localizable string from <see cref="DisplayAttribute"/> GroupName property.
/// </summary>
public class DisplayAttributeGroupNameStringExtractor : DisplayAttributeStringExtractor
/// <remarks>
/// Creates a new instance of a <see cref="DisplayAttributeGroupNameStringExtractor"/>.
/// </remarks>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public class DisplayAttributeGroupNameStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider)
: DisplayAttributeStringExtractor("GroupName", metadataProvider)
{
/// <summary>
/// Creates a new instance of a <see cref="DisplayAttributeGroupNameStringExtractor"/>.
/// </summary>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public DisplayAttributeGroupNameStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider)
: base("GroupName", metadataProvider)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
using Microsoft.CodeAnalysis;
using System.ComponentModel.DataAnnotations;

namespace OrchardCoreContrib.PoExtractor.DotNet
namespace OrchardCoreContrib.PoExtractor.DotNet;

/// <summary>
/// Extracts localizable string from <see cref="DisplayAttribute"/> Name property.
/// </summary>
/// <remarks>
/// Creates a new instanceof a <see cref="DisplayAttributeNameStringExtractor"/>.
/// </remarks>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public class DisplayAttributeNameStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider)
: DisplayAttributeStringExtractor("Name", metadataProvider)
{
/// <summary>
/// Extracts localizable string from <see cref="DisplayAttribute"/> Name property.
/// </summary>
public class DisplayAttributeNameStringExtractor : DisplayAttributeStringExtractor
{
/// <summary>
/// Creates a new instanceof a <see cref="DisplayAttributeNameStringExtractor"/>.
/// </summary>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public DisplayAttributeNameStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider)
: base("Name", metadataProvider)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
using Microsoft.CodeAnalysis;
using System.ComponentModel.DataAnnotations;

namespace OrchardCoreContrib.PoExtractor.DotNet
namespace OrchardCoreContrib.PoExtractor.DotNet;

/// <summary>
/// Extracts localizable string from <see cref="DisplayAttribute"/> ShortName property.
/// </summary>
/// <remarks>
/// Creates a new instance of a <see cref="DisplayAttributeShortNameStringExtractor"/>.
/// </remarks>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public class DisplayAttributeShortNameStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider)
: DisplayAttributeStringExtractor("ShortName", metadataProvider)
{
/// <summary>
/// Extracts localizable string from <see cref="DisplayAttribute"/> ShortName property.
/// </summary>
public class DisplayAttributeShortNameStringExtractor : DisplayAttributeStringExtractor
{
/// <summary>
/// Creates a new instance of a <see cref="DisplayAttributeShortNameStringExtractor"/>.
/// </summary>
/// <param name="metadataProvider">The <see cref="IMetadataProvider{TNode}"/>.</param>
public DisplayAttributeShortNameStringExtractor(IMetadataProvider<SyntaxNode> metadataProvider)
: base("ShortName", metadataProvider)
{
}
}
}
Loading

0 comments on commit 3d44a59

Please sign in to comment.