Exemple #1
0
        /// <summary>
        /// Write the localizable key-value pairs
        /// </summary>
        /// <param name="options"></param>
        internal static void Write(LocBamlOptions options)
        {
            InputBamlStreamList bamlStreamList = new InputBamlStreamList(options);

            using (ITranslationWriter writer = options.GetTranslationWriter())
            {
                options.WriteLine(StringLoader.Get("WriteBamlValues"));
                for (int i = 0; i < bamlStreamList.Count; i++)
                {
                    options.Write("    ");
                    options.Write(StringLoader.Get("ProcessingBaml", bamlStreamList[i].Name));

                    // Search for comment file in the same directory. The comment file has the extension to be
                    // "loc".
                    string     commentFile   = Path.ChangeExtension(bamlStreamList[i].Name, "loc");
                    TextReader commentStream = null;

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

                        // create the baml localizer
                        BamlLocalizer mgr = new BamlLocalizer(
                            bamlStreamList[i].Stream,
                            new BamlLocalizabilityByReflection(options.Assemblies),
                            commentStream
                            );

                        // extract localizable resource from the baml stream
                        BamlLocalizationDictionary dict = mgr.ExtractResources();

                        // write out each resource
                        foreach (DictionaryEntry entry in dict)
                        {
                            BamlLocalizableResourceKey key      = (BamlLocalizableResourceKey)entry.Key;
                            BamlLocalizableResource    resource = (BamlLocalizableResource)entry.Value;

                            writer.WriteResource(bamlStreamList[i].Name, LocBamlConst.ResourceKeyToString(key), resource);
                        }

                        options.WriteLine(StringLoader.Get("Done"));
                    }
                    finally
                    {
                        if (commentStream != null)
                        {
                            commentStream.Close();
                        }
                    }
                }

                // close all the baml input streams, output stream is closed by writer.
                bamlStreamList.Close();
            }
        }
Exemple #2
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();
                }
            }
        }
Exemple #3
0
        internal static List <BamlString> ExtractBamlStrings(Assembly assembly)
        {
            InputBamlStreamList bamlStreamList = new InputBamlStreamList(assembly);

            List <BamlString> resultingList = new List <BamlString>();

            for (int i = 0; i < bamlStreamList.Count; i++)
            {
                // Search for comment file in the same directory. The comment file has the extension to be
                // "loc".
//                string commentFile = Path.ChangeExtension(bamlStreamList[i].Name, "loc");
                TextReader commentStream = null;

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

                    // create the baml localizer
                    BamlLocalizer mgr = new BamlLocalizer(
                        bamlStreamList[i].Stream,
                        new BamlLocalizabilityByReflection(new Assembly[] { assembly }),
                        commentStream
                        );

                    // extract localizable resource from the baml stream
                    BamlLocalizationDictionary dict = mgr.ExtractResources();

                    // write out each resource
                    foreach (DictionaryEntry entry in dict)
                    {
                        BamlLocalizableResourceKey key      = (BamlLocalizableResourceKey)entry.Key;
                        BamlLocalizableResource    resource = (BamlLocalizableResource)entry.Value;

                        resultingList.Add(new BamlString(bamlStreamList[i].Name, LocBamlConst.ResourceKeyToString(key), resource));
                    }
                }
                finally
                {
                    if (commentStream != null)
                    {
                        commentStream.Close();
                    }
                }
            }

            // close all the baml input streams, output stream is closed by writer.
            bamlStreamList.Close();

            // options.WriteLine(StringLoader.Get("Done"));


            return(resultingList);
        }
        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);
        }
    public static void Main(string[] args)
    {
        if (args.Length != 1)
        {
            Console.WriteLine("this.exe [resource.dll]");
            return;
        }

        Assembly assembly = Assembly.LoadFrom(args[0]);

        foreach (string resourceName in assembly.GetManifestResourceNames())
        {
            Stream resourceStream = assembly.GetManifestResourceStream(resourceName);
            using (ResourceReader reader = new ResourceReader(resourceStream))
            {
                foreach (DictionaryEntry entry in reader)
                {
                    string name = entry.Key as string;

                    if (Path.GetExtension(name).ToUpperInvariant() == ".BAML")
                    {
                        Console.WriteLine("Processing baml {0}", name);

                        // <Snippet1>

                        // Obtain the BAML stream.
                        Stream source = entry.Value as Stream;

                        // Create a BamlLocalizer on the stream.
                        BamlLocalizer localizer = new BamlLocalizer(source);
                        BamlLocalizationDictionary resources = localizer.ExtractResources();

                        // Write out all the localizable resources in the BAML.
                        foreach (DictionaryEntry resourceEntry in resources)
                        {
                            BamlLocalizableResourceKey key   = resourceEntry.Key as BamlLocalizableResourceKey;
                            BamlLocalizableResource    value = resourceEntry.Value as BamlLocalizableResource;
                            Console.WriteLine(
                                "    {0}.{1}.{2} = {3}",
                                key.Uid,
                                key.ClassName,
                                key.PropertyName,
                                value.Content
                                );
                        }
                        // </Snippet1>

                        Console.WriteLine("Done");
                    }
                }
            }
        }
    }
        /// <summary>
        /// Write the localizable key-value pairs
        /// </summary>
        /// <param name="options"></param>
        internal static void Write(LocBamlOptions options)
        {
            Stream output = new FileStream(options.Output, FileMode.Create);
            InputBamlStreamList bamlStreamList = new InputBamlStreamList(options);

            using (ResourceTextWriter writer = new ResourceTextWriter(options.TranslationFileType, output))
            {
                options.WriteLine(StringLoader.Get("WriteBamlValues"));
                for (int i = 0; i < bamlStreamList.Count; i++)
                {
                    options.Write("    ");
                    options.Write(StringLoader.Get("ProcessingBaml", bamlStreamList[i].Name));

                    // Search for comment file in the same directory. The comment file has the extension to be
                    // "loc".
                    string     commentFile   = Path.ChangeExtension(bamlStreamList[i].Name, "loc");
                    TextReader commentStream = null;

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

                        // create the baml localizer
                        BamlLocalizer mgr = new BamlLocalizer(
                            bamlStreamList[i].Stream,
                            new BamlLocalizabilityByReflection(options.Assemblies),
                            commentStream
                            );

                        // extract localizable resource from the baml stream
                        BamlLocalizationDictionary dict = mgr.ExtractResources();

                        // write out each resource
                        foreach (DictionaryEntry entry in dict)
                        {
                            // column 1: baml stream name
                            writer.WriteColumn(bamlStreamList[i].Name);

                            BamlLocalizableResourceKey key      = (BamlLocalizableResourceKey)entry.Key;
                            BamlLocalizableResource    resource = (BamlLocalizableResource)entry.Value;

                            // column 2: localizable resource key
                            writer.WriteColumn(LocBamlConst.ResourceKeyToString(key));

                            // column 3: localizable resource's category
                            writer.WriteColumn(resource.Category.ToString());

                            // column 4: localizable resource's readability
                            writer.WriteColumn(resource.Readable.ToString());

                            // column 5: localizable resource's modifiability
                            writer.WriteColumn(resource.Modifiable.ToString());

                            // column 6: localizable resource's localization comments
                            writer.WriteColumn(resource.Comments);

                            // column 7: localizable resource's content
                            writer.WriteColumn(resource.Content);

                            // Done. finishing the line
                            writer.EndLine();
                        }

                        options.WriteLine(StringLoader.Get("Done"));
                    }
                    finally
                    {
                        if (commentStream != null)
                        {
                            commentStream.Close();
                        }
                    }
                }

                // close all the baml input streams, output stream is closed by writer.
                bamlStreamList.Close();
            }
        }