Exporting to .resx files.

This commit is contained in:
Kay Unkroth 2021-03-24 12:10:17 -07:00
parent 21156167f5
commit f2c74441fb
4 changed files with 69 additions and 8 deletions

View File

@ -11,7 +11,8 @@ namespace MTCmd
{ {
Export, Export,
Import, Import,
Overwrite Overwrite,
ExportResx
} }
class Program class Program
@ -49,7 +50,8 @@ namespace MTCmd
switch (mode) switch (mode)
{ {
case Mode.Export: case Mode.Export:
Export(model, exportFolder, localeId); case Mode.ExportResx:
Export(mode, model, exportFolder, localeId);
break; break;
case Mode.Import: case Mode.Import:
Import(model, importFile, false); Import(model, importFile, false);
@ -61,7 +63,7 @@ namespace MTCmd
break; break;
} }
} }
catch(Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"{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<string> export = (path) => { if (mode == Mode.ExportResx) model.ExportToResx(path); else model.ExportToCsv(path); };
if (exportFolder != null) if (exportFolder != null)
{ {
if(!string.IsNullOrEmpty(lcid)) if (!string.IsNullOrEmpty(lcid))
{ {
model.DeselectAllLanguages(); model.DeselectAllLanguages();
model.SetLanguageFlags(lcid, true, false); model.SetLanguageFlags(lcid, true, false);
model.ExportToCsv(exportFolder.FullName); export(exportFolder.FullName);
Console.WriteLine(Strings.singleLocalExportSuccess, lcid, exportFolder); Console.WriteLine(Strings.singleLocalExportSuccess, lcid, exportFolder);
} }
else if (model.HasTargetLanguages) else if (model.HasTargetLanguages)
{ {
model.ExportToCsv(exportFolder.FullName); export(exportFolder.FullName);
Console.WriteLine(Strings.exportSuccess, exportFolder); Console.WriteLine(Strings.exportSuccess, exportFolder);
} }
else else

View File

@ -16,6 +16,7 @@ using Microsoft.VisualBasic.FileIO;
using Adomd = Microsoft.AnalysisServices.AdomdClient; using Adomd = Microsoft.AnalysisServices.AdomdClient;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Resources;
namespace Metadata_Translator namespace Metadata_Translator
{ {
@ -423,7 +424,7 @@ namespace Metadata_Translator
} }
/// <summary> /// <summary>
/// Exports the translations to individual language files. /// Exports the translations to individual language (csv) files.
/// The files are placed into the specified export folder. /// The files are placed into the specified export folder.
/// </summary> /// </summary>
/// <param name="exportFolderPath"></param> /// <param name="exportFolderPath"></param>
@ -467,6 +468,34 @@ namespace Metadata_Translator
} }
} }
/// <summary>
/// Exports the translations to individual resx files.
/// The files are placed into the specified export folder.
/// </summary>
/// <param name="exportFolderPath"></param>
public void ExportToResx(string exportFolderPath)
{
List<ExpandoObject> dataRows = GetAllDataRows();
if (dataRows != null && dataRows.Count > 0)
{
List<string> 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);
}
}
}
}
}
}
/// <summary> /// <summary>
/// Imports translations from a csv file. The file name must match the LCID of the target language. /// Imports translations from a csv file. The file name must match the LCID of the target language.
/// </summary> /// </summary>

View File

@ -11,10 +11,13 @@ namespace Metadata_Translator
{ {
public virtual NamedMetadataObject TabularObject { get; protected set; } public virtual NamedMetadataObject TabularObject { get; protected set; }
public TranslatedProperty TranslatedProperty { get; protected set; } public TranslatedProperty TranslatedProperty { get; protected set; }
public Guid TemporaryObjectId { get; protected set; }
public MetadataObjectContainer(NamedMetadataObject metadataObject, TranslatedProperty translatedProperty) public MetadataObjectContainer(NamedMetadataObject metadataObject, TranslatedProperty translatedProperty)
{ {
TabularObject = metadataObject; TabularObject = metadataObject;
TranslatedProperty = translatedProperty; TranslatedProperty = translatedProperty;
TemporaryObjectId = Guid.NewGuid();
} }
public override string ToString() public override string ToString()

View File

@ -26,5 +26,30 @@ namespace Metadata_Translator
} }
return values; return values;
} }
public static Dictionary<Guid, string> GetValues(this List<ExpandoObject> collection, string containerColumnName, string columnName)
{
if (collection == null) return new Dictionary<Guid, string>();
var values = new Dictionary<Guid, string>();
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;
}
} }
} }