public void BasicLoadAndTranslate() { string source = @" Say hello: @@@idhello|Hello@@@<end> Say goodbye: @@@idgoodbye|Goodbye@@@<end> "; var translations = new Dictionary <string, string> { ["idhello"] = "Bonjour!", ["idgoodbye"] = "Au revoir!", }; string expectedTranslation = @" Say hello: Bonjour!<end> Say goodbye: Au revoir!<end> "; var document = new UnstructuredDocument(); var writer = new StringWriter(); document.Load(new StringReader(source)); document.Translate(translations); document.Save(writer); AssertEx.EqualIgnoringLineEndings(expectedTranslation, writer.ToString()); }
public void SourceEndsWithTranslatableSpan() { string source = "@@@idhello|Hello@@@"; var translations = new Dictionary <string, string> { ["idhello"] = "Bonjour!", }; string expectedTranslation = "Bonjour!"; var document = new UnstructuredDocument(); var writer = new StringWriter(); document.Load(new StringReader(source)); document.Translate(translations); document.Save(writer); AssertEx.EqualIgnoringLineEndings(expectedTranslation, writer.ToString()); }
private ITaskItem TransformTemplate(ITaskItem template, string language, IDictionary <string, ITaskItem> resourceMap) { if ((language == null) ^ (resourceMap == null)) { throw new ArgumentException($"Either both '{nameof(language)}' and '{nameof(resourceMap)}' must be specified, or they both must be 'null'."); } var transformingDefaultTemplate = language == null; var templateCulture = transformingDefaultTemplate ? "1033" : language; var templateName = Path.GetFileNameWithoutExtension(template.ItemSpec); var templatePath = template.GetMetadata("FullPath"); var templateDirectory = Path.GetDirectoryName(templatePath); var templateXml = XDocument.Load(templatePath); // create a copy of the .vstemplate and all files var localizedTemplateDirectory = transformingDefaultTemplate ? Path.Combine(TranslatedOutputDirectory, $"{templateName}.default.1033") : Path.Combine(TranslatedOutputDirectory, $"{templateName}.{language}"); Directory.CreateDirectory(localizedTemplateDirectory); var cultureSpecificTemplateFile = Path.Combine(localizedTemplateDirectory, Path.GetFileName(template.ItemSpec)); File.Copy(templatePath, cultureSpecificTemplateFile, overwrite: true); // copy the template project files foreach (var projectNode in templateXml.Descendants().Where(d => d.Name.LocalName == "Project")) { var projectFileFullPath = Path.Combine(templateDirectory, projectNode.Attribute("File").Value); File.Copy(projectFileFullPath, Path.Combine(localizedTemplateDirectory, Path.GetFileName(projectNode.Attribute("File").Value)), overwrite: true); } // copy the template project items foreach (var templateItem in templateXml.Descendants().Where(d => d.Name.LocalName == "ProjectItem")) { var templateItemFullPath = Path.Combine(templateDirectory, templateItem.Value); var templateItemDestinationPath = Path.Combine(localizedTemplateDirectory, templateItem.Value); if (transformingDefaultTemplate) { // if not localizing anything, simply strip out the translation markers var document = new UnstructuredDocument(); document.Load(templateItemFullPath); var defaultTranslation = document.Nodes.ToDictionary(node => node.Id, node => node.Source); document.Translate(defaultTranslation); document.Save(templateItemDestinationPath); } else { // try to localize the template items if (resourceMap.TryGetValue(templateItemFullPath, out var unstructuredResource)) { // copy a localized file var localizedFileName = string.Concat( Path.GetFileNameWithoutExtension(unstructuredResource.ItemSpec), ".", language, Path.GetExtension(unstructuredResource.ItemSpec)); File.Copy(Path.Combine(TranslatedOutputDirectory, localizedFileName), templateItemDestinationPath, overwrite: true); } else { // copy the original unaltered file File.Copy(templateItemFullPath, templateItemDestinationPath, overwrite: true); } } } var item = new TaskItem(cultureSpecificTemplateFile); item.SetMetadata("Culture", templateCulture); return(item); }