Added option to generate empty translation files on the command line.

This commit is contained in:
Kay Unkroth 2021-03-12 10:09:07 -08:00
parent 9e13b2fa17
commit ecef6133fb
5 changed files with 71 additions and 5 deletions

View File

@ -32,12 +32,14 @@ namespace MTCmd
new Option<DirectoryInfo>( new Option<DirectoryInfo>(
new string[]{ "--export-folder", "-ef" }, Strings.efDescription).ExistingOnly(), new string[]{ "--export-folder", "-ef" }, Strings.efDescription).ExistingOnly(),
new Option<FileInfo>( new Option<FileInfo>(
new string[]{ "--import-file", "-if" }, Strings.ifDescription).ExistingOnly() new string[]{ "--import-file", "-if" }, Strings.ifDescription).ExistingOnly(),
new Option<string>(
new string[]{ "--locale-id", "-lcid" }, Strings.lcidDescription)
}; };
// Note that the parameters of the handler method are matched according to the names of the options // Note that the parameters of the handler method are matched according to the names of the options
rootCommand.Handler = CommandHandler.Create<string, Mode, DirectoryInfo, FileInfo>((connectionString, mode, exportFolder, importFile) => rootCommand.Handler = CommandHandler.Create<string, Mode, DirectoryInfo, FileInfo, string>((connectionString, mode, exportFolder, importFile, localeId) =>
{ {
try try
{ {
@ -47,7 +49,7 @@ namespace MTCmd
switch (mode) switch (mode)
{ {
case Mode.Export: case Mode.Export:
Export(model, exportFolder); Export(model, exportFolder, localeId);
break; break;
case Mode.Import: case Mode.Import:
Import(model, importFile, false); 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 (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); model.ExportToCsv(exportFolder.FullName);
Console.WriteLine(Strings.exportSuccess, exportFolder); Console.WriteLine(Strings.exportSuccess, exportFolder);

View File

@ -114,6 +114,15 @@ namespace MTCmd {
} }
} }
/// <summary>
/// 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..
/// </summary>
internal static string lcidDescription {
get {
return ResourceManager.GetString("lcidDescription", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to The mode of the command-line operation. /// Looks up a localized string similar to The mode of the command-line operation.
/// </summary> /// </summary>
@ -158,5 +167,14 @@ namespace MTCmd {
return ResourceManager.GetString("rootCmdDescription", resourceCulture); return ResourceManager.GetString("rootCmdDescription", resourceCulture);
} }
} }
/// <summary>
/// Looks up a localized string similar to Translations for &apos;{0}&apos; exported to: {1}.
/// </summary>
internal static string singleLocalExportSuccess {
get {
return ResourceManager.GetString("singleLocalExportSuccess", resourceCulture);
}
}
} }
} }

View File

@ -135,6 +135,9 @@
<data name="invalidLocale" xml:space="preserve"> <data name="invalidLocale" xml:space="preserve">
<value>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 &lt;lcid&gt;.csv.</value> <value>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 &lt;lcid&gt;.csv.</value>
</data> </data>
<data name="lcidDescription" xml:space="preserve">
<value>A valid Windows Language Code Identifier (LCID), aka language tag, such as en-US, af-NA, and zh-CN.</value>
</data>
<data name="modeDescription" xml:space="preserve"> <data name="modeDescription" xml:space="preserve">
<value>The mode of the command-line operation</value> <value>The mode of the command-line operation</value>
</data> </data>
@ -150,4 +153,7 @@
<data name="rootCmdDescription" xml:space="preserve"> <data name="rootCmdDescription" xml:space="preserve">
<value>Metadata Translator command-line app</value> <value>Metadata Translator command-line app</value>
</data> </data>
<data name="singleLocalExportSuccess" xml:space="preserve">
<value>Translations for '{0}' exported to: {1}</value>
</data>
</root> </root>

View File

@ -46,6 +46,8 @@ namespace Metadata_Translator
public List<Language> SelectedLanguages { get => SupportedLanguages?.Where(x => x.IsSelected==true).ToList(); } public List<Language> SelectedLanguages { get => SupportedLanguages?.Where(x => x.IsSelected==true).ToList(); }
public bool HasTargetLanguages { get => SelectedLanguages?.Count > 1; } public bool HasTargetLanguages { get => SelectedLanguages?.Count > 1; }
private const string appTag = "__MT";
/// <summary> /// <summary>
/// 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. /// 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.
/// </summary> /// </summary>
@ -220,6 +222,18 @@ namespace Metadata_Translator
} }
} }
/// <summary>
/// Marks all translation languages as unselected.
/// </summary>
public void DeselectAllLanguages()
{
foreach(Language lang in SelectedLanguages)
{
if (lang.IsNotModelDefault)
lang.IsSelected = false;
}
}
/// <summary> /// <summary>
/// Marks a language specified by lcid as selected and as model default. /// Marks a language specified by lcid as selected and as model default.
/// </summary> /// </summary>
@ -397,6 +411,14 @@ namespace Metadata_Translator
/// Save the changes in the dataset. /// 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); Model.Database.Update(AS.UpdateOptions.ExpandFull);
} }

View File

@ -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). > 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 ### 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. 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.