Пример #1
0
        private static void CreateMissingBamlTreeNode(
            BamlLocalizationDictionary dictionary, 
            BamlTreeUpdateMap          treeMap
            )
        {
            BamlLocalizationDictionaryEnumerator enumerator = dictionary.GetEnumerator(); 
            while (enumerator.MoveNext())
            { 
                BamlLocalizableResourceKey key   = enumerator.Key; 
                BamlLocalizableResource resource = enumerator.Value;
 
                // get the baml tree node from the tree
                BamlTreeNode node = treeMap.MapKeyToBamlTreeNode(key);

                if (node == null) 
                {
                    if (key.PropertyName == BamlConst.ContentSuffix) 
                    { 
                        // see if there is already a Baml node with the Uid. If so
                        // ignore this entry 
                        node = treeMap.MapUidToBamlTreeElementNode(key.Uid);
                        if (node == null)
                        {
                            // create new Baml element node 
                            BamlStartElementNode newNode    = new BamlStartElementNode(
                                treeMap.Resolver.ResolveAssemblyFromClass(key.ClassName), 
                                key.ClassName, 
                                false, /*isInjected*/
                                false /*CreateUsingTypeConverter*/ 
                                );

                            // create new x:Uid node for this element node
                            newNode.AddChild( 
                                new BamlDefAttributeNode(
                                    XamlReaderHelper.DefinitionUid, 
                                    key.Uid 
                                    )
                                ); 

                            TryAddContentPropertyToNewElement(treeMap, newNode);

                            // terminate the node with EndElementNode 
                            newNode.AddChild(new BamlEndElementNode());
 
                            // store this new node into the map so that it can be found 
                            // when other translations reference it as a childplace holder, or property owner
                            treeMap.AddBamlTreeNode(key.Uid, key, newNode); 
                        }
                    }
                    else
                    { 
                        BamlTreeNode newNode;
                        if (key.PropertyName == BamlConst.LiteralContentSuffix) 
                        { 
                            // create a LiterContent node
                            newNode = new BamlLiteralContentNode(resource.Content); 
                        }
                        else
                        {
                            newNode = new BamlPropertyNode( 
                                treeMap.Resolver.ResolveAssemblyFromClass(key.ClassName),
                                key.ClassName, 
                                key.PropertyName, 
                                resource.Content,
                                BamlAttributeUsage.Default 
                                );
                        }

                        // add to the map 
                        treeMap.AddBamlTreeNode(null, key, newNode);
                    } 
                } 
            }
 
        }
        private static void CreateMissingBamlTreeNode(
            BamlLocalizationDictionary dictionary,
            BamlTreeUpdateMap treeMap
            )
        {
            BamlLocalizationDictionaryEnumerator enumerator = dictionary.GetEnumerator();

            while (enumerator.MoveNext())
            {
                BamlLocalizableResourceKey key      = enumerator.Key;
                BamlLocalizableResource    resource = enumerator.Value;

                // get the baml tree node from the tree
                BamlTreeNode node = treeMap.MapKeyToBamlTreeNode(key);

                if (node == null)
                {
                    if (key.PropertyName == BamlConst.ContentSuffix)
                    {
                        // see if there is already a Baml node with the Uid. If so
                        // ignore this entry
                        node = treeMap.MapUidToBamlTreeElementNode(key.Uid);
                        if (node == null)
                        {
                            // create new Baml element node
                            BamlStartElementNode newNode = new BamlStartElementNode(
                                treeMap.Resolver.ResolveAssemblyFromClass(key.ClassName),
                                key.ClassName,
                                false, /*isInjected*/
                                false  /*CreateUsingTypeConverter*/
                                );

                            // create new x:Uid node for this element node
                            newNode.AddChild(
                                new BamlDefAttributeNode(
                                    XamlReaderHelper.DefinitionUid,
                                    key.Uid
                                    )
                                );

                            TryAddContentPropertyToNewElement(treeMap, newNode);

                            // terminate the node with EndElementNode
                            newNode.AddChild(new BamlEndElementNode());

                            // store this new node into the map so that it can be found
                            // when other translations reference it as a childplace holder, or property owner
                            treeMap.AddBamlTreeNode(key.Uid, key, newNode);
                        }
                    }
                    else
                    {
                        BamlTreeNode newNode;
                        if (key.PropertyName == BamlConst.LiteralContentSuffix)
                        {
                            // create a LiterContent node
                            newNode = new BamlLiteralContentNode(resource.Content);
                        }
                        else
                        {
                            newNode = new BamlPropertyNode(
                                treeMap.Resolver.ResolveAssemblyFromClass(key.ClassName),
                                key.ClassName,
                                key.PropertyName,
                                resource.Content,
                                BamlAttributeUsage.Default
                                );
                        }

                        // add to the map
                        treeMap.AddBamlTreeNode(null, key, newNode);
                    }
                }
            }
        }
Пример #3
0
        private static bool ApplyChangeToBamlTree(
            BamlLocalizableResourceKey key, 
            BamlLocalizableResource    resource,
            BamlTreeUpdateMap          treeMap 
            ) 
        {
            if (   resource == null 
                || resource.Content == null
                || !resource.Modifiable)
            {
                // Invalid translation or the resource is marked as non-modifiable. 
                return true;
            } 
 
            if (   !treeMap.LocalizationDictionary.Contains(key)
                && !treeMap.IsNewBamlTreeNode(key)) 
            {
                // A localizable node is either in the localization dicationary extracted
                // from the source or it is a new node created by the localizer.
                // Otherwise, we cannot modify it. 
                return true;
            } 
 
            // get the node, at this point, all the missing nodes are created
            BamlTreeNode node = treeMap.MapKeyToBamlTreeNode(key); 
            Invariant.Assert(node != null);

            // apply translations
            switch (node.NodeType) 
            {
                case BamlNodeType.LiteralContent : 
                { 
                    BamlLiteralContentNode literalNode = (BamlLiteralContentNode) node;
 
                    // set the content to the node.
                    literalNode.Content = BamlResourceContentUtil.UnescapeString(resource.Content);

                    // now try to link this node into the parent. 
                    if (literalNode.Parent == null)
                    { 
                        BamlTreeNode parent = treeMap.MapUidToBamlTreeElementNode(key.Uid); 
                        if (parent != null)
                        { 
                            // link it up with the parent
                            parent.AddChild(literalNode);
                        }
                        else 
                        {
                            return false; // can't resolve the parent yet 
                        } 
                    }
                    break; 
                }
                case BamlNodeType.Property :
                {
                    BamlPropertyNode propertyNode = (BamlPropertyNode) node; 

                    // set the translation into the property 
                    propertyNode.Value = BamlResourceContentUtil.UnescapeString(resource.Content); 

                    // now try to link this node into the parent 
                    if (propertyNode.Parent == null)
                    {
                        BamlStartElementNode parent = (BamlStartElementNode) treeMap.MapUidToBamlTreeElementNode(key.Uid);
                        if (parent != null) 
                        {
                            // insert property node to the parent 
                            parent.InsertProperty(node); 
                        }
                        else 
                        {
                            return false;
                        }
                    } 
                    break;
                } 
                case BamlNodeType.StartElement : 
                {
                    string source = null; 
                    if (treeMap.LocalizationDictionary.Contains(key))
                    {
                        source = ((BamlLocalizableResource) treeMap.LocalizationDictionary[key]).Content;
                    } 

                    if (resource.Content != source) 
                    { 
                        // only rearrange the value if source and update are different
                        ReArrangeChildren(key, node, resource.Content, treeMap); 
                    }

                    break;
                } 
                default :
                    break; 
            } 

 
           return true;
        }
        private static bool ApplyChangeToBamlTree(
            BamlLocalizableResourceKey key,
            BamlLocalizableResource resource,
            BamlTreeUpdateMap treeMap
            )
        {
            if (resource == null ||
                resource.Content == null ||
                !resource.Modifiable)
            {
                // Invalid translation or the resource is marked as non-modifiable.
                return(true);
            }

            if (!treeMap.LocalizationDictionary.Contains(key) &&
                !treeMap.IsNewBamlTreeNode(key))
            {
                // A localizable node is either in the localization dicationary extracted
                // from the source or it is a new node created by the localizer.
                // Otherwise, we cannot modify it.
                return(true);
            }

            // get the node, at this point, all the missing nodes are created
            BamlTreeNode node = treeMap.MapKeyToBamlTreeNode(key);

            Invariant.Assert(node != null);

            // apply translations
            switch (node.NodeType)
            {
            case BamlNodeType.LiteralContent:
            {
                BamlLiteralContentNode literalNode = (BamlLiteralContentNode)node;

                // set the content to the node.
                literalNode.Content = BamlResourceContentUtil.UnescapeString(resource.Content);

                // now try to link this node into the parent.
                if (literalNode.Parent == null)
                {
                    BamlTreeNode parent = treeMap.MapUidToBamlTreeElementNode(key.Uid);
                    if (parent != null)
                    {
                        // link it up with the parent
                        parent.AddChild(literalNode);
                    }
                    else
                    {
                        return(false);    // can't resolve the parent yet
                    }
                }
                break;
            }

            case BamlNodeType.Property:
            {
                BamlPropertyNode propertyNode = (BamlPropertyNode)node;

                // set the translation into the property
                propertyNode.Value = BamlResourceContentUtil.UnescapeString(resource.Content);

                // now try to link this node into the parent
                if (propertyNode.Parent == null)
                {
                    BamlStartElementNode parent = (BamlStartElementNode)treeMap.MapUidToBamlTreeElementNode(key.Uid);
                    if (parent != null)
                    {
                        // insert property node to the parent
                        parent.InsertProperty(node);
                    }
                    else
                    {
                        return(false);
                    }
                }
                break;
            }

            case BamlNodeType.StartElement:
            {
                string source = null;
                if (treeMap.LocalizationDictionary.Contains(key))
                {
                    source = ((BamlLocalizableResource)treeMap.LocalizationDictionary[key]).Content;
                }

                if (resource.Content != source)
                {
                    // only rearrange the value if source and update are different
                    ReArrangeChildren(key, node, resource.Content, treeMap);
                }

                break;
            }

            default:
                break;
            }


            return(true);
        }