JD2022-TU1/main/tools/apps/LocalisationExporter/Options/ExportVerbOption.cs

111 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using CommandLine;
using Newtonsoft.Json.Linq;
namespace LocalisationExporter
{
/// <summary>
/// Exports a Json localisation file into UAF localisation file (.loc8)
/// </summary>
public class ExportVerbOption
{
[Option("input", HelpText = "Input file (.json).", Required = true)]
public string Input { get; set; }
[Option("output", HelpText = "Output file (.loc8)", Required = false)]
public string Output { get; set; }
public int Execute()
{
if (string.IsNullOrEmpty(Input) || !File.Exists(Input) || !Input.EndsWith(".json"))
{
Console.Out.WriteLine("ERROR: Please specify a valid json input file path.");
return -1;
}
if (string.IsNullOrEmpty(Output) || !Output.EndsWith(".loc8"))
{
Console.Out.WriteLine("ERROR: Please specify a valid output .loc8 file path.");
return -1;
}
var engine = new engineWrapper("");
engine.initFilemanager();
JObject jsonRoot;
try
{
jsonRoot = JObject.Parse(File.ReadAllText(Input));
}
catch (Exception e)
{
Console.Out.WriteLine($"ERROR: Unable to read in put file {e.Message}.");
return -1;
}
var languagesJson = jsonRoot["languages"] as JArray;
if (languagesJson == null)
{
Console.Out.WriteLine("ERROR: The input file is missing a 'languages' section.");
return -1;
}
var linesJson = jsonRoot["lines"] as JObject;
if (linesJson == null)
{
Console.Out.WriteLine("ERROR: The input file is missing a 'lines' section.");
return -1;
}
var localisationWrapper = new LocalisationWrapper();
var uafLanguages = new List<int>();
foreach (var languageJson in languagesJson)
{
var uafLanguage = GetUafLanguage(languageJson["Name"].ToString(), localisationWrapper);
uafLanguages.Add(uafLanguage);
var regions = uint.Parse(languageJson["Regions"].ToString());
localisationWrapper.setLanguageRegions(uafLanguage, regions);
}
foreach (var pair in linesJson)
{
var lineId = int.Parse(pair.Key);
var translations = (JArray) pair.Value;
if (translations.Count != uafLanguages.Count)
{
Console.Out.WriteLine($"WARNING: translation count for line {lineId} doesn't match the number of languages.");
continue;
}
int index = 0;
foreach (var translation in translations)
{
localisationWrapper.addTextData(uafLanguages[index], lineId, translation.ToString());
index++;
}
}
localisationWrapper.save(Output);
return 0;
}
private int GetUafLanguage(string languageName, LocalisationWrapper localisationWrapper)
{
var uafLanguageCount = localisationWrapper.getLanguageCount();
for (int i = 0; i < uafLanguageCount; i++)
{
var uafLanguageName = localisationWrapper.getLanguageToStr(i);
if (uafLanguageName == languageName)
{
return i;
}
}
Console.Out.WriteLine($"ERROR: Unable to find language {languageName}.");
return 0;
}
}
}