public Dictionary <string, XliffTranslationFiles> GetXliffFilesPerLocale() { var files = new Dictionary <string, XliffTranslationFiles>(); foreach (var document in xlfDocuments) { string xliffFilePath = XliffFileHelpers.GetRelativeFilePathTo(baseDirectory, document.FileName); string plainFileName = XliffFileHelpers.GetPlainFileName(baseDirectory, xliffFilePath); foreach (var xlfFile in document.Files) { string storageLocation = XliffFileHelpers.GetStorageLocation(plainFileName, xlfFile.Original); string locale = XliffFileHelpers.ExtractLocaleFromFileName(xliffFilePath); if (!files.TryGetValue(storageLocation, out XliffTranslationFiles translationFiles)) { translationFiles = new XliffTranslationFiles(storageLocation); files.Add(storageLocation, translationFiles); } translationFiles.AddXliffFile(locale, xlfFile); } } return(files); }
private string GetLocale(string fileName, XlfFile xlfFile) { if (!string.IsNullOrWhiteSpace(xlfFile.Optional.TargetLang)) { return(xlfFile.Optional.TargetLang); } return(XliffFileHelpers.ExtractLocaleFromFileName(fileName)); }
/// <summary> /// Exports the resource strings from the Babylon.NET project /// </summary> /// <param name="projectName">Name of the project the resources will be exported from</param> /// <param name="projectLocale">The project invariant locale</param> /// <param name="resourceStrings">The strings to be exported</param> /// <param name="resultDelegate">Callback delegate to provide progress information and storage operation results</param> public void ExportResourceStrings(string projectName, string projectLocale, IReadOnlyCollection <string> localesToExport, ICollection <StringResource> resourceStrings, ResourceStorageOperationResultDelegate resultDelegate) { XliffResourceExporter xliffResourceExporter = new XliffResourceExporter(XliffFileHelpers.GetBaseDirectory(StorageLocation, SolutionPath), projectLocale, localesToExport, resourceStrings, (string xliffFileName) => { resultDelegate?.Invoke(new ResourceStorageOperationResultItem(xliffFileName) { ProjectName = projectName }); }, (XliffFileError fileError) => { resultDelegate?.Invoke(new ResourceStorageOperationResultItem(fileError.XliffFilePath) { ProjectName = projectName, Result = ResourceStorageOperationResult.Error, Message = fileError.Ex.Message }); }); xliffResourceExporter.Export(); }
public ICollection <StringResource> Import() { var matchedXlfFile = false; xliffDocumentProvider.LoadXlfDocuments(); foreach (var doc in xliffDocumentProvider.XlfDocuments) { string plainFileName = XliffFileHelpers.GetPlainFileName(baseDirectory, doc.FileName); foreach (var xlfFile in doc.Files) { if (!FileMatchesProjectInvariantLocale(xlfFile)) { continue; } matchedXlfFile = true; string storageLocation = XliffFileHelpers.GetStorageLocation(plainFileName, xlfFile.Original); string locale = GetLocale(doc.FileName, xlfFile); Dictionary <string, StringResource> fileStringResources = GetStringResourcesForFile(storageLocation); foreach (var transUnit in xlfFile.TransUnits) { StringResource stringResource = GetOrCreateStringResource(fileStringResources, storageLocation, transUnit); if (ShouldWriteTargetLanguage(locale, transUnit)) { stringResource.SetLocaleText(locale, transUnit.Target); } } } } ValidateXlfFilesFound(matchedXlfFile, xliffDocumentProvider.XlfDocuments.Count); return(resourcesPerFile.SelectMany(t => t.Value.Select(x => x.Value)).ToList()); }
public void SaveDocuments(IEnumerable <string> localesToSave) { foreach (var document in xlfDocuments) { string xliffFilePath = XliffFileHelpers.GetRelativeFilePathTo(baseDirectory, document.FileName); string plainFileName = XliffFileHelpers.GetPlainFileName(baseDirectory, xliffFilePath); string locale = XliffFileHelpers.ExtractLocaleFromFileName(xliffFilePath); if (!localesToSave.Contains(locale)) { continue; } try { document.Save(); savedXlfDocuments.Add(document); } catch (Exception ex) { fileSaveErrors.Add(new XliffFileError(document.FileName, ex)); } } }
/// <summary> /// Imports the resource strings to be translated into the Babylon.NET project /// </summary> /// <param name="projectName">Name of the project the resources will be imported into</param> /// <param name="projectLocale">The project invariant locale</param> /// <returns>The imported strings</returns> public ICollection <StringResource> ImportResourceStrings(string projectName, string projectLocale) { var resourceImporter = new XliffResourceImporter(projectLocale, XliffFileHelpers.GetBaseDirectory(StorageLocation, SolutionPath)); return(resourceImporter.Import()); }