From ecef6133fb13053bf3f1d6b34ecc91ab11728333 Mon Sep 17 00:00:00 2001 From: Kay Unkroth Date: Fri, 12 Mar 2021 10:09:07 -0800 Subject: [PATCH] Added option to generate empty translation files on the command line. --- MetadataTranslator/MTCmd/Program.cs | 20 ++++++++++++----- MetadataTranslator/MTCmd/Strings.Designer.cs | 18 +++++++++++++++ MetadataTranslator/MTCmd/Strings.resx | 6 +++++ .../Metadata Translator/Data/DataModel.cs | 22 +++++++++++++++++++ .../Documentation/README.md | 10 +++++++++ 5 files changed, 71 insertions(+), 5 deletions(-) diff --git a/MetadataTranslator/MTCmd/Program.cs b/MetadataTranslator/MTCmd/Program.cs index ecf69b2..e0d049b 100644 --- a/MetadataTranslator/MTCmd/Program.cs +++ b/MetadataTranslator/MTCmd/Program.cs @@ -32,12 +32,14 @@ namespace MTCmd new Option( new string[]{ "--export-folder", "-ef" }, Strings.efDescription).ExistingOnly(), new Option( - new string[]{ "--import-file", "-if" }, Strings.ifDescription).ExistingOnly() + new string[]{ "--import-file", "-if" }, Strings.ifDescription).ExistingOnly(), + new Option( + new string[]{ "--locale-id", "-lcid" }, Strings.lcidDescription) }; // Note that the parameters of the handler method are matched according to the names of the options - rootCommand.Handler = CommandHandler.Create((connectionString, mode, exportFolder, importFile) => + rootCommand.Handler = CommandHandler.Create((connectionString, mode, exportFolder, importFile, localeId) => { try { @@ -47,7 +49,7 @@ namespace MTCmd switch (mode) { case Mode.Export: - Export(model, exportFolder); + Export(model, exportFolder, localeId); break; case Mode.Import: Import(model, importFile, false); @@ -91,11 +93,19 @@ namespace MTCmd } } - static void Export(DataModel model, DirectoryInfo exportFolder) + static void Export(DataModel model, DirectoryInfo exportFolder, string lcid) { if (exportFolder != null) { - if (model.HasTargetLanguages) + if(!string.IsNullOrEmpty(lcid)) + { + model.DeselectAllLanguages(); + model.SetLanguageFlags(lcid, true, false); + + model.ExportToCsv(exportFolder.FullName); + Console.WriteLine(Strings.singleLocalExportSuccess, lcid, exportFolder); + } + else if (model.HasTargetLanguages) { model.ExportToCsv(exportFolder.FullName); Console.WriteLine(Strings.exportSuccess, exportFolder); diff --git a/MetadataTranslator/MTCmd/Strings.Designer.cs b/MetadataTranslator/MTCmd/Strings.Designer.cs index 145cb2a..a859355 100644 --- a/MetadataTranslator/MTCmd/Strings.Designer.cs +++ b/MetadataTranslator/MTCmd/Strings.Designer.cs @@ -114,6 +114,15 @@ namespace MTCmd { } } + /// + /// Looks up a localized string similar to A valid Windows Language Code Identifier (LCID), aka language tag, such as en-US, af-NA, and zh-CN.. + /// + internal static string lcidDescription { + get { + return ResourceManager.GetString("lcidDescription", resourceCulture); + } + } + /// /// Looks up a localized string similar to The mode of the command-line operation. /// @@ -158,5 +167,14 @@ namespace MTCmd { return ResourceManager.GetString("rootCmdDescription", resourceCulture); } } + + /// + /// Looks up a localized string similar to Translations for '{0}' exported to: {1}. + /// + internal static string singleLocalExportSuccess { + get { + return ResourceManager.GetString("singleLocalExportSuccess", resourceCulture); + } + } } } diff --git a/MetadataTranslator/MTCmd/Strings.resx b/MetadataTranslator/MTCmd/Strings.resx index 972f3e6..52ee966 100644 --- a/MetadataTranslator/MTCmd/Strings.resx +++ b/MetadataTranslator/MTCmd/Strings.resx @@ -135,6 +135,9 @@ Either the imported language matches the default locale, or the file name does not correspond to a supported locale identifier. The file name convention is <lcid>.csv. + + A valid Windows Language Code Identifier (LCID), aka language tag, such as en-US, af-NA, and zh-CN. + The mode of the command-line operation @@ -150,4 +153,7 @@ Metadata Translator command-line app + + Translations for '{0}' exported to: {1} + \ No newline at end of file diff --git a/MetadataTranslator/Metadata Translator/Data/DataModel.cs b/MetadataTranslator/Metadata Translator/Data/DataModel.cs index c4ce4cd..b14a270 100644 --- a/MetadataTranslator/Metadata Translator/Data/DataModel.cs +++ b/MetadataTranslator/Metadata Translator/Data/DataModel.cs @@ -45,6 +45,8 @@ namespace Metadata_Translator public List SupportedLanguages { get; private set; } public List SelectedLanguages { get => SupportedLanguages?.Where(x => x.IsSelected==true).ToList(); } public bool HasTargetLanguages { get => SelectedLanguages?.Count > 1; } + + private const string appTag = "__MT"; /// /// Connect to the dataset by using server and database name. This is how external tools typically connect to a dataset inside of Power BI Desktop. @@ -220,6 +222,18 @@ namespace Metadata_Translator } } + /// + /// Marks all translation languages as unselected. + /// + public void DeselectAllLanguages() + { + foreach(Language lang in SelectedLanguages) + { + if (lang.IsNotModelDefault) + lang.IsSelected = false; + } + } + /// /// Marks a language specified by lcid as selected and as model default. /// @@ -397,6 +411,14 @@ namespace Metadata_Translator /// Save the changes in the dataset. /// + Annotation mtAnnotation = new Annotation(); + mtAnnotation.Name = appTag; + mtAnnotation.Value = "1"; + if (!Model.Annotations.Contains(appTag)) + { + Model.Annotations.Add(mtAnnotation); + } + Model.Database.Update(AS.UpdateOptions.ExpandFull); } diff --git a/MetadataTranslator/Metadata Translator/Documentation/README.md b/MetadataTranslator/Metadata Translator/Documentation/README.md index 82ef4e0..4e4f331 100644 --- a/MetadataTranslator/Metadata Translator/Documentation/README.md +++ b/MetadataTranslator/Metadata Translator/Documentation/README.md @@ -116,6 +116,16 @@ To export existing translations from a dataset, you must specify full path to an > > The specified export folder must exist prior to running the command-line app. MTCmd.exe does not create the export folder. However, if any files exist in the export folder, MTCmd.exe might overwrite them without warning. MTCmd.exe exports each culture into a separate .csv file based on the locale identifier (LCID). +### Generating new translations + +The -ef option exports existing translations from a dataset. If you want to add a new translation, you can specify the corresponding locale identifier by using the --locale-id (-lcid) option. For example, the following command generates a translation file in the aforementioned export folder for the locale 'de-DE': + +`MTCmd -cs "powerbi://api.powerbi.com/v1.0/myorg/AdventureWorksSource;initial catalog=AdventureWorks" -ef C:\ExportedTranslations -lcid "de-DE" ` + +> Note +> +> If a translation exists for the specified locale identifier, MTCmd exports the existing translated strings. If a translation does not exist, MTCmd generates a translation file without translated strings. You can then add the translations by using a localization tool and import the file as explained in the next section to add the translations to the dataset. For a list of supported locale identifiers, refer to the [supportedlanguages.json](https://github.com/microsoft/Analysis-Services/blob/master/MetadataTranslator/Metadata Translator/Resources/supportedlanguages.json) file. + ### Importing translations To import translations from a .csv file, you must specify full path to the import file by using the --import-file (-if) parameter. The file name must correspond to the locale identifier (LCID) of the target language. You must also specify the --mode (-m) parameter. Valid options are Import or Overwrite. Import applies translations for strings that have not been translated yet in the dataset. Overwrite, as the name implies, overwrites any existing translations in the dataset. Both, Import and Overwrite create new translations if you import a locale that does not yet exist in the dataset.