-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add plugin support for other templates languages #102
Open
sarahelsaig
wants to merge
13
commits into
OrchardCoreContrib:main
Choose a base branch
from
sarahelsaig:plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1f1cac7
Command line refactoring.
sarahelsaig 9eb4f21
Add -p/--plugin command line option.
sarahelsaig a8d4f00
Add Microsoft.CodeAnalysis.CSharp.Scripting package.
sarahelsaig f3420df
Add CSX plugin support.
sarahelsaig c172b43
Some documentation.
sarahelsaig aeaa3bd
Access bug fix.
sarahelsaig 31224e0
Add sample and test.
sarahelsaig 11b7fd5
Fix spacing.
sarahelsaig e7c2a10
Fix test name.
sarahelsaig 8716a94
Fix test for Windows.
sarahelsaig bd6cfd5
Fix warning NU1507: There are 2 package sources defined in your confi…
sarahelsaig d634380
Move ProcessPluginsAsync logic into the PluginHelper class in the Abs…
sarahelsaig 132d4f4
Revert unnecessary nuget source name change.
sarahelsaig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<packageSources> | ||
<!-- Ignore global configuration --> | ||
<clear /> | ||
<add key="NuGet" value="https://api.nuget.org/v3/index.json" /> | ||
<add key="OrchardCore" value="https://nuget.cloudsmith.io/orchardcore/preview/v3/index.json" /> | ||
</packageSources> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/OrchardCoreContrib.PoExtractor.Abstractions/PluginHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Reflection; | ||
using Microsoft.CodeAnalysis.CSharp.Scripting; | ||
using Microsoft.CodeAnalysis.Scripting; | ||
|
||
namespace OrchardCoreContrib.PoExtractor; | ||
|
||
public static class PluginHelper | ||
{ | ||
public static async Task ProcessPluginsAsync( | ||
IList<string> plugins, | ||
List<IProjectProcessor> projectProcessors, | ||
List<string> projectFiles, | ||
IEnumerable<Assembly> assemblies) | ||
{ | ||
var options = ScriptOptions.Default.AddReferences(assemblies); | ||
|
||
foreach (var plugin in plugins) | ||
{ | ||
var code = await File.ReadAllTextAsync(plugin); | ||
await CSharpScript.EvaluateAsync(code, options, new PluginContext(projectProcessors, projectFiles)); | ||
} | ||
} | ||
|
||
public record PluginContext(List<IProjectProcessor> projectProcessors, List<string> projectFiles); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace OrchardCoreContrib.PoExtractor; | ||
|
||
public class GetCliOptionsResult | ||
{ | ||
public string Language { get; set; } | ||
public string TemplateEngine { get; set; } | ||
public string SingleOutputFile { get; set; } | ||
public IList<string> Plugins { get; set; } = new List<string>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
test/OrchardCoreContrib.PoExtractor.Tests/PluginTestFiles/BasicJsonLocalizationProcessor.csx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.Json.Nodes; | ||
using OrchardCoreContrib.PoExtractor; | ||
|
||
// This example plugin implements processing for a very simplistic subset of the i18next JSON format. It only supports | ||
// strings and other objects, and the files must be located in i18n/{language}.json. Even though this is only meant as a | ||
// demo, even this much can be useful in a real life scenario if paired with a backend API that generates the files for | ||
// other languages using PO files, to centralize the localization tooling. | ||
public class BasicJsonLocalizationProcessor : IProjectProcessor | ||
{ | ||
public void Process(string path, string basePath, LocalizableStringCollection strings) | ||
{ | ||
ArgumentException.ThrowIfNullOrEmpty(path); | ||
ArgumentException.ThrowIfNullOrEmpty(basePath); | ||
ArgumentNullException.ThrowIfNull(strings); | ||
|
||
var jsonFilePaths = Directory.GetFiles(path, "*.json", SearchOption.AllDirectories) | ||
.Where(path => Path.GetFileNameWithoutExtension(path).ToUpperInvariant() is "EN" or "00" or "IV") | ||
.Where(path => Path.GetFileName(Path.GetDirectoryName(path))?.ToUpperInvariant() is "I18N") | ||
.GroupBy(Path.GetDirectoryName) | ||
.Select(group => group | ||
.OrderBy(path => Path.GetFileNameWithoutExtension(path).ToUpperInvariant() switch | ||
{ | ||
"EN" => 0, | ||
"00" => 1, | ||
"IV" => 2, | ||
_ => 3, | ||
}) | ||
.ThenBy(path => path) | ||
.First()); | ||
|
||
foreach (var jsonFilePath in jsonFilePaths) | ||
{ | ||
try | ||
{ | ||
ProcessJson( | ||
jsonFilePath, | ||
strings, | ||
JObject.Parse(File.ReadAllText(jsonFilePath)), | ||
string.Empty); | ||
} | ||
catch | ||
{ | ||
Console.WriteLine("Process failed for: {0}", path); | ||
} | ||
} | ||
} | ||
|
||
private static void ProcessJson(string path, LocalizableStringCollection strings, JsonNode json, string prefix) | ||
{ | ||
if (json is JsonObject jsonObject) | ||
{ | ||
foreach (var (name, value) in jsonObject) | ||
{ | ||
var newPrefix = string.IsNullOrEmpty(prefix) ? name : $"{prefix}.{name}"; | ||
ProcessJson(path, strings, value, newPrefix); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (json is JsonValue jsonValue) | ||
{ | ||
var value = jsonValue.GetObjectValue()?.ToString(); | ||
strings.Add(new() | ||
{ | ||
Context = prefix, | ||
Location = new() { SourceFile = path }, | ||
Text = value, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
projectProcessors.Add(new BasicJsonLocalizationProcessor()); |
13 changes: 13 additions & 0 deletions
13
test/OrchardCoreContrib.PoExtractor.Tests/PluginTestFiles/i18n/en.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"about": { | ||
"title": "About us", | ||
"notes": "Title for main menu" | ||
}, | ||
"home": { | ||
"title": "Home page", | ||
"context": "Displayed on the main website page" | ||
}, | ||
"admin.login": { | ||
"title": "Administrator login" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why this is in Abstractions while it relies heavily on
CSharpScript
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where should it be then?