From f2c74441fbd19bb2abea9c341b5f40d5e32720bb Mon Sep 17 00:00:00 2001 From: Kay Unkroth Date: Wed, 24 Mar 2021 12:10:17 -0700 Subject: [PATCH] Exporting to .resx files. --- MetadataTranslator/MTCmd/Program.cs | 18 ++++++----- .../Metadata Translator/Data/DataModel.cs | 31 ++++++++++++++++++- .../Data/MetadataObjectContainer.cs | 3 ++ .../Helpers/ListExtensions.cs | 25 +++++++++++++++ 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/MetadataTranslator/MTCmd/Program.cs b/MetadataTranslator/MTCmd/Program.cs index e0d049b..146381e 100644 --- a/MetadataTranslator/MTCmd/Program.cs +++ b/MetadataTranslator/MTCmd/Program.cs @@ -11,7 +11,8 @@ namespace MTCmd { Export, Import, - Overwrite + Overwrite, + ExportResx } class Program @@ -49,7 +50,8 @@ namespace MTCmd switch (mode) { case Mode.Export: - Export(model, exportFolder, localeId); + case Mode.ExportResx: + Export(mode, model, exportFolder, localeId); break; case Mode.Import: Import(model, importFile, false); @@ -61,7 +63,7 @@ namespace MTCmd break; } } - catch(Exception ex) + catch (Exception ex) { Console.WriteLine($"{ex}"); } @@ -93,21 +95,23 @@ namespace MTCmd } } - static void Export(DataModel model, DirectoryInfo exportFolder, string lcid) + static void Export(Mode mode, DataModel model, DirectoryInfo exportFolder, string lcid) { + Action export = (path) => { if (mode == Mode.ExportResx) model.ExportToResx(path); else model.ExportToCsv(path); }; + if (exportFolder != null) { - if(!string.IsNullOrEmpty(lcid)) + if (!string.IsNullOrEmpty(lcid)) { model.DeselectAllLanguages(); model.SetLanguageFlags(lcid, true, false); - model.ExportToCsv(exportFolder.FullName); + export(exportFolder.FullName); Console.WriteLine(Strings.singleLocalExportSuccess, lcid, exportFolder); } else if (model.HasTargetLanguages) { - model.ExportToCsv(exportFolder.FullName); + export(exportFolder.FullName); Console.WriteLine(Strings.exportSuccess, exportFolder); } else diff --git a/MetadataTranslator/Metadata Translator/Data/DataModel.cs b/MetadataTranslator/Metadata Translator/Data/DataModel.cs index b14a270..955f728 100644 --- a/MetadataTranslator/Metadata Translator/Data/DataModel.cs +++ b/MetadataTranslator/Metadata Translator/Data/DataModel.cs @@ -16,6 +16,7 @@ using Microsoft.VisualBasic.FileIO; using Adomd = Microsoft.AnalysisServices.AdomdClient; using System.Text.RegularExpressions; using System.Security.Cryptography; +using System.Resources; namespace Metadata_Translator { @@ -423,7 +424,7 @@ namespace Metadata_Translator } /// - /// Exports the translations to individual language files. + /// Exports the translations to individual language (csv) files. /// The files are placed into the specified export folder. /// /// @@ -467,6 +468,34 @@ namespace Metadata_Translator } } + /// + /// Exports the translations to individual resx files. + /// The files are placed into the specified export folder. + /// + /// + public void ExportToResx(string exportFolderPath) + { + List dataRows = GetAllDataRows(); + if (dataRows != null && dataRows.Count > 0) + { + List languages = SelectedLanguages.Select(l => l.LanguageTag).ToList(); + + if (languages != null && languages.Count > 0) + { + foreach (string lcid in languages) + { + using (ResXResourceWriter resx = new ResXResourceWriter(System.IO.Path.Combine(exportFolderPath, $"{lcid}.resx"))) + { + foreach(var kvp in dataRows.GetValues(ContainerColumnHeader, lcid)) + { + resx.AddResource(kvp.Key.ToString(), kvp.Value); + } + } + } + } + } + } + /// /// Imports translations from a csv file. The file name must match the LCID of the target language. /// diff --git a/MetadataTranslator/Metadata Translator/Data/MetadataObjectContainer.cs b/MetadataTranslator/Metadata Translator/Data/MetadataObjectContainer.cs index 316029d..6ed2eb3 100644 --- a/MetadataTranslator/Metadata Translator/Data/MetadataObjectContainer.cs +++ b/MetadataTranslator/Metadata Translator/Data/MetadataObjectContainer.cs @@ -11,10 +11,13 @@ namespace Metadata_Translator { public virtual NamedMetadataObject TabularObject { get; protected set; } public TranslatedProperty TranslatedProperty { get; protected set; } + + public Guid TemporaryObjectId { get; protected set; } public MetadataObjectContainer(NamedMetadataObject metadataObject, TranslatedProperty translatedProperty) { TabularObject = metadataObject; TranslatedProperty = translatedProperty; + TemporaryObjectId = Guid.NewGuid(); } public override string ToString() diff --git a/MetadataTranslator/Metadata Translator/Helpers/ListExtensions.cs b/MetadataTranslator/Metadata Translator/Helpers/ListExtensions.cs index 7190aeb..0f7e22b 100644 --- a/MetadataTranslator/Metadata Translator/Helpers/ListExtensions.cs +++ b/MetadataTranslator/Metadata Translator/Helpers/ListExtensions.cs @@ -26,5 +26,30 @@ namespace Metadata_Translator } return values; } + + public static Dictionary GetValues(this List collection, string containerColumnName, string columnName) + { + if (collection == null) return new Dictionary(); + + var values = new Dictionary(); + foreach (ExpandoObject row in collection) + { + var metaContainer = (MetadataObjectContainer)row.GetObject(containerColumnName); + var columnValue = row.GetValue(columnName); + + if (metaContainer != null && !string.IsNullOrEmpty(columnValue)) + { + try + { + values.Add(metaContainer.TemporaryObjectId, columnValue); + } + catch (Exception ex) + { + throw new Exception(ex.Message + " --- " + metaContainer.TemporaryObjectId.ToString() + metaContainer.ToString()); + } + } + } + return values; + } } }