Ejemplo n.º 1
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Finds or creates the writing system.
        /// </summary>
        /// <param name="cache">The cache.</param>
        /// <param name="icuLocale">The icu locale.</param>
        /// <returns>The writing system</returns>
        /// ------------------------------------------------------------------------------------
        private LgWritingSystem FindOrCreateWs(FdoCache cache, string icuLocale)
        {
            // Look to see if the writing system already exists in the database
            foreach (LgWritingSystem ws in cache.LanguageEncodings)
            {
                if (LanguageDefinition.SameLocale(ws.ICULocale, icuLocale))
                {
                    return(ws);
                }
            }

            // Create a new writing system based on the one noted in the XML file.
            // Load it in from the XML and save into the database.
            LanguageDefinitionFactory ldf =
                new LanguageDefinitionFactory(cache.LanguageWritingSystemFactoryAccessor, icuLocale);

            ldf.LanguageDefinition.SaveWritingSystem(icuLocale);
            cache.ResetLanguageEncodings();
            // search again. It better be there now!
            foreach (LgWritingSystem ws in cache.LanguageEncodings)
            {
                if (LanguageDefinition.SameLocale(ws.ICULocale, icuLocale))
                {
                    return(ws);
                }
            }
            Debug.Assert(false);
            return(null);
        }
Ejemplo n.º 2
0
        public void PopulateFromLanguageClass()
        {
            //Extracts the locale filename from a given path
            int    icuName    = m_inputFilename.LastIndexOf("\\");
            string icuPortion = m_inputFilename.Substring(icuName + 1);

            //Appears this maps the XML file to a LanguageDefinition class
            /////////////////
            ILgWritingSystemFactory wsf = LgWritingSystemFactoryClass.Create();

            LanguageDefinitionFactory langDefFactory = new LanguageDefinitionFactory(wsf, icuPortion);

            LanguageDefinition langDef = langDefFactory.InitializeFromXml(wsf, icuPortion) as LanguageDefinition;

            if (langDef == null)
            {
                throw new Exception("Unable to read and parse the input XML file " + m_inputFilename);
            }
            /////////////////

            int i    = 0;
            int cpua = langDef.PuaDefinitionCount;

            // if we have PUA characters in the LD file make an array of PUACharacters.  But be careful
            // to handle invalid definitions gracefully.
            if (langDef.PuaDefinitions != null && cpua != 0)
            {
                puaChars = new PUACharacter[cpua];
                foreach (CharDef charDef in langDef.PuaDefinitions)
                {
                    try
                    {
                        puaChars[i] = new PUACharacter(charDef);
                        ++i;
                    }
                    catch
                    {
                    }
                }
            }
            if (i < cpua)
            {
                if (i == 0)
                {
                    puaChars = null;
                }
                else
                {
                    PUACharacter[] puaGoodChars = new PUACharacter[i];
                    for (int ic = 0; ic < i; ++ic)
                    {
                        puaGoodChars[ic] = puaChars[ic];
                    }
                    puaChars = puaGoodChars;
                }
                if (LogFile.IsLogging())
                {
                    LogFile.AddErrorLine("Warning, " + (cpua - i) + " out of " + cpua +
                                         " PUA character definitions are invalid.");
                }
            }
            baseLocale      = langDef.BaseLocale;
            newLocale       = langDef.XmlWritingSystem.WritingSystem.IcuLocale;
            localeResources = langDef.LocaleResources;
            // Get the collation elements, whether from the CollationElements element directly,
            // or from the WritingSystem element.
            collationElements = langDef.CollationElements;
            if (collationElements == null)
            {
                IWritingSystem lws   = langDef.WritingSystem;
                int            ccoll = lws.CollationCount;
                if (ccoll > 0)
                {
                    collationElements = lws.get_Collation(0).IcuRules;
                }
            }
            localeWinLCID = langDef.XmlWritingSystem.WritingSystem.Locale.ToString();

            // make sure the newlocale has the proper case for each property:
            // lang, country and variant
            InstallLanguage.LocaleParser lp = new LocaleParser(newLocale);
            newLocale = lp.Locale;

            // Make sure the display names [Name, Country & Variant] have Unicode characters
            // greater than 7F converted to the \uxxxx format where xxxx is the unicode
            // hex value of the character.
            localeName    = ConvertToUnicodeNotation(langDef.LocaleName);
            localeScript  = ConvertToUnicodeNotation(langDef.LocaleScript);
            localeCountry = ConvertToUnicodeNotation(langDef.LocaleCountry);
            localeVariant = ConvertToUnicodeNotation(langDef.LocaleVariant);

            // Save the multilingual names of the writing system, together with the
            // ICU locale for each name.
            NameMultiUnicode rgName = langDef.XmlWritingSystem.Name;
            int cws = rgName.Count;

            // If we don't have a name, use the IcuLocale rather than going without a name.
            // Otherwise it won't register as a language in en.txt/res.
            if (cws == 0)
            {
                StringWithWs sw = new StringWithWs(langDef.XmlWritingSystem.WritingSystem.IcuLocale, "en");
                rgName.Add(sw);
                cws = 1;
            }
            m_rgNames = new System.Collections.ArrayList(cws);
            for (int iws = 0; iws < cws; ++iws)
            {
                StringWithWs x = rgName[iws];
                m_rgNames.Add(x);
            }

            // TODO - dlh
            // Once collationElements are handled, something will have to be checked there
            // as the current implementation assumes that it's in the valid format.

            wsf.Shutdown();                     // This is (always) needed to balance creating the factory.
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get the writing system code (int) for the given RFC4646 language code
        /// (string).
        /// </summary>
        public int GetWsFromRfcLang(string code, string sLdmlDir)
        {
            int ws;

            if (m_mapRFCtoWs.TryGetValue(code, out ws))
            {
                return(ws);
            }
            string sWs      = ConvertFromRFCtoICU(code);
            string sWsLower = sWs.ToLowerInvariant();

            if (!m_mapIcuLCToWs.TryGetValue(sWsLower, out ws))
            {
                // See if a compatible XML file exists defining this writing system.
                LanguageDefinition langDef;
                try
                {
                    LanguageDefinitionFactory fact = new LanguageDefinitionFactory();
                    langDef = fact.InitializeFromXml(
                        m_cache.LanguageWritingSystemFactoryAccessor, sWs) as LanguageDefinition;
                }
                catch
                {
                    langDef = null;
                }
                ILgWritingSystem lgws;
                if (langDef != null)
                {
                    // ICU locale case may differ - keep existing XML based value
                    string sICU = langDef.IcuLocaleOriginal;
                    Debug.Assert(sWsLower == sICU.ToLowerInvariant());
                    langDef.SaveWritingSystem(sICU, true);
                    ws = m_cache.LanguageWritingSystemFactoryAccessor.GetWsFromStr(sICU);
                    Debug.Assert(ws >= 1);
                    lgws = LgWritingSystem.CreateFromDBObject(m_cache, ws);
                }
                else
                {
                    WritingSystemDefinition wsd = null;
                    if (!String.IsNullOrEmpty(sLdmlDir))
                    {
                        LdmlInFolderWritingSystemStore ldmlstore = new LdmlInFolderWritingSystemStore(sLdmlDir);
                        foreach (WritingSystemDefinition wsdT in ldmlstore.WritingSystemDefinitions)
                        {
                            if (wsdT.RFC4646 == code)
                            {
                                wsd = wsdT;
                                break;
                            }
                        }
                    }
                    // This creates a new writing system for the given key.
                    IWritingSystem wrsy = m_cache.LanguageWritingSystemFactoryAccessor.get_Engine(sWs);
                    m_cache.ResetLanguageEncodings();
                    ws = wrsy.WritingSystem;
                    Debug.Assert(ws >= 1);
                    lgws           = LgWritingSystem.CreateFromDBObject(m_cache, ws);
                    lgws.ICULocale = sWs;
                    if (wsd == null)
                    {
                        lgws.Abbr.UserDefaultWritingSystem = sWs;
                        lgws.Name.UserDefaultWritingSystem = sWs;
                    }
                    else
                    {
                        lgws.Abbr.UserDefaultWritingSystem = wsd.Abbreviation;
                        lgws.Name.UserDefaultWritingSystem = wsd.LanguageName;
                        lgws.DefaultSerif    = wsd.DefaultFontName;
                        lgws.DefaultBodyFont = wsd.DefaultFontName;
                        lgws.RightToLeft     = wsd.RightToLeftScript;
                        // TODO: collation, keyboard.
                    }
                    // Make sure XML file is written.  See LT-8743.
                    wrsy.SaveIfDirty(m_cache.DatabaseAccessor);
                }
                m_rgnewWrtSys.Add(lgws);
                m_cache.LangProject.AnalysisWssRC.Add(ws);
                m_cache.LangProject.CurAnalysisWssRS.Append(ws);
                if (m_fUpdateVernWss)
                {
                    m_cache.LangProject.VernWssRC.Add(ws);
                    m_cache.LangProject.CurVernWssRS.Append(ws);
                }
                m_mapIcuLCToWs.Add(sWsLower, ws);
            }
            m_mapRFCtoWs.Add(code, ws);
            return(ws);
        }