|
| 1 | +/* **************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (c) Microsoft Corporation. |
| 4 | + * |
| 5 | + * This source code is subject to terms and conditions of the Apache License, Version 2.0. A |
| 6 | + * copy of the license can be found in the License.html file at the root of this distribution. |
| 7 | + * By using this source code in any fashion, you are agreeing to be bound by the terms of |
| 8 | + * the Apache License, Version 2.0. |
| 9 | + * |
| 10 | + * You must not remove this notice, or any other, from this software. |
| 11 | + * |
| 12 | + * ***************************************************************************/ |
| 13 | + |
| 14 | +using System; |
| 15 | +using System.IO; |
| 16 | + |
| 17 | +namespace VsctToXliff |
| 18 | +{ |
| 19 | + class Program |
| 20 | + { |
| 21 | + private static readonly string[] Locales = new[] { "cs", "de", "en", "es", "fr", "it", "ja", "ko", "pl", "pt-BR", "ru", "tr", "zh-Hans", "zh-Hant" }; |
| 22 | + |
| 23 | + private static int Main(string[] args) |
| 24 | + { |
| 25 | + var parsedArgs = new Args(args); |
| 26 | + if (parsedArgs.IsError) |
| 27 | + { |
| 28 | + return -1; |
| 29 | + } |
| 30 | + |
| 31 | + switch (parsedArgs.Mode) |
| 32 | + { |
| 33 | + case Mode.GenerateXliff: |
| 34 | + CreateXliffFiles(parsedArgs.SourceFile, parsedArgs.XliffDir); |
| 35 | + return 0; |
| 36 | + case Mode.GenerateVsct: |
| 37 | + CreateVsctFiles(parsedArgs.SourceFile, parsedArgs.XliffDir); |
| 38 | + return 0; |
| 39 | + case Mode.Error: |
| 40 | + default: |
| 41 | + Console.WriteLine($"Unexpected processing mode: \'{parsedArgs.Mode}\'."); |
| 42 | + return -1; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static void CreateXliffFiles(string sourceFile, string xlfDir) |
| 47 | + { |
| 48 | + if (string.IsNullOrEmpty(sourceFile) || string.IsNullOrEmpty(xlfDir)) |
| 49 | + { |
| 50 | + throw new ArgumentNullException("file and targetDir should be set."); |
| 51 | + } |
| 52 | + |
| 53 | + var rootName = Utilities.VsctFileNameWithoutExtension(sourceFile); |
| 54 | + |
| 55 | + var reader = new VsctFile(sourceFile); |
| 56 | + var writer = new XliffFile(xlfDir, rootName); |
| 57 | + |
| 58 | + foreach (var locale in Locales) |
| 59 | + { |
| 60 | + writer.WriteTranslationFile(sourceFile, reader.ReadTranslatableUnits(), locale); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static void CreateVsctFiles(string sourceFile, string xlfDir) |
| 65 | + { |
| 66 | + if (string.IsNullOrEmpty(sourceFile)) |
| 67 | + { |
| 68 | + throw new ArgumentNullException("file should be set."); |
| 69 | + } |
| 70 | + |
| 71 | + var targetDir = Path.GetDirectoryName(sourceFile); |
| 72 | + var rootName = Utilities.VsctFileNameWithoutExtension(sourceFile); |
| 73 | + |
| 74 | + var vsctFile = new VsctFile(sourceFile); |
| 75 | + var xlfFiles = new XliffFile(xlfDir, rootName); |
| 76 | + |
| 77 | + foreach (var locale in Locales) |
| 78 | + { |
| 79 | + if (StringComparer.OrdinalIgnoreCase.Equals(locale, "en")) |
| 80 | + { |
| 81 | + // for english just copy the file to a new file name |
| 82 | + var destFileName = Path.Combine(targetDir, $"{rootName}.en{VsctFile.VsctExt}"); |
| 83 | + File.Copy(sourceFile, destFileName, overwrite: true); |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + var translations = xlfFiles.LoadTranslatedElements(locale); |
| 88 | + vsctFile.WriteTranslatedFile(translations, locale); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private class Args |
| 94 | + { |
| 95 | + public bool IsError { get; } |
| 96 | + |
| 97 | + public string XliffDir { get; } |
| 98 | + |
| 99 | + public string SourceFile { get; } |
| 100 | + |
| 101 | + public Mode Mode { get; } |
| 102 | + |
| 103 | + public Args(string[] args) |
| 104 | + { |
| 105 | + if (args.Length < 3) |
| 106 | + { |
| 107 | + this.IsError = true; |
| 108 | + DisplayHelp(); |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + this.SourceFile = Utilities.EnsureRootPath(args[0]); |
| 113 | + this.XliffDir = Utilities.EnsureRootPath(args[1]); |
| 114 | + switch (args[2].ToLowerInvariant()) |
| 115 | + { |
| 116 | + case "--generatexlf": |
| 117 | + this.Mode = Mode.GenerateXliff; |
| 118 | + break; |
| 119 | + case "--generatevsct": |
| 120 | + this.Mode = Mode.GenerateVsct; |
| 121 | + break; |
| 122 | + default: |
| 123 | + this.IsError = true; |
| 124 | + break; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private void DisplayHelp() |
| 130 | + { |
| 131 | + Console.WriteLine("usage: VsctToXliff.exe <sourcefile.vsct> <xliff dir> [--generatexlf | --generatevsct]."); |
| 132 | + Console.WriteLine("--generatexlf\tThis will create xlf files for all VS locales in the xliff dir, overwriting any existing files!"); |
| 133 | + Console.WriteLine("--generatevsct\tThis will create vsct files for all VS locales in the same dir as the sourecfile.vsct, overwriting any existing files!"); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private enum Mode |
| 138 | + { |
| 139 | + Error, |
| 140 | + GenerateXliff, |
| 141 | + GenerateVsct, |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments