コード例 #1
0
        //----------------------------------
        // private method
        //----------------------------------

        /// <summary>
        /// Serialize the tree out to the stream.
        /// </summary>
        private void SerializeImp(
            BamlLocalizer           localizer,
            BamlTree                tree, 
            Stream                  output
            )
        {

            Debug.Assert(output != null, "The output stream given is null");
            Debug.Assert(tree != null && tree.Root != null, "The tree to be serialized is null.");

            _writer = new BamlWriter(output);            
            _bamlTreeStack = new Stack<BamlTreeNode>();
        
            // intialize the stack.
            _bamlTreeStack.Push(tree.Root);

            while (_bamlTreeStack.Count > 0)
            {
                BamlTreeNode currentNode = _bamlTreeStack.Pop();
                if (!currentNode.Visited)
                {           
                    // Mark this node so that it won't be serialized again.
                    currentNode.Visited = true; 
                    currentNode.Serialize(_writer);            
                    PushChildrenToStack(currentNode.Children);                                   
                }
                else
                {
                    BamlStartElementNode elementNode = currentNode as BamlStartElementNode;
                    Debug.Assert(elementNode != null);                    

                    if (elementNode != null)
                    {                   
                        localizer.RaiseErrorNotifyEvent(
                            new BamlLocalizerErrorNotifyEventArgs(
                                BamlTreeMap.GetKey(elementNode),
                                BamlLocalizerError.DuplicateElement
                            )
                        );
                    }                                     
                }
            }
            
            // do not close stream as we don't own it.            
        }
コード例 #2
0
        //----------------------------------
        // internal Constructor
        //----------------------------------

        /// <summary>
        /// BamlTreeMap.
        /// </summary>
        internal BamlTreeMap(
            BamlLocalizer               localizer,
            BamlTree                    tree, 
            BamlLocalizabilityResolver  resolver,
            TextReader                  comments
            )
        {
            Debug.Assert(tree!= null, "Baml Tree is empty");
            Debug.Assert(localizer!= null, "BamlLocalizer is null");            

            _tree = tree;
            
            // creates an internal resolver which willd delegate calls to client's resolver intelligently.
            _resolver = new InternalBamlLocalizabilityResolver(localizer, resolver, comments);

            // create a LocalizableResourceBuilder to build localizable resources
            _localizableResourceBuilder = new LocalizableResourceBuilder(_resolver);
        }        
コード例 #3
0
 //-------------------------------
 // Internal static
 //-------------------------------
 internal static void Serialize(BamlLocalizer localizer, BamlTree tree, Stream output)
 {
     // Thread safe implementation
     (new BamlResourceSerializer()).SerializeImp(localizer, tree, output);
 }
コード例 #4
0
        private static void 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);
            }
            finally
            {
                if (commentStream != null)
                {
                    commentStream.Close();
                }
            }
        }
コード例 #5
0
 internal InternalBamlLocalizabilityResolver(
     BamlLocalizer              localizer,
     BamlLocalizabilityResolver externalResolver,
     TextReader                 comments
     )
 {
     _localizer                  = localizer;
     _externalResolver           = externalResolver;
     _commentingText             = comments;
 }        
コード例 #6
0
        /// <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)
                        {
                            BamlLocalizableResourceKey key = (BamlLocalizableResourceKey)entry.Key;
                            BamlLocalizableResource resource = (BamlLocalizableResource)entry.Value;

                            // Output XlmData
                            if (key.Uid.Contains("XmlData") && resource.Category == LocalizationCategory.None) resource.Category = LocalizationCategory.XmlData;
                            // Ignore Category None or no content
                            if (resource.Category == LocalizationCategory.None) continue;
                            if (string.IsNullOrEmpty(resource.Content)) continue;

                            // column 1: baml stream name
                            writer.WriteColumn(bamlStreamList[i].Name);

                            // 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();
            }
        }