private static JsonResources generateJsonResources(IResxToJsonFormatter formatter, ResourceBundle bundle, ResxToJsonConverterOptions options) { var result = new JsonResources(); // root resoruce IDictionary <string, string> baseValues = bundle.GetValues(null); JObject jBaseValues = convertValues(baseValues, options); result.BaseResources = formatter.GetJsonResource(jBaseValues, CultureInfo.InvariantCulture, bundle, options); // culture specific resources foreach (CultureInfo culture in bundle.Cultures) { if (culture.Equals(CultureInfo.InvariantCulture)) { continue; } IDictionary <string, string> values = bundle.GetValues(culture); if (options.UseFallbackForMissingTranslation) { foreach (var baseValue in baseValues) { if (!values.ContainsKey(baseValue.Key)) { values[baseValue.Key] = baseValues[baseValue.Key]; } } } JObject jCultureValues = convertValues(values, options); result.LocalizedResources[culture] = formatter.GetJsonResource(jCultureValues, culture, bundle, options); } return(result); }
public override bool CheckOptions(ResxToJsonConverterOptions options, out string message) { if (string.IsNullOrEmpty(options.FallbackCulture)) { message = "The parameter fallbackCulture is not specified."; return(false); } message = string.Empty; return(true); }
private static JObject convertValues(IDictionary <string, string> values, ResxToJsonConverterOptions options) { JObject json = new JObject(); foreach (KeyValuePair <string, string> pair in values) { if (pair.Key.Contains(options.KeySeparator)) { JObject curJObject = json; // hierarchy structure string[] fieldNames = pair.Key.Split(options.KeySeparator[0]); for (int i = 0; i < fieldNames.Length; i++) { string fieldName = fieldNames[i]; string jKey = adjustCasing(fieldName, options); if (i == fieldNames.Length - 1) { curJObject[jKey] = pair.Value; } else { var child = curJObject.Property(jKey); if (child == null) { JObject childObject = new JObject(); JProperty childProperty = new JProperty(jKey, childObject); curJObject.Add(childProperty); curJObject = childObject; } else { JToken value = child.Value; if (value.Type == JTokenType.Object) { curJObject = (JObject)value; } } } } } else { // flat json[adjustCasing(pair.Key, options)] = pair.Value; } } var s = json.ToString(); return(json); }
static void checkOptions(ResxToJsonConverterOptions options) { if (options.Inputs.Count > 0) { foreach (string input in options.Inputs) { string path = input; if (!Path.IsPathRooted(input)) { path = Path.Combine(Environment.CurrentDirectory, input); } if (Directory.Exists(path)) { if (!string.IsNullOrEmpty(options.FilePattern)) { try { var files = Directory.EnumerateFiles(path, options.FilePattern); foreach (string file in files) { options.InputFiles.Add(file); } } catch (Exception exception) { CrashAndBurn(ExitCode.InvalidInputPath, "'{0}' is not a valid pattern.", options.FilePattern); } } else { options.InputFolders.Add(path); } } else if (File.Exists(path)) { options.InputFiles.Add(path); } else { CrashAndBurn(ExitCode.InvalidInputPath, "input path '{0}' doesn't relate to a file or a directory", path); } } } else { options.InputFolders.Add(Environment.CurrentDirectory); } if (String.IsNullOrEmpty(options.OutputFolder) && String.IsNullOrEmpty(options.OutputFile)) { options.OutputFolder = Environment.CurrentDirectory; } }
static string stringifyJson(JObject json, ResxToJsonConverterOptions options) { string text = json.ToString(Formatting.Indented); switch (options.OutputFormat) { case OutputFormat.RequireJs: return("define(" + text + ");"); default: return(text); } }
private static string adjustCasing(string key, ResxToJsonConverterOptions options) { switch (options.Casing) { case JsonCasing.Camel: char[] chars = key.ToCharArray(); chars[0] = Char.ToLower(chars[0]); return(new string(chars)); case JsonCasing.Lower: return(key.ToLowerInvariant()); } return(key); }
private static string getOutputPath(ResxToJsonConverterOptions options, string baseDir, string baseFileName, string lang) { if (false) { string dirPath = options.OutputFormat == OutputFormat.i18next ? Path.Combine(baseDir, lang) : baseDir; return(Path.Combine(dirPath, baseFileName)); } else { return(Path.Combine(baseDir, $"{Path.GetFileNameWithoutExtension(baseFileName)}.{lang}{Path.GetExtension(baseFileName)}")); } }
private static JsonResources generateJsonResources(ResourceBundle bundle, ResxToJsonConverterOptions options) { var result = new JsonResources(); // root resoruce IDictionary <string, string> baseValues = bundle.GetValues(null); JObject jBaseValues = convertValues(baseValues, options); switch (options.OutputFormat) { case OutputFormat.RequireJs: // When dealing with require.js i18n the root resource contains a "root" subnode that contains all // of the base translations and then a bunch of nodes like the following for each supported culture: // "en-US" : true // "fr" : true // ... var jRoot = new JObject(); jRoot["root"] = jBaseValues; foreach (CultureInfo culture in bundle.Cultures) { if (culture.Equals(CultureInfo.InvariantCulture)) { continue; } jRoot[culture.Name] = true; } result.BaseResources = jRoot; break; default: // In the simplest case our output format is plain vanilla json (just a kvp dictionary) result.BaseResources = jBaseValues; break; } // culture specific resources foreach (CultureInfo culture in bundle.Cultures) { if (culture.Equals(CultureInfo.InvariantCulture)) { continue; } IDictionary <string, string> values = bundle.GetValues(culture); JObject jCultureValues = convertValues(values, options); result.LocalizedResources[culture.Name] = jCultureValues; } return(result); }
private static void writeOutput(string outputPath, string jsonText, ResxToJsonConverterOptions options, ConverterLogger logger) { Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); if (File.Exists(outputPath)) { var attrs = File.GetAttributes(outputPath); if ((attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { if (options.Overwrite == OverwriteModes.Skip) { logger.AddMsg(Severity.Error, "Cannot overwrite {0} file, skipping", outputPath); return; } // remove read-only attribute attrs = ~FileAttributes.ReadOnly & attrs; File.SetAttributes(outputPath, attrs); } // if existing file isn't readonly we just overwrite it } File.WriteAllText(outputPath, jsonText, Encoding.UTF8); logger.AddMsg(Severity.Info, "Created {0} file", outputPath); }
private static JObject convertValues(IDictionary <string, string> values, ResxToJsonConverterOptions options) { var json = new JObject(); foreach (KeyValuePair <string, string> pair in values) { string fieldName = pair.Key; switch (options.Casing) { case JsonCasing.Camel: char[] chars = fieldName.ToCharArray(); chars[0] = Char.ToLower(chars[0]); fieldName = new string(chars); break; case JsonCasing.Lower: fieldName = fieldName.ToLowerInvariant(); break; } json[fieldName] = pair.Value; } return(json); }
static void checkOptions(ResxToJsonConverterOptions options) { if (options.Inputs.Count > 0) { foreach (string input in options.Inputs) { string path = input; if (!Path.IsPathRooted(input)) { path = Path.Combine(Environment.CurrentDirectory, input); } if (Directory.Exists(path)) { options.InputFolders.Add(path); } else if (File.Exists(path)) { options.InputFiles.Add(path); } else { CrashAndBurn(ExitCode.InvalidInputPath, "input path '{0}' doesn't relate to a file or a directory", path); } } } else { options.InputFolders.Add(Environment.CurrentDirectory); } if (String.IsNullOrEmpty(options.OutputFolder) && String.IsNullOrEmpty(options.OutputFile)) { options.OutputFolder = Environment.CurrentDirectory; } }
public override string GetFileContent(JObject json, ResxToJsonConverterOptions options) { string jsonSerialized = SerializeJsonObject(json); string content = $@"""use strict""; ! function(root, factory) {{ if (""function"" === typeof define && define.amd) {{ define(function(require) {{ factory(require(""devextreme/localization"")); }}) }} else {{ if (""object"" === typeof module && module.exports) {{ factory(require(""devextreme/localization"")); }} else {{ factory(DevExpress.localization); }} }} }} (this, function(localization) {{ localization.loadMessages({jsonSerialized}); }}); "; return(content); }
public virtual JObject GetJsonResource(JObject jBaseValues, CultureInfo cultureInfo, ResourceBundle bundle, ResxToJsonConverterOptions options) { return(jBaseValues); }
public virtual string GetLanguageFileName(string baseFileName, CultureInfo cultureInfo, ResxToJsonConverterOptions options) { return(baseFileName); }
public virtual string GetBaseOutputDirectory(string baseOutputDirectory, CultureInfo cultureInfo, ResxToJsonConverterOptions options) { if (Equals(cultureInfo, CultureInfo.InvariantCulture)) { return(baseOutputDirectory); } return(Path.Combine(baseOutputDirectory, cultureInfo.Name)); }
private static JsonResources generateJsonResources(ResourceBundle bundle, ResxToJsonConverterOptions options) { var result = new JsonResources(); // root resoruce IDictionary<string, string> baseValues = bundle.GetValues(null); JObject jBaseValues = convertValues(baseValues, options); switch (options.OutputFormat) { case OutputFormat.RequireJs: // When dealing with require.js i18n the root resource contains a "root" subnode that contains all // of the base translations and then a bunch of nodes like the following for each supported culture: // "en-US" : true // "fr" : true // ... var jRoot = new JObject(); jRoot["root"] = jBaseValues; foreach (CultureInfo culture in bundle.Cultures) { if (culture.Equals(CultureInfo.InvariantCulture)) continue; jRoot[culture.Name] = true; } result.BaseResources = jRoot; break; default: // In the simplest case our output format is plain vanilla json (just a kvp dictionary) result.BaseResources = jBaseValues; break; } // culture specific resources foreach (CultureInfo culture in bundle.Cultures) { if (culture.Equals(CultureInfo.InvariantCulture)) continue; IDictionary<string, string> values = bundle.GetValues(culture); JObject jCultureValues = convertValues(values, options); result.LocalizedResources[culture.Name] = jCultureValues; } return result; }
public override string GetBaseOutputDirectory(string baseOutputDirectory, CultureInfo cultureInfo, ResxToJsonConverterOptions options) { if (Equals(cultureInfo, CultureInfo.InvariantCulture)) { return(Path.Combine(baseOutputDirectory, options.FallbackCulture)); } return(base.GetBaseOutputDirectory(baseOutputDirectory, cultureInfo, options)); }
public override JObject GetJsonResource(JObject jBaseValues, CultureInfo cultureInfo, ResourceBundle bundle, ResxToJsonConverterOptions options) { if (!Equals(cultureInfo, CultureInfo.InvariantCulture)) { return(jBaseValues); } // When dealing with require.js i18n the root resource contains a "root" subnode that contains all // of the base translations and then a bunch of nodes like the following for each supported culture: // "en-US" : true // "fr" : true // ... var jRoot = new JObject(); jRoot["root"] = jBaseValues; foreach (CultureInfo bundleCulture in bundle.Cultures) { if (bundleCulture.Equals(CultureInfo.InvariantCulture)) { continue; } jRoot[cultureInfo.Name] = true; } return(jRoot); }
public override string GetBaseOutputDirectory(string baseOutputDirectory, CultureInfo cultureInfo, ResxToJsonConverterOptions options) { return(baseOutputDirectory); }
public static ConverterLogger Convert(ResxToJsonConverterOptions options) { var logger = new ConverterLogger(); IDictionary <string, ResourceBundle> bundles = null; if (options.InputFiles.Count > 0) { bundles = ResxHelper.GetResources(options.InputFiles, logger); } if (options.InputFolders.Count > 0) { var bundles2 = ResxHelper.GetResources(options.InputFolders, options.Recursive, logger); if (bundles == null) { bundles = bundles2; } else { // join two bundles collection foreach (var pair in bundles2) { bundles[pair.Key] = pair.Value; } } } if (bundles == null || bundles.Count == 0) { logger.AddMsg(Severity.Warning, "No resx files were found"); return(logger); } logger.AddMsg(Severity.Trace, "Found {0} resx bundles", bundles.Count); if (bundles.Count > 1 && !String.IsNullOrEmpty(options.OutputFile)) { // join multiple resx resources into a single js-bundle var bundleMerge = new ResourceBundle(Path.GetFileNameWithoutExtension(options.OutputFile)); foreach (var pair in bundles) { bundleMerge.MergeWith(pair.Value); } logger.AddMsg(Severity.Trace, "As 'outputFile' option was specified all bundles were merged into single bundle '{0}'", bundleMerge.BaseName); bundles = new Dictionary <string, ResourceBundle> { { bundleMerge.BaseName, bundleMerge } }; } foreach (ResourceBundle bundle in bundles.Values) { JsonResources jsonResources = generateJsonResources(bundle, options); string baseFileName; string baseDir; if (!string.IsNullOrEmpty(options.OutputFile)) { baseFileName = Path.GetFileName(options.OutputFile); baseDir = Path.GetDirectoryName(options.OutputFile); } else { baseFileName = bundle.BaseName.ToLowerInvariant() + GetOutputFileExtension(options.OutputFormat); baseDir = options.OutputFolder; } if (string.IsNullOrEmpty(baseDir)) { baseDir = Environment.CurrentDirectory; } logger.AddMsg(Severity.Trace, "Processing '{0}' bundle (contains {1} resx files)", bundle.BaseName, bundle.Cultures.Count); string dirPath = options.OutputFormat == OutputFormat.i18next ? Path.Combine(baseDir, options.FallbackCulture) : baseDir; string outputPath = Path.Combine(dirPath, baseFileName); string jsonText = stringifyJson(jsonResources.BaseResources, options); writeOutput(outputPath, jsonText, options, logger); if (jsonResources.LocalizedResources.Count > 0) { foreach (KeyValuePair <string, JObject> pair in jsonResources.LocalizedResources) { dirPath = Path.Combine(baseDir, pair.Key); outputPath = Path.Combine(dirPath, baseFileName); jsonText = stringifyJson(pair.Value, options); writeOutput(outputPath, jsonText, options, logger); } } } return(logger); }
private static JObject convertValues(IDictionary<string, string> values, ResxToJsonConverterOptions options) { var json = new JObject(); foreach (KeyValuePair<string, string> pair in values) { string fieldName = pair.Key; switch (options.Casing) { case JsonCasing.Camel: char[] chars = fieldName.ToCharArray(); chars[0] = Char.ToLower(chars[0]); fieldName = new string(chars); break; case JsonCasing.Lower: fieldName = fieldName.ToLowerInvariant(); break; } json[fieldName] = pair.Value; } return json; }
static string stringifyJson(JObject json, ResxToJsonConverterOptions options) { string text = json.ToString(Formatting.Indented); switch (options.OutputFormat) { case OutputFormat.RequireJs: return "define(" + text + ");"; default: return text; } }
public static ConverterLogger Convert(ResxToJsonConverterOptions options) { var logger = new ConverterLogger(); IDictionary<string, ResourceBundle> bundles = null; if (options.InputFiles.Count > 0) { bundles = ResxHelper.GetResources(options.InputFiles, logger); } if (options.InputFolders.Count > 0) { var bundles2 = ResxHelper.GetResources(options.InputFolders, options.Recursive, logger); if (bundles == null ) { bundles = bundles2; } else { // join two bundles collection foreach (var pair in bundles2) { bundles[pair.Key] = pair.Value; } } } if (bundles == null || bundles.Count == 0) { logger.AddMsg(Severity.Warning, "No resx files were found"); return logger; } logger.AddMsg(Severity.Trace, "Found {0} resx bundles", bundles.Count); if (bundles.Count > 1 && !String.IsNullOrEmpty(options.OutputFile)) { // join multiple resx resources into a single js-bundle var bundleMerge = new ResourceBundle(Path.GetFileNameWithoutExtension(options.OutputFile)); foreach (var pair in bundles) { bundleMerge.MergeWith(pair.Value); } logger.AddMsg(Severity.Trace, "As 'outputFile' option was specified all bundles were merged into single bundle '{0}'", bundleMerge.BaseName); bundles = new Dictionary<string, ResourceBundle> {{bundleMerge.BaseName, bundleMerge}}; } foreach (ResourceBundle bundle in bundles.Values) { JsonResources jsonResources = generateJsonResources(bundle, options); string baseFileName; string baseDir; if (!string.IsNullOrEmpty(options.OutputFile)) { baseFileName = Path.GetFileName(options.OutputFile); baseDir = Path.GetDirectoryName(options.OutputFile); } else { baseFileName = bundle.BaseName.ToLowerInvariant() + GetOutputFileExtension(options.OutputFormat); baseDir = options.OutputFolder; } if (string.IsNullOrEmpty(baseDir)) { baseDir = Environment.CurrentDirectory; } logger.AddMsg(Severity.Trace, "Processing '{0}' bundle (contains {1} resx files)", bundle.BaseName, bundle.Cultures.Count); string dirPath = options.OutputFormat == OutputFormat.i18next ? Path.Combine(baseDir, options.FallbackCulture) : baseDir; string outputPath = Path.Combine(dirPath, baseFileName); string jsonText = stringifyJson(jsonResources.BaseResources, options); writeOutput(outputPath, jsonText, options, logger); if (jsonResources.LocalizedResources.Count > 0) { foreach (KeyValuePair<string, JObject> pair in jsonResources.LocalizedResources) { dirPath = Path.Combine(baseDir, pair.Key); outputPath = Path.Combine(dirPath, baseFileName); jsonText = stringifyJson(pair.Value, options); writeOutput(outputPath, jsonText, options, logger); } } } return logger; }
static string stringifyJson(IResxToJsonFormatter formatter, JObject json, ResxToJsonConverterOptions options) { return(formatter.GetFileContent(json, options)); }
public virtual string GetFileContent(JObject json, ResxToJsonConverterOptions options) { return(json.ToString(Formatting.Indented)); }
public virtual bool CheckOptions(ResxToJsonConverterOptions options, out string message) { message = string.Empty; return(true); }
public override string GetLanguageFileName(string baseFileName, CultureInfo cultureInfo, ResxToJsonConverterOptions options) { string cultureName = cultureInfo.Name; if (Equals(cultureInfo, CultureInfo.InvariantCulture)) { cultureName = options.FallbackCulture; } string extension = Path.GetExtension(baseFileName); baseFileName = Path.ChangeExtension(baseFileName, cultureName + extension); return(baseFileName); }
public override string GetFileContent(JObject json, ResxToJsonConverterOptions options) { return("define(" + base.GetFileContent(json, options) + ");"); }
public override JObject GetJsonResource(JObject jBaseValues, CultureInfo cultureInfo, ResourceBundle bundle, ResxToJsonConverterOptions options) { string cultureName = cultureInfo.Name; if (Equals(cultureInfo, CultureInfo.InvariantCulture)) { cultureName = options.FallbackCulture; } var jRoot = new JObject(); jRoot[cultureName] = jBaseValues; return(jRoot); }
static ResxToJsonConverterOptions getOptions(string[] args) { var options = new ResxToJsonConverterOptions(); 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 == "-format" || key == "-outputFormat") { if (args.Length == i + 1) { CrashAndBurn(ExitCode.OutputFormatArgumentMissing, "Value for option 'outputFormat' is missing"); } OutputFormat format; if (Enum.TryParse(args[i + 1], true, out format)) { options.OutputFormat = format; } 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 == "-c" || key == "-case") { if (args.Length == i + 1) { CrashAndBurn(ExitCode.CaseArgumentMissing, "Value for option 'case' is missing"); } JsonCasing casing; if (Enum.TryParse(args[i + 1], true, out casing)) { options.Casing = casing; } i++; continue; } if (key == "-f" || key == "-force") { options.Overwrite = OverwriteModes.Force; continue; } if (key == "-r" || key == "-recursively") { options.Recursive = true; i++; continue; } if (key == "-outputFileFormat") { if (args.Length == i + 1) { CrashAndBurn(ExitCode.CaseArgumentMissing, "Value for option 'outputFileFormat' is missing"); } options.OutputFileFormat = args[i + 1]; i++; continue; } } return(options); }
static ResxToJsonConverterOptions getOptions(string[] args) { var options = new ResxToJsonConverterOptions(); 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 == "-format" || key == "-outputFormat") { if (args.Length == i + 1) { CrashAndBurn(ExitCode.OutputFormatArgumentMissing, "Value for option 'outputFormat' is missing"); } OutputFormat format; if (Enum.TryParse(args[i + 1], true, out format)) { options.OutputFormat = format; } 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 == "-c" || key == "-case") { if (args.Length == i + 1) { CrashAndBurn(ExitCode.CaseArgumentMissing, "Value for option 'case' is missing"); } JsonCasing casing; if (Enum.TryParse(args[i + 1], true, out casing)) { options.Casing = casing; } i++; continue; } if (key == "-f" || key == "-force") { options.Overwrite = OverwriteModes.Force; continue; } if (key == "-r" || key == "-recursively") { options.Recursive = true; continue; } } return options; }