2021-03-08 11:44:08 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.CommandLine;
|
|
|
|
|
using System.CommandLine.Invocation;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Metadata_Translator;
|
|
|
|
|
|
|
|
|
|
namespace MTCmd
|
|
|
|
|
{
|
|
|
|
|
public enum Mode
|
|
|
|
|
{
|
|
|
|
|
Export,
|
|
|
|
|
Import,
|
2021-03-25 03:10:17 +08:00
|
|
|
|
Overwrite,
|
|
|
|
|
ExportResx
|
2021-03-08 11:44:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Program
|
|
|
|
|
{
|
|
|
|
|
static int Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
///
|
|
|
|
|
/// Create a root command with the required options.
|
|
|
|
|
///
|
|
|
|
|
var rootCommand = new RootCommand(Strings.rootCmdDescription)
|
|
|
|
|
{
|
|
|
|
|
new Option<string>(
|
|
|
|
|
new string[]{ "--connection-string", "-cs" }, Strings.csDescription){ IsRequired = true },
|
|
|
|
|
new Option<Mode>(
|
|
|
|
|
new string[]{ "--mode", "-m" },
|
|
|
|
|
getDefaultValue: () => Mode.Export,
|
|
|
|
|
description: Strings.modeDescription),
|
|
|
|
|
new Option<DirectoryInfo>(
|
|
|
|
|
new string[]{ "--export-folder", "-ef" }, Strings.efDescription).ExistingOnly(),
|
|
|
|
|
new Option<FileInfo>(
|
2021-03-13 02:09:07 +08:00
|
|
|
|
new string[]{ "--import-file", "-if" }, Strings.ifDescription).ExistingOnly(),
|
|
|
|
|
new Option<string>(
|
2021-04-07 08:20:08 +08:00
|
|
|
|
new string[]{ "--locale-id", "-lcid" }, Strings.lcidDescription),
|
|
|
|
|
new Option<bool>(
|
|
|
|
|
new string[]{ "--fallback-mode", "-fm" }, Strings.fallbackDescription),
|
|
|
|
|
new Option<string>(
|
|
|
|
|
new string[]{ "--key-prefix", "-kp" }, Strings.keyPrefixDescription)
|
|
|
|
|
};
|
2021-03-08 11:44:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Note that the parameters of the handler method are matched according to the names of the options
|
2021-04-07 08:20:08 +08:00
|
|
|
|
rootCommand.Handler = CommandHandler.Create<string, Mode, DirectoryInfo, FileInfo, string, bool, string>((connectionString, mode, exportFolder, importFile, localeId, fallbackMode, keyPrefix) =>
|
2021-03-08 11:44:08 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
DataModel model = DataModel.Connect(connectionString);
|
|
|
|
|
model.InitializeLanguages();
|
|
|
|
|
|
|
|
|
|
switch (mode)
|
|
|
|
|
{
|
|
|
|
|
case Mode.Export:
|
2021-03-25 03:10:17 +08:00
|
|
|
|
case Mode.ExportResx:
|
2021-04-07 08:20:08 +08:00
|
|
|
|
Export(mode, model, exportFolder, localeId, keyPrefix);
|
2021-03-08 11:44:08 +08:00
|
|
|
|
break;
|
|
|
|
|
case Mode.Import:
|
2021-04-07 08:20:08 +08:00
|
|
|
|
Import(model, importFile, false, fallbackMode);
|
2021-03-08 11:44:08 +08:00
|
|
|
|
break;
|
|
|
|
|
case Mode.Overwrite:
|
2021-04-07 08:20:08 +08:00
|
|
|
|
Import(model, importFile, true, fallbackMode);
|
2021-03-08 11:44:08 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-25 03:10:17 +08:00
|
|
|
|
catch (Exception ex)
|
2021-03-08 11:44:08 +08:00
|
|
|
|
{
|
2021-03-27 05:17:03 +08:00
|
|
|
|
Console.Error.WriteLine($"{ex}");
|
2021-03-08 11:44:08 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Parse the incoming args and invoke the handler
|
|
|
|
|
return rootCommand.InvokeAsync(args).Result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-07 08:20:08 +08:00
|
|
|
|
static void Import(DataModel model, FileInfo importFile, bool overwriteMode, bool fallbackDefault)
|
2021-03-08 11:44:08 +08:00
|
|
|
|
{
|
2021-03-27 05:17:03 +08:00
|
|
|
|
Func<string, bool> import = (lcid) => {
|
|
|
|
|
if (Path.GetExtension(importFile.Name).Equals(".csv", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
2021-04-07 08:20:08 +08:00
|
|
|
|
model.ImportFromCsv(importFile.FullName, lcid, overwriteMode, fallbackDefault);
|
2021-03-27 05:17:03 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (Path.GetExtension(importFile.Name).Equals(".resx", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
string referenceResx = $"{importFile.DirectoryName}\\{model.DefaultCulture}.resx";
|
|
|
|
|
if(File.Exists(referenceResx))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-04-07 08:20:08 +08:00
|
|
|
|
model.ImportFromResx(importFile.FullName, referenceResx, lcid, overwriteMode, fallbackDefault);
|
2021-03-27 05:17:03 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (NoResxMatchesException noMatch)
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine(string.Format(Strings.NoResxMatches, noMatch.TranslationResx, noMatch.ReferenceResx));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine(string.Format(Strings.noResxReferenceFile, importFile.FullName, referenceResx));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine(Strings.invalidFileType);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-08 11:44:08 +08:00
|
|
|
|
if (importFile != null)
|
|
|
|
|
{
|
|
|
|
|
string lcid = Path.GetFileNameWithoutExtension(importFile.Name);
|
|
|
|
|
if (model.SetLanguageFlags(lcid, true))
|
|
|
|
|
{
|
2021-03-27 05:17:03 +08:00
|
|
|
|
if (import(lcid))
|
|
|
|
|
{
|
|
|
|
|
model.Update();
|
|
|
|
|
Console.WriteLine(Strings.importSuccess, importFile);
|
|
|
|
|
}
|
2021-03-08 11:44:08 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-27 05:17:03 +08:00
|
|
|
|
Console.Error.WriteLine(Strings.invalidLocale);
|
2021-03-08 11:44:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-27 05:17:03 +08:00
|
|
|
|
Console.Error.WriteLine(Strings.noImportFileSpecified);
|
2021-03-08 11:44:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-07 08:20:08 +08:00
|
|
|
|
static void Export(Mode mode, DataModel model, DirectoryInfo exportFolder, string lcid, string keyPrefix)
|
2021-03-08 11:44:08 +08:00
|
|
|
|
{
|
2021-04-07 08:20:08 +08:00
|
|
|
|
Action export = () => { if (mode == Mode.ExportResx) model.ExportToResx(exportFolder.FullName, keyPrefix); else model.ExportToCsv(exportFolder.FullName); };
|
2021-03-25 03:10:17 +08:00
|
|
|
|
|
2021-03-08 11:44:08 +08:00
|
|
|
|
if (exportFolder != null)
|
|
|
|
|
{
|
2021-03-25 03:10:17 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(lcid))
|
2021-03-13 02:09:07 +08:00
|
|
|
|
{
|
|
|
|
|
model.DeselectAllLanguages();
|
|
|
|
|
model.SetLanguageFlags(lcid, true, false);
|
|
|
|
|
|
2021-03-27 05:17:03 +08:00
|
|
|
|
export();
|
2021-03-13 02:09:07 +08:00
|
|
|
|
Console.WriteLine(Strings.singleLocalExportSuccess, lcid, exportFolder);
|
|
|
|
|
}
|
2021-03-27 05:17:03 +08:00
|
|
|
|
else if (model.HasTargetLanguages || mode == Mode.ExportResx)
|
2021-03-08 11:44:08 +08:00
|
|
|
|
{
|
2021-03-27 05:17:03 +08:00
|
|
|
|
export();
|
2021-03-08 11:44:08 +08:00
|
|
|
|
Console.WriteLine(Strings.exportSuccess, exportFolder);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-27 05:17:03 +08:00
|
|
|
|
Console.Error.WriteLine(Strings.noExportableTranslations);
|
2021-03-08 11:44:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-27 05:17:03 +08:00
|
|
|
|
Console.Error.WriteLine(Strings.noExportFolderSpecified);
|
2021-03-08 11:44:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|