Example #1
0
        private async Task <object> RetrieveTranslation(IProgress <string> progress, CancellationToken cancelToken)
        {
            LocalizationAppDomain appDomain = await StringsRetriever.CreateAppDomain(App.MainWindow.Document.PathToBinaries);

            var document = App.MainWindow.Document;

            foreach (var culture in __TranslationCultures)
            {
                if (!culture.IsSelected)
                {
                    continue;
                }

                document.AddLanguage(culture.Culture);

                StringsRetriever retriever = new StringsRetriever(App.MainWindow.Document);

                foreach (var assembly in document.Assemblies)
                {
                    string resourceFile = System.IO.Path.Combine(
                        culture.Culture.CultureCode,
                        System.IO.Path.GetFileNameWithoutExtension(assembly.Assembly.AssemblyFile) + ".resources.dll"
                        );

                    string resourceFileFullPath = System.IO.Path.Combine(document.PathToBinaries, resourceFile);

                    if (!System.IO.File.Exists(resourceFileFullPath))
                    {
                        continue;
                    }

                    string progressString = StringUtils.String("Progress_RetrievingAssemblyTranslation", resourceFile);
                    progress.Report(progressString);

                    LoadedAssembly loadedAssembly = await Task.Run <LoadedAssembly>(() =>
                    {
                        return(appDomain.LoadAssembly(
                                   resourceFileFullPath));
                    });

                    var progressReporter = new ProgressPercentageReporter(progress, progressString);

                    var lines = await retriever.ExtractTranslationLines(loadedAssembly);

                    lines = lines.Where(retriever.LinesFilter).ToList();

                    await document.ImportApi.ImportTranslationStrings(
                        progressReporter,
                        cancelToken,
                        assembly.Assembly.AssemblyFile,
                        document.AssembliesLanguage.CultureCode,
                        culture.Culture.CultureCode,
                        lines);
                }
            }

            await StringsRetriever.DisposeAppDomain(appDomain);

            return(null);
        }
 public static async Task DisposeAppDomain(LocalizationAppDomain appDomain)
 {
     await Task.Run(() =>
     {
         appDomain.Dispose();
     });
 }
Example #3
0
        private void SearchForLocalizableAssemblies()
        {
            List <LocalizableAssembly> localizableAssemblies = new List <LocalizableAssembly>();

            using (var appDomain = new LocalizationAppDomain(__PathToBinaries))
            {
                foreach (var fullFilePath in Directory.EnumerateFiles(__PathToBinaries, "*.exe"))
                {
                    CheckAndAddAssembly(
                        localizableAssemblies,
                        System.IO.Path.GetFileName(fullFilePath),
                        __PathToBinaries,
                        appDomain);
                }

                foreach (var fullFilePath in Directory.EnumerateFiles(__PathToBinaries, "*.dll"))
                {
                    CheckAndAddAssembly(
                        localizableAssemblies,
                        System.IO.Path.GetFileName(fullFilePath),
                        __PathToBinaries,
                        appDomain);
                }
            }

            SetDefaultCulture();
            __AvailableAssemblies.Clear();

            if (localizableAssemblies.Count > 0)
            {
                DefaultCulture = localizableAssemblies[0].DefaultCulture;
            }

            foreach (var assembly in localizableAssemblies)
            {
                if (assembly.DefaultCulture == DefaultCulture)
                {
                    var selectedAssembly = new AvailableAssemblyForTranslation(__AvailableAssemblies, assembly);
                    selectedAssembly.IsSelected = true;
                    __AvailableAssemblies.Add(selectedAssembly);
                }
            }

            DoPropertyChange("DefaultCulture");
        }
        public async Task <object> RetrieveAllAssemblies(IProgress <string> progress, CancellationToken cancelToken)
        {
            LocalizationAppDomain appDomain = await CreateAppDomain(__Document.PathToBinaries);

            List <ImportResult> results = new List <ImportResult>();

            foreach (var assembly in __Document.Assemblies)
            {
                progress.Report(StringUtils.String("LoadingAssembly_0", assembly.Assembly.AssemblyFile));

                LoadedAssembly loadedAssembly = await Task.Run <LoadedAssembly>(() =>
                {
                    return(appDomain.LoadAssembly(
                               System.IO.Path.Combine(__Document.PathToBinaries, assembly.Assembly.DefaultResourceFile)));
                });

                var progressString   = StringUtils.String("ImportingFromAssembly_0", assembly.Assembly.AssemblyFile);
                var progressReporter = new ProgressPercentageReporter(progress, progressString);

                var lines = await ExtractTranslationLines(loadedAssembly);

                lines = lines.Where(LinesFilter).ToList();

                ImportResult importResult = await __Document.ImportApi.ImportStrings(
                    progressReporter,
                    cancelToken,
                    assembly.Assembly.AssemblyFile, lines);

                results.Add(importResult);
            }

            progress.Report(StringUtils.String("Import_Finishing"));
            await DisposeAppDomain(appDomain);

            __Document.UpdateTranslatedCultures();
            return(results);
        }
        private async Task <object> GenerateAllAssemblies(IProgress <string> progress, CancellationToken cancelToken)
        {
            var document = App.MainWindow.Document;

            LocalizationAppDomain appDomain = await StringsRetriever.CreateAppDomain(document.PathToBinaries);

            List <AssemblyGenerationResult> resultsList = new List <AssemblyGenerationResult>();

            Dictionary <string, LoadedAssembly> loadedAssemblies = new Dictionary <string, LoadedAssembly>();

            foreach (var cultureItem in Cultures)
            {
                if (!cultureItem.IsSelected)
                {
                    continue;
                }

                foreach (var assemblyItem in Assemblies)
                {
                    if (!assemblyItem.IsSelected)
                    {
                        continue;
                    }

                    string satteliteTargetFileName  = System.IO.Path.GetFileName(assemblyItem.Assembly.Assembly.DefaultResourceFile);
                    AssemblyGenerationResult result = new AssemblyGenerationResult(satteliteTargetFileName, cultureItem.Culture.CultureCode);

                    if (cancelToken.IsCancellationRequested)
                    {
                        result.IsError = true;
                        result.Log.LogLine(StringUtils.String("AssemblyResult_Cancelled"), LogSeverity.Info);
                        result.ResultMessage = StringUtils.String("AssemblyResult_Cancelled");
                        continue;
                    }

                    try
                    {
                        string assemblyToLoad = System.IO.Path.Combine(document.PathToBinaries,
                                                                       assemblyItem.Assembly.Assembly.DefaultResourceFile);
                        LoadedAssembly loadedAssembly;

                        if (loadedAssemblies.ContainsKey(assemblyToLoad))
                        {
                            loadedAssembly = loadedAssemblies[assemblyToLoad];
                        }
                        else
                        {
                            loadedAssembly = await Task.Run <LoadedAssembly>(() =>
                            {
                                return(appDomain.LoadAssembly(assemblyToLoad));
                            });

                            loadedAssemblies.Add(assemblyToLoad, loadedAssembly);
                        }

                        string outputDirectory = System.IO.Path.Combine(document.PathToBinaries, cultureItem.Culture.CultureCode);

                        if (!Directory.Exists(outputDirectory))
                        {
                            Directory.CreateDirectory(outputDirectory);
                        }

                        string satteliteTargetFullPath = System.IO.Path.Combine(outputDirectory, satteliteTargetFileName);

                        progress.Report(StringUtils.String("GenerationAssemblyForCulture", satteliteTargetFileName, cultureItem.Culture.CultureCode));

                        await Task.Run(() =>
                        {
                            List <BamlString> translatedStrings = document.ExportApi.GetTranslatedStrings(
                                assemblyItem.Assembly.Assembly.AssemblyFile, cultureItem.Culture.CultureCode);
                            loadedAssembly.GenerateSatteliteAssembly(cultureItem.Culture.CultureCode, translatedStrings, satteliteTargetFullPath);
                        });

                        result.ResultMessage = StringUtils.String("AssemblyResult_Created");
                        resultsList.Add(result);
                    }
                    catch (Exception e)
                    {
                        result.IsError = true;
                        result.Log.LogLine(e.Message, LogSeverity.Error);
                        result.ResultMessage = StringUtils.String("AssemblyResult_Exception");

                        resultsList.Add(result);
                        continue;
                    }
                }
            }

            return(resultsList);
        }
Example #6
0
        public async Task <object> SearchForLocalizableAssemblies(IProgress <string> progress, CancellationToken cancelToken)
        {
            List <LocalizableAssembly> localizableAssemblies = new List <LocalizableAssembly>();

            List <string> alreadyDefinedCultures = null;

            progress.Report(StringUtils.String("SearchingForAssemblies"));

            await Task.Run(() =>
            {
                using (var appDomain = new LocalizationAppDomain(Document.PathToBinaries))
                {
                    foreach (var fullFilePath in Directory.EnumerateFiles(Document.PathToBinaries, "*.exe"))
                    {
                        CheckAndAddAssembly(
                            localizableAssemblies,
                            System.IO.Path.GetFileName(fullFilePath),
                            Document.PathToBinaries,
                            appDomain);
                    }

                    foreach (var fullFilePath in Directory.EnumerateFiles(Document.PathToBinaries, "*.dll"))
                    {
                        CheckAndAddAssembly(
                            localizableAssemblies,
                            System.IO.Path.GetFileName(fullFilePath),
                            Document.PathToBinaries,
                            appDomain);
                    }

                    alreadyDefinedCultures = FindTranslationCultures(localizableAssemblies);
                }
            });

            __AvailableAssemblies.Clear();

            if (localizableAssemblies.Count > 0)
            {
                DefaultCulture = new Culture(localizableAssemblies[0].DefaultCulture);
            }

            foreach (var assembly in localizableAssemblies)
            {
                if (assembly.DefaultCulture == DefaultCulture.CultureCode)
                {
                    var selectedAssembly = new AvailableAssemblyForTranslation(__AvailableAssemblies, assembly);
                    selectedAssembly.IsSelected = true;
                    __AvailableAssemblies.Add(selectedAssembly);
                }
            }

            __TranslationCultures.Clear();

            if (AddRemoveAssembliesOnly)
            {
                return(null);
            }

            if (alreadyDefinedCultures != null)
            {
                FillTranslationCultures(alreadyDefinedCultures);
            }


            return(null);
        }
Example #7
0
        private void CheckAndAddAssembly(List <LocalizableAssembly> localizableAssemblies, string file, string basePath, LocalizationAppDomain appDomain)
        {
            string fullPathToAssembly = System.IO.Path.Combine(Document.PathToBinaries, file);

            if (!LoadedAssembly.IsNetAssembly(fullPathToAssembly))
            {
                return;
            }

            LoadedAssembly assembly = appDomain.LoadAssembly(fullPathToAssembly);

            if (assembly.WasExceptionOnLoad)
            {
                return;
            }

            if (assembly.NeutralResourceCultureName == "")
            {
                return;
            }

            var assemblyName = System.IO.Path.GetFileNameWithoutExtension(file);
            var resourceFile = System.IO.Path.Combine(
                assembly.NeutralResourceCultureName,
                String.Format("{0}.resources.dll", assemblyName));

            var resourceFullPath = System.IO.Path.Combine(
                Document.PathToBinaries,
                resourceFile);

            if (!File.Exists(resourceFullPath))
            {
                return;
            }

            localizableAssemblies.Add(new LocalizableAssembly(file,
                                                              assembly.NeutralResourceCultureName,
                                                              resourceFile));
            return;
        }