コード例 #1
0
        /// <summary>
        /// Add a localization file.
        /// </summary>
        /// <param name="localization">The localization file to add.</param>
        public void AddLocalization(Localization localization)
        {
            if (-1 == this.codepage)
            {
                this.codepage = localization.Codepage;
            }
            else if (-1 != this.codepage && -1 != localization.Codepage && this.codepage != localization.Codepage)
            {
                this.OnMessage(WixErrors.DuplicateLocalizedCodepage(null, this.codepage));
            }

            foreach (WixVariableRow wixVariableRow in localization.Variables)
            {
                WixVariableRow existingWixVariableRow = (WixVariableRow)this.variables[wixVariableRow.Id];

                if (null == existingWixVariableRow || (existingWixVariableRow.Overridable && !wixVariableRow.Overridable))
                {
                    this.variables.Add(wixVariableRow.Id, wixVariableRow);
                }
                else if (!wixVariableRow.Overridable)
                {
                    this.OnMessage(WixErrors.DuplicateLocalizationIdentifier(wixVariableRow.SourceLineNumbers, wixVariableRow.Id));
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Load localized strings from a wixloc file.
        /// </summary>
        /// <param name="path">Path to the wixloc file.</param>
        public void LoadFromFile(string path)
        {
            SourceLineNumberCollection sourceLineNumbers = SourceLineNumberCollection.FromFileName(path);

            try
            {
                XPathDocument  doc = new XPathDocument(path);
                XPathNavigator nav = doc.CreateNavigator();

                nav.MoveToRoot();
                if (nav.MoveToFirstChild())
                {
                    // move to the first element (skipping comments and stuff)
                    while (XPathNodeType.Element != nav.NodeType)
                    {
                        nav.MoveToNext();
                    }

                    if ("WixLocalization" != nav.LocalName)
                    {
                        this.OnMessage(WixErrors.InvalidDocumentElement(sourceLineNumbers, nav.Name, "localization", "WixLocalization"));
                    }

                    if (nav.MoveToAttribute("Codepage", String.Empty))
                    {
                        try
                        {
                            int newCodepage = Convert.ToInt32(nav.Value, CultureInfo.InvariantCulture.NumberFormat);

                            // if the codepage has not been set
                            if (-1 == this.codepage)
                            {
                                this.codepage = newCodepage;
                            }
                            else if (newCodepage != this.codepage)                             // fail if codepage has already been specified but is different
                            {
                                this.OnMessage(WixErrors.DuplicateLocalizedCodepage(sourceLineNumbers, nav.Value));
                            }
                        }
                        catch (FormatException)
                        {
                            this.OnMessage(WixErrors.IllegalIntegerValue(null, "WixLocalization", nav.Name, nav.Value));
                        }
                        catch (OverflowException)
                        {
                            this.OnMessage(WixErrors.IllegalIntegerValue(null, "WixLocalization", nav.Name, nav.Value));
                        }

                        nav.MoveToParent();
                    }

                    if (nav.MoveToFirstChild())
                    {
                        do
                        {
                            if (XPathNodeType.Element == nav.NodeType)
                            {
                                string localizationId    = null;
                                string localizationValue = String.Empty;

                                if ("String" != nav.LocalName)
                                {
                                    continue;
                                }

                                if (nav.MoveToAttribute("Id", String.Empty))
                                {
                                    localizationId = nav.Value;
                                    nav.MoveToParent();
                                }

                                if (null == localizationId)
                                {
                                    this.OnMessage(WixErrors.ExpectedAttribute(sourceLineNumbers, "String", "Id"));
                                }

                                if (nav.MoveToFirstChild())
                                {
                                    // find the text() of the element
                                    do
                                    {
                                        if (XPathNodeType.Text == nav.NodeType)
                                        {
                                            localizationValue = nav.Value;
                                            break;                                             // found the text() of the element and bail
                                        }
                                    }while (nav.MoveToNext());

                                    nav.MoveToParent();                                     // back up
                                }

                                if (!this.stringDictionary.ContainsKey(localizationId))
                                {
                                    this.stringDictionary.Add(localizationId, localizationValue);
                                }
                                else                                 // duplicate localization identifier
                                {
                                    this.OnMessage(WixErrors.DuplicateLocalizationIdentifier(sourceLineNumbers, localizationId));
                                }
                            }
                        }while (nav.MoveToNext());
                    }
                }
            }
            catch (DirectoryNotFoundException)
            {
                this.OnMessage(WixErrors.FileNotFound(path));
            }
            catch (FileNotFoundException)
            {
                this.OnMessage(WixErrors.FileNotFound(path));
            }
            catch (XmlException e)
            {
                this.OnMessage(WixErrors.InvalidXml(sourceLineNumbers, "localization", e.Message));
            }
        }