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

feat(PoExtractor): add single file output #94

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public void Add(LocalizableStringOccurence item)
_values.Add(key, new LocalizableString(item));
}
}

/// <summary>
/// Clear collection
/// </summary>
public void Clear() => _values.Clear();
}
46 changes: 38 additions & 8 deletions src/OrchardCoreContrib.PoExtractor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void Main(string[] args)
return;
}

(string language, string templateEngine) = GetCliOptions(args);
(string language, string templateEngine, string singleOutputFile) = GetCliOptions(args);

if (language == null || templateEngine == null)
{
Expand Down Expand Up @@ -77,6 +77,8 @@ public static void Main(string[] args)
projectProcessors.Add(new LiquidProjectProcessor());
}

var isSingleFileOutput = !string.IsNullOrEmpty(singleOutputFile);
var localizableStrings = new LocalizableStringCollection();
foreach (var projectFile in projectFiles)
{
var projectPath = Path.GetDirectoryName(projectFile);
Expand All @@ -88,30 +90,49 @@ public static void Main(string[] args)
continue;
}

var localizableStrings = new LocalizableStringCollection();
foreach (var projectProcessor in projectProcessors)
{
projectProcessor.Process(projectPath, projectBasePath, localizableStrings);
}

if (!isSingleFileOutput)
{
if (localizableStrings.Values.Any())
{
var potPath = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(projectFile) + PoWriter.PortaleObjectTemplateExtension);

Directory.CreateDirectory(Path.GetDirectoryName(potPath));

using var potFile = new PoWriter(potPath);
potFile.WriteRecord(localizableStrings.Values);
localizableStrings.Clear();
}

Console.WriteLine($"{Path.GetFileName(projectPath)}: Found {localizableStrings.Values.Count()} strings.");
}
}

if (isSingleFileOutput)
{
if (localizableStrings.Values.Any())
{
var potPath = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(projectFile) + PoWriter.PortaleObjectTemplateExtension);
var potPath = Path.Combine(outputPath, singleOutputFile);

Directory.CreateDirectory(Path.GetDirectoryName(potPath));

using var potFile = new PoWriter(potPath);
potFile.WriteRecord(localizableStrings.Values);
}

Console.WriteLine($"{Path.GetFileName(projectPath)}: Found {localizableStrings.Values.Count()} strings.");
Console.WriteLine($"Found {localizableStrings.Values.Count()} strings.");
}
}

private static (string language, string templateEngine) GetCliOptions(string[] args)
private static (string language, string templateEngine, string singleOutputFile) GetCliOptions(string[] args)
{
var language = _defaultLanguage;
var templateEngine = _defaultTemplateEngine;
string singleOutputFile = null;
for (int i = 4; i <= args.Length; i += 2)
{
switch (args[i - 2])
Expand Down Expand Up @@ -153,7 +174,7 @@ private static (string language, string templateEngine) GetCliOptions(string[] a
if (!string.IsNullOrEmpty(args[i - 1]))
{
var ignoredProjects = args[i - 1].Split(',', StringSplitOptions.RemoveEmptyEntries);

foreach (var ignoredProject in ignoredProjects)
{
IgnoredProject.Add(ignoredProject);
Expand All @@ -169,6 +190,14 @@ private static (string language, string templateEngine) GetCliOptions(string[] a
LocalizerAccessors.LocalizerIdentifiers = localizerIdentifiers;
}

break;
case "-s":
case "--single":
if (!string.IsNullOrEmpty(args[i - 1]))
{
singleOutputFile = args[i - 1];
}

break;
default:
language = null;
Expand All @@ -177,7 +206,7 @@ private static (string language, string templateEngine) GetCliOptions(string[] a
}
}

return (language, templateEngine);
return (language, templateEngine, singleOutputFile);
}

private static void ShowHelp()
Expand All @@ -195,6 +224,7 @@ private static void ShowHelp()
Console.WriteLine(" -t, --template <Razor|Liquid> Specifies the template engine to extract the translatable strings from.");
Console.WriteLine(" Default: Razor & Liquid templates");
Console.WriteLine(" -i, --ignore project1,project2 Ignores extracting PO filed from a given project(s).");
Console.WriteLine(" --localizer localizer1,localizer2 Specifies the name of the localizer(s) that will be used suring the extraction process.");
Console.WriteLine(" --localizer localizer1,localizer2 Specifies the name of the localizer(s) that will be used during the extraction process.");
Console.WriteLine(" -s, --single <FILE_NAME> Specifies the single output file.");
}
}
Loading