Beispiel #1
0
 // Token: 0x060024CD RID: 9421 RVA: 0x000B278A File Offset: 0x000B098A
 internal BamlLocalizableResource(BamlLocalizableResource other)
 {
     this._content  = other._content;
     this._comments = other._comments;
     this._flags    = other._flags;
     this._category = other._category;
 }
 /// <summary>
 /// constructor that creates a deep copy of the other localizable resource
 /// </summary>
 /// <param name="other"> the other localizale resource </param>
 internal BamlLocalizableResource(BamlLocalizableResource other)
 {
     Debug.Assert(other != null);
     _content  = other._content;
     _comments = other._comments;
     _flags    = other._flags;
     _category = other._category;
 }
 /// <summary>
 /// constructor that creates a deep copy of the other localizable resource
 /// </summary>
 /// <param name="other"> the other localizale resource </param>
 internal BamlLocalizableResource(BamlLocalizableResource other)
 {
     Debug.Assert(other != null);            
     _content        = other._content;
     _comments       = other._comments;
     _flags          = other._flags;            
     _category       = other._category;
 }
        // Token: 0x06002507 RID: 9479 RVA: 0x000B2D74 File Offset: 0x000B0F74
        internal BamlLocalizationDictionary Copy()
        {
            BamlLocalizationDictionary bamlLocalizationDictionary = new BamlLocalizationDictionary();

            foreach (KeyValuePair <BamlLocalizableResourceKey, BamlLocalizableResource> keyValuePair in this._dictionary)
            {
                BamlLocalizableResource value = (keyValuePair.Value == null) ? null : new BamlLocalizableResource(keyValuePair.Value);
                bamlLocalizationDictionary.Add(keyValuePair.Key, value);
            }
            bamlLocalizationDictionary._rootElementKey = this._rootElementKey;
            return(bamlLocalizationDictionary);
        }
        /// <summary>
        /// compare equality
        /// </summary>
        public override bool Equals(object other)
        {
            BamlLocalizableResource otherResource = other as BamlLocalizableResource;

            if (otherResource == null)
            {
                return(false);
            }

            return(_content == otherResource._content &&
                   _comments == otherResource._comments &&
                   _flags == otherResource._flags &&
                   _category == otherResource._category);
        }
        //------------------------------
        // internal functions
        //------------------------------
        internal BamlLocalizationDictionary Copy()
        {
            BamlLocalizationDictionary newDictionary = new BamlLocalizationDictionary();

            foreach (KeyValuePair <BamlLocalizableResourceKey, BamlLocalizableResource> pair in _dictionary)
            {
                BamlLocalizableResource resourceCopy =
                    pair.Value == null ?
                    null :
                    new BamlLocalizableResource(pair.Value);

                newDictionary.Add(pair.Key, resourceCopy);
            }

            newDictionary._rootElementKey = _rootElementKey;

            // return the new dictionary
            return(newDictionary);
        }
Beispiel #7
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;
        }
        /// <summary>
        /// build a localizable resource from a baml tree node
        /// </summary>        
        internal BamlLocalizableResource BuildFromNode(BamlLocalizableResourceKey key, BamlTreeNode node)
        {          
            if (node.Formatted)
            {
                // the content of the node has been formatted to be part of 
                // parents' content, so no need to create a seperate entry for the
                // element
                return null;
            }            
            
            BamlLocalizableResource resource = null;
            LocalizabilityAttribute localizability = null; 
            string                  formattingTag;

            // 
            // variable controling what comments gets applied
            //
            BamlStartElementNode    commentNode       = null;  // node containing comment
            string                  commentTargetName = null;  // the target of the comment, e.g. $Content, FontSize, etc.            

            //
            // step 1: Get the localizability attribute from the source files
            //        
            switch(node.NodeType)
            {                
                case BamlNodeType.StartElement:                                     
                {
                    // For element
                    commentNode = (BamlStartElementNode) node;
                    GetLocalizabilityForElementNode(commentNode, out localizability, out formattingTag);                    
                    commentTargetName = BamlConst.ContentSuffix;
                    break;
                }
                case BamlNodeType.LiteralContent:
                {
                    // For literal content, get the attribute from parent element
                    GetLocalizabilityForElementNode((BamlStartElementNode) node.Parent, out localizability, out formattingTag);

                    commentNode       = (BamlStartElementNode) node.Parent;
                    commentTargetName = BamlConst.ContentSuffix;
                    break;
                }

                case BamlNodeType.Property:
                {

                    BamlStartComplexPropertyNode propertyNode = (BamlStartComplexPropertyNode) node;
                    if (  LocComments.IsLocCommentsProperty(propertyNode.OwnerTypeFullName, propertyNode.PropertyName)
                       || LocComments.IsLocLocalizabilityProperty(propertyNode.OwnerTypeFullName, propertyNode.PropertyName)
                       )
                    {
                        // skip Localization.Comments and Localization.Attributes properties. They aren't localizable
                        return null;
                    }
                    
                    // For property                    
                    GetLocalizabilityForPropertyNode(propertyNode, out localizability);                                                                

                    commentTargetName = propertyNode.PropertyName;                    
                    commentNode       = (BamlStartElementNode) node.Parent;
                    break;
                }
                default:                        
                {                           
                    Invariant.Assert(false); // no localizable resource for such node
                    break;
                }
            }

            //
            // Step 2: Find out the inheritance value
            //

            // The node participates in localizability inheritance
            // let's fill things in
            localizability = CombineAndPropagateInheritanceValues(
                node as ILocalizabilityInheritable, 
                localizability
                );                

            //
            // Step 3: We finalized the localizability values. We now apply.
            //            
            string content = null;     
            
            if (localizability.Category != LocalizationCategory.NeverLocalize 
             && localizability.Category != LocalizationCategory.Ignore 
             && TryGetContent(key, node, out content))                
            {   
                // we only create one if it is localizable
                resource                = new BamlLocalizableResource();
                resource.Readable       = (localizability.Readability   == Readability.Readable);               
                resource.Modifiable     = (localizability.Modifiability == Modifiability.Modifiable);
                resource.Category       = localizability.Category;
                // continue to fill in content.
                resource.Content        = content;
                resource.Comments       = _resolver.GetStringComment(commentNode, commentTargetName);
            }
            
            // return the resource
            return resource;  
        }
Beispiel #9
0
        /// <summary>Determines whether a specified <see cref="T:System.Windows.Markup.Localizer.BamlLocalizableResource" /> object is equal to this object.</summary>
        /// <param name="other">The <see cref="T:System.Windows.Markup.Localizer.BamlLocalizableResource" /> object test for equality.</param>
        /// <returns>
        ///     <see langword="true" /> if <paramref name="other" /> is equal to this object; otherwise, <see langword="false" />.</returns>
        // Token: 0x060024D8 RID: 9432 RVA: 0x000B285C File Offset: 0x000B0A5C
        public override bool Equals(object other)
        {
            BamlLocalizableResource bamlLocalizableResource = other as BamlLocalizableResource;

            return(bamlLocalizableResource != null && (this._content == bamlLocalizableResource._content && this._comments == bamlLocalizableResource._comments && this._flags == bamlLocalizableResource._flags) && this._category == bamlLocalizableResource._category);
        }
 /// <summary>
 /// Adds a localizable resource with the provided key
 /// </summary>
 /// <param name="key">the BamlLocalizableResourceKey key</param>
 /// <param name="value">the BamlLocalizableResource</param>
 public void Add(BamlLocalizableResourceKey key, BamlLocalizableResource value)
 {
     CheckNonNullParam(key, "key");
     _dictionary.Add(key, value);
 }
 /// <summary>
 /// Adds a localizable resource with the provided key
 /// </summary>
 /// <param name="key">the BamlLocalizableResourceKey key</param>
 /// <param name="value">the BamlLocalizableResource</param>
 public void Add(BamlLocalizableResourceKey key, BamlLocalizableResource value)
 {
     CheckNonNullParam(key, "key");
     _dictionary.Add(key, value);
 }
 public void Add(BamlLocalizableResourceKey key, BamlLocalizableResource value)
 {
 }
 public void Add(BamlLocalizableResourceKey key, BamlLocalizableResource value)
 {
 }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="reader">resoure text reader that reads CSV or a tab-separated txt file</param>
        internal TranslationDictionariesReader(ResourceTextReader reader)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");

            // hash key is case insensitive strings
            _table = new Hashtable();

            // we read each Row
            int rowNumber = 0;
            while (reader.ReadRow())
            {
                rowNumber ++;

                // field #1 is the baml name.
                string bamlName = reader.GetColumn(0);

                // it can't be null
                if (bamlName == null)
                    throw new ApplicationException(StringLoader.Get("EmptyRowEncountered"));

                if (string.IsNullOrEmpty(bamlName))
                {
                    // allow for comment lines in csv file.
                    // each comment line starts with ",". It will make the first entry as String.Empty.
                    // and we will skip the whole line.
                    continue;   // if the first column is empty, take it as a comment line
                }

                // field #2: key to the localizable resource
                string key = reader.GetColumn(1);
                if (key == null)
                    throw new ApplicationException(StringLoader.Get("NullBamlKeyNameInRow"));

                BamlLocalizableResourceKey resourceKey = LocBamlConst.StringToResourceKey(key);

                // get the dictionary
                BamlLocalizationDictionary dictionary = this[bamlName];
                if (dictionary == null)
                {
                    // we create one if it is not there yet.
                    dictionary = new BamlLocalizationDictionary();
                    this[bamlName] = dictionary;
                }

                BamlLocalizableResource resource;

                // the rest of the fields are either all null,
                // or all non-null. If all null, it means the resource entry is deleted.

                // get the string category
                string categoryString = reader.GetColumn(2);
                if (categoryString == null)
                {
                    // it means all the following fields are null starting from column #3.
                    resource = null;
                }
                else
                {
                    // the rest must all be non-null.
                    // the last cell can be null if there is no content
                    for (int i = 3; i < 6; i++)
                    {
                        if (reader.GetColumn(i) == null)
                            throw new Exception(StringLoader.Get("InvalidRow"));
                    }

                    // now we know all are non-null. let's try to create a resource
                    resource  = new BamlLocalizableResource();

                    // field #3: Category
                    resource.Category = (LocalizationCategory) StringCatConverter.ConvertFrom(categoryString);

                    // field #4: Readable
                    resource.Readable     = (bool) BoolTypeConverter.ConvertFrom(reader.GetColumn(3));

                    // field #5: Modifiable
                    resource.Modifiable   = (bool) BoolTypeConverter.ConvertFrom(reader.GetColumn(4));

                    // field #6: Comments
                    resource.Comments     = reader.GetColumn(5);

                    // field #7: Content
                    resource.Content      = reader.GetColumn(6);

                    // in case content being the last column, consider null as empty.
                    if (resource.Content == null)
                        resource.Content = string.Empty;

                    // field > #7: Ignored.
                }

                // at this point, we are good.
                // add to the dictionary.
                dictionary.Add(resourceKey, resource);
            }
        }