Example #1
0
        private void GenerateBamlStream(Stream input, Stream output, Dictionaries curDictionaries)
        {
            string     commentFile   = Path.ChangeExtension(options.Input, "loc");
            TextReader commentStream = null;

            try
            {
                if (File.Exists(commentFile))
                {
                    commentStream = new StreamReader(commentFile);
                }

                // create a localizabilty resolver based on reflection
                var localizabilityReflector =
                    new BamlLocalizabilityByReflection(options.Assemblies);

                // create baml localizer
                var mgr = new BamlLocalizer(
                    input,
                    localizabilityReflector,
                    commentStream
                    );

                // get the resources
                var source       = mgr.ExtractResources();
                var translations = new BamlLocalizationDictionary();

                foreach (DictionaryEntry entry in source)
                {
                    var key = (BamlLocalizableResourceKey)entry.Key;
                    var translatedResource = curDictionaries.FindCorrespondence(key);
                    if (translatedResource != null)
                    {
                        string translatedContent = translatedResource.Content;

                        if (!String.IsNullOrEmpty(translatedContent))
                        {
                            var curResource = (BamlLocalizableResource)entry.Value;
                            if (curResource.Content != translatedContent)
                            {
                                translations.Add(key, translatedResource);
                            }
                        }
                    }
                }

                // update baml
                mgr.UpdateBaml(output, translations);
            }
            finally
            {
                if (commentStream != null)
                {
                    commentStream.Close();
                }
            }
        }
        private static string GenerateBamlStream(Stream input, Stream output, BamlLocalizationDictionary dictionary, LocBamlOptions options)
        {
            string     commentFile   = Path.ChangeExtension(options.Input, "loc");
            TextReader commentStream = null;

            try
            {
                if (File.Exists(commentFile))
                {
                    commentStream = new StreamReader(commentFile);
                }

                // create a localizabilty resolver based on reflection
                BamlLocalizabilityByReflection localizabilityReflector =
                    new BamlLocalizabilityByReflection(options.Assemblies);

                // create baml localizer
                BamlLocalizer mgr = new BamlLocalizer(
                    input,
                    localizabilityReflector,
                    commentStream
                    );

                // get the resources
                BamlLocalizationDictionary source       = mgr.ExtractResources();
                BamlLocalizationDictionary translations = new BamlLocalizationDictionary();

                foreach (DictionaryEntry entry in dictionary)
                {
                    BamlLocalizableResourceKey key = (BamlLocalizableResourceKey)entry.Key;
                    // filter out unchanged items
                    if (!source.Contains(key) ||
                        entry.Value == null ||
                        source[key].Content != ((BamlLocalizableResource)entry.Value).Content)
                    {
                        translations.Add(key, (BamlLocalizableResource)entry.Value);
                    }
                }

                // update baml
                mgr.UpdateBaml(output, translations);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
            finally
            {
                if (commentStream != null)
                {
                    commentStream.Close();
                }
            }
            return(null);
        }