private static string getResXName(JsonToResxConverterOptions options, string lang) { string fileName = options.OutputFile; if (lang != options.FallbackCulture) { fileName = $"{Path.GetFileNameWithoutExtension(fileName)}.{lang}{Path.GetExtension(fileName)}"; } return(Path.Combine(options.OutputFolder, fileName)); }
private static ResXResourceWriter getResX(JsonToResxConverterOptions options, Dictionary <string, ResXResourceWriter> resXfiles, string lang) { ResXResourceWriter resX; if (!resXfiles.TryGetValue(lang, out resX)) { resX = new ResXResourceWriter(getResXName(options, lang)); resXfiles.Add(lang, resX); } return(resX); }
static JsonToResxConverterOptions getOptions(string[] args) { var options = new JsonToResxConverterOptions(); for (int i = 0; i < args.Length; i++) { string key = args[i]; if (key == "-i" || key == "-input") { if (args.Length == i + 1) { CrashAndBurn(ExitCode.InputArgumentMissing, "Value for option 'input' is missing"); } options.Inputs.Add(args[i + 1]); i++; continue; } if (key == "-dir" || key == "-outputDir") { if (args.Length == i + 1) { CrashAndBurn(ExitCode.InvalidOutputArgument, "Value for option 'outputDir' is missing"); } options.OutputFolder = args[i + 1]; i++; continue; } if (key == "-file" || key == "-outputFile") { if (args.Length == i + 1) { CrashAndBurn(ExitCode.InvalidOutputArgument, "Value for option 'outputFile' is missing"); Console.WriteLine("ERROR: Value for option 'outputFile' is missing"); Environment.Exit(-2); } options.OutputFile = args[i + 1]; i++; continue; } if (key == "-fallback" || key == "-fallbackCulture") { if (args.Length == i + 1) { CrashAndBurn(ExitCode.FallbackArgumentMissing, "Value for option 'fallbackCulture' is missing"); } options.FallbackCulture = args[i + 1]; i++; continue; } if (key == "-f" || key == "-force") { options.Overwrite = OverwriteModes.Force; continue; } } return(options); }
public static ConverterLogger Convert(JsonToResxConverterOptions options) { var logger = new ConverterLogger(); Dictionary <string, ResXResourceWriter> resXfiles = new Dictionary <string, ResXResourceWriter>(); List <string> key = new List <string>(); JsonTextReader reader = new JsonTextReader(new StringReader(File.ReadAllText(options.Inputs.First()))); while (reader.Read()) { if (reader.TokenType == JsonToken.Comment) { continue; } if (reader.Value != null) { if (reader.TokenType == JsonToken.PropertyName) { key.Add(reader.Value.ToString()); var next = reader.Read(); if (reader.TokenType == JsonToken.String) { string value = reader.Value.ToString(); string keyname = $"{String.Join(options.KeySeparator, key.Skip(1))}"; Console.WriteLine($"{keyname} = {value}"); ResXResourceWriter resX = getResX(options, resXfiles, key[0]); resX.AddResource(keyname, value); key.Remove(key.Last()); continue; } if (reader.TokenType == JsonToken.StartArray) { int i = 0; while (reader.Read()) { if (reader.TokenType == JsonToken.EndArray) { break; } string value = reader.Value.ToString(); string keyname = $"{String.Join(options.KeySeparator, key.Skip(1))}{options.KeySeparator}{i}"; Console.WriteLine($"{keyname} = {value}"); ResXResourceWriter resX = getResX(options, resXfiles, key[0]); resX.AddResource(keyname, value); i++; } key.Remove(key.Last()); continue; } } } else if (key.Count > 0) { key.Remove(key.Last()); } } foreach (var resX in resXfiles.Values) { resX.Close(); } return(logger); }