Exemplo n.º 1
0
 /// <summary>
 /// Upload resource to workspace.
 /// </summary>
 /// <param name="workspace"></param>
 /// <param name="resourceFileFormat"></param>
 /// <param name="filePath"></param>
 public static async void UploadResource(this Workspace workspace, ResourceFileFormat resourceFileFormat, string filePath = "dataset")
 {
     await _managementService.UploadResourceAsync(new WorkspaceSettings()
     {
         WorkspaceId        = workspace.WorkspaceId,
         AuthorizationToken = workspace.AuthorizationToken.PrimaryToken,
         Location           = workspace.Region
     }, resourceFileFormat.GetDescription(), filePath);
 }
 /// <summary>
 /// Upload resource to workspaces.
 /// </summary>
 /// <param name="workspaces"></param>
 /// <param name="resourceFileFormat"></param>
 /// <param name="filePath"></param>
 public static void UploadResource(this IEnumerable <Workspace> workspaces, ResourceFileFormat resourceFileFormat, string filePath = "dataset")
 {
     workspaces.ForEach(w => WorkspaceExtensions.UploadResource(w, resourceFileFormat, filePath));
 }
        public async Task <ResourceFileForResult> GetResourceFileForAsync(int branch, int file, string culture,
                                                                          bool fillEmptyWithOriginalResourceString, ResourceFileFormat format)
        {
            var branches = await GetCachedBranchesAsync().ConfigureAwait(false);

            var resourceFiles = await GetCachedResourceFilesAsync().ConfigureAwait(false);

            var languages = await GetCachedLanguagesAsync().ConfigureAwait(false);

            var theBranch   = branches.FirstOrDefault(b => b.Id == branch);
            var theFile     = resourceFiles.FirstOrDefault(rf => rf.Id == file);
            var theLanguage = languages.FirstOrDefault(l => l.Culture == culture);

            // this is a check if a wrong combo is requested
            if (null == theBranch || null == theFile || null == theLanguage)
            {
                return(new ResourceFileForResult()
                {
                    Succeeded = false
                });
            }

            // Properly format the filename, extension depends on the enum, and we always make it lowercase
            string extension = "." + format.ToString().ToLower();
            string filename  = String.Format(theFile.ResourceFileNameFormat, culture) + extension;

            // Get the translations
            var translations = await _dataService
                               .GetNonNullTranslationsForExportAsync(branch, file, culture)
                               .ConfigureAwait(false);

            // Get the original resource strings (only when needed! memory & time)
            Dictionary <string, string> resourceStrings = null;

            if (fillEmptyWithOriginalResourceString)
            {
                resourceStrings = (await _dataService
                                   .GetResourceStringsForExportAsync(branch, file)
                                   .ConfigureAwait(false)).ToDictionary(r => r.ResourceIdentifier, r => r.Value);
            }

            // Create the resource file: First the translations, then fill with the original language strings (if requested)
            var             ms     = new MemoryStream();
            IResourceWriter writer = null;

            // format decides the writer, both implement IResourceWriter
            if (ResourceFileFormat.ResX == format)
            {
                writer = new ResXResourceWriter(ms);
            }
            else
            {
                writer = new ResourceWriter(ms);
            }

            foreach (var t in translations)
            {
                writer.AddResource(t.ResourceIdentifier, t.Value);
                if (fillEmptyWithOriginalResourceString)
                {
                    resourceStrings.Remove(t.ResourceIdentifier);
                }
            }
            if (fillEmptyWithOriginalResourceString)
            {
                foreach (var t in resourceStrings)
                {
                    writer.AddResource(t.Key, t.Value);
                }
            }

            writer.Generate();
            ms.Seek(0, SeekOrigin.Begin);

            return(new ResourceFileForResult()
            {
                Succeeded = true,
                Stream = ms,
                Filename = filename
            });
        }
        public async Task<ResourceFileForResult> GetResourceFileForAsync(int branch, int file, string culture, 
            bool fillEmptyWithOriginalResourceString, ResourceFileFormat format)
        {
            var branches = await GetCachedBranchesAsync().ConfigureAwait(false);
            var resourceFiles = await GetCachedResourceFilesAsync().ConfigureAwait(false);
            var languages = await GetCachedLanguagesAsync().ConfigureAwait(false);

            var theBranch = branches.FirstOrDefault(b => b.Id == branch);
            var theFile = resourceFiles.FirstOrDefault(rf => rf.Id == file);
            var theLanguage = languages.FirstOrDefault(l => l.Culture == culture);

            // this is a check if a wrong combo is requested
            if (null == theBranch || null == theFile || null == theLanguage)
            {
                return new ResourceFileForResult()
                {
                    Succeeded = false
                };
            }
            
            // Properly format the filename, extension depends on the enum, and we always make it lowercase
            string extension = "." + format.ToString().ToLower();
            string filename = String.Format(theFile.ResourceFileNameFormat, culture) +  extension;

            // Get the translations
            var translations = await _dataService
                .GetNonNullTranslationsForExportAsync(branch, file, culture)
                .ConfigureAwait(false);

            // Get the original resource strings (only when needed! memory & time)
            Dictionary<string, string> resourceStrings = null;
            if (fillEmptyWithOriginalResourceString)
            {
                resourceStrings = (await _dataService
                    .GetResourceStringsForExportAsync(branch, file)
                    .ConfigureAwait(false)).ToDictionary(r => r.ResourceIdentifier, r => r.Value);
            }

            // Create the resource file: First the translations, then fill with the original language strings (if requested)
            var ms = new MemoryStream();
            IResourceWriter writer = null;

            // format decides the writer, both implement IResourceWriter
            if (ResourceFileFormat.ResX == format)
            {
                writer = new ResXResourceWriter(ms);
            }
            else
            {
                writer = new ResourceWriter(ms);
            }

            foreach (var t in translations)
            {
                writer.AddResource(t.ResourceIdentifier, t.Value);
                if (fillEmptyWithOriginalResourceString)
                {
                    resourceStrings.Remove(t.ResourceIdentifier);
                }
            }
            if (fillEmptyWithOriginalResourceString)
            {
                foreach (var t in resourceStrings)
                {
                    writer.AddResource(t.Key, t.Value);
                }
            }

            writer.Generate();
            ms.Seek(0, SeekOrigin.Begin);

            return new ResourceFileForResult()
            {
                Succeeded = true,
                Stream = ms,
                Filename = filename
            };
        }