private void ExportWsAsLDML(string sDirectory) { LdmlInFolderWritingSystemStore ldmlstore = new LdmlInFolderWritingSystemStore(sDirectory); ldmlstore.DontAddDefaultDefinitions = true; Set <int> rgwsWritten = new Set <int>(); foreach (LgWritingSystem lgws in m_cache.LangProject.VernWssRC) { ExportLDML(lgws, ldmlstore); rgwsWritten.Add(lgws.Hvo); } foreach (LgWritingSystem lgws in m_cache.LangProject.AnalysisWssRC) { if (!rgwsWritten.Contains(lgws.Hvo)) { ExportLDML(lgws, ldmlstore); rgwsWritten.Add(lgws.Hvo); } } foreach (NamedWritingSystem nws in m_cache.LangProject.GetPronunciationWritingSystems()) { if (nws.Hvo > 0 && !rgwsWritten.Contains(nws.Hvo)) { LgWritingSystem lgws = LgWritingSystem.CreateFromDBObject(m_cache, nws.Hvo) as LgWritingSystem; ExportLDML(lgws, ldmlstore); rgwsWritten.Add(nws.Hvo); } } foreach (ReversalIndex ri in m_cache.LangProject.LexDbOA.ReversalIndexesOC) { if (ri.WritingSystemRAHvo > 0 && !rgwsWritten.Contains(ri.WritingSystemRAHvo)) { ExportLDML(ri.WritingSystemRA as LgWritingSystem, ldmlstore); rgwsWritten.Add(ri.WritingSystemRAHvo); } } }
private void ExportLDML(LgWritingSystem lgws, LdmlInFolderWritingSystemStore ldmlstore) { foreach (WritingSystemDefinition wsdT in ldmlstore.WritingSystemDefinitions) { // Don't bother changing an existing LDML file. if (wsdT.RFC4646 == lgws.RFC4646bis) return; } string sICU = lgws.ICULocale; string sLang; string sScript; string sCountry; string sVariant; Icu.UErrorCode err = Icu.UErrorCode.U_ZERO_ERROR; Icu.GetLanguageCode(sICU, out sLang, out err); if (sLang.Length > 3 && sLang.StartsWith("x")) sLang = sLang.Insert(1, "-"); Icu.GetScriptCode(sICU, out sScript, out err); Icu.GetCountryCode(sICU, out sCountry, out err); Icu.GetVariantCode(sICU, out sVariant, out err); if (sVariant == "IPA") sVariant = "fonipa"; Debug.Assert(err == Icu.UErrorCode.U_ZERO_ERROR); string sKeyboard; if (String.IsNullOrEmpty(lgws.KeymanKeyboard)) sKeyboard = GetKeyboardName(lgws.Locale); else sKeyboard = lgws.KeymanKeyboard; string sSortUsing = null; string sSortRules = null; if (lgws.CollationsOS.Count > 0) { try { ILgCollation coll = lgws.CollationsOS[0]; string sResName = coll.IcuResourceName; string sRules = coll.ICURules; int lcid = coll.WinLCID; if (!String.IsNullOrEmpty(sRules)) { sSortUsing = "CustomICU"; sSortRules = sRules; } else if (!String.IsNullOrEmpty(sResName)) { sSortUsing = "OtherLanguage"; sSortRules = sResName; } else if (lcid != 0) { sSortUsing = "OtherLanguage"; System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(lcid); sSortRules = ci.Name; } } catch { // This try-catch shouldn't be needed, but as LT-9545 points out, creating // the collation can crash for some non-repeatable reason. It's happened // twice on my machine, and once each for a couple of testers. } } WritingSystemDefinition wsd = new WritingSystemDefinition(sLang); wsd.Script = sScript; wsd.Region = sCountry; wsd.Variant = sVariant; wsd.LanguageName = lgws.Name.UserDefaultWritingSystem; wsd.Abbreviation = lgws.Abbr.UserDefaultWritingSystem; wsd.RightToLeftScript = lgws.RightToLeft; wsd.DefaultFontName = lgws.DefaultSerif; wsd.DefaultFontSize = 12; // pure guesswork - we need a stylesheet or a model change! wsd.Keyboard = sKeyboard; if (!String.IsNullOrEmpty(sSortUsing)) { wsd.SortUsing = sSortUsing; wsd.SortRules = sSortRules; } //wsd.NativeName = null; //wsd.SpellCheckingId = null; //wsd.StoreID = null; //wsd.VersionDescription = null; //wsd.VersionNumber = null; try { ldmlstore.SaveDefinition(wsd); } catch (Exception ex) { Debug.WriteLine(String.Format("Error writing LDML file: lgws.RFC={0}, wsd.RFC={1}; error: {2}", lgws.RFC4646bis, wsd.RFC4646, ex.Message)); } }
private void ExportWsAsLDML(string sDirectory) { LdmlInFolderWritingSystemStore ldmlstore = new LdmlInFolderWritingSystemStore(sDirectory); ldmlstore.DontAddDefaultDefinitions = true; Set<int> rgwsWritten = new Set<int>(); foreach (LgWritingSystem lgws in m_cache.LangProject.VernWssRC) { ExportLDML(lgws, ldmlstore); rgwsWritten.Add(lgws.Hvo); } foreach (LgWritingSystem lgws in m_cache.LangProject.AnalysisWssRC) { if (!rgwsWritten.Contains(lgws.Hvo)) { ExportLDML(lgws, ldmlstore); rgwsWritten.Add(lgws.Hvo); } } foreach (NamedWritingSystem nws in m_cache.LangProject.GetPronunciationWritingSystems()) { if (nws.Hvo > 0 && !rgwsWritten.Contains(nws.Hvo)) { LgWritingSystem lgws = LgWritingSystem.CreateFromDBObject(m_cache, nws.Hvo) as LgWritingSystem; ExportLDML(lgws, ldmlstore); rgwsWritten.Add(nws.Hvo); } } foreach (ReversalIndex ri in m_cache.LangProject.LexDbOA.ReversalIndexesOC) { if (ri.WritingSystemRAHvo > 0 && !rgwsWritten.Contains(ri.WritingSystemRAHvo)) { ExportLDML(ri.WritingSystemRA as LgWritingSystem, ldmlstore); rgwsWritten.Add(ri.WritingSystemRAHvo); } } }
/// <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; }
private void ExportLDML(LgWritingSystem lgws, LdmlInFolderWritingSystemStore ldmlstore) { foreach (WritingSystemDefinition wsdT in ldmlstore.WritingSystemDefinitions) { // Don't bother changing an existing LDML file. if (wsdT.RFC4646 == lgws.RFC4646bis) { return; } } string sICU = lgws.ICULocale; string sLang; string sScript; string sCountry; string sVariant; Icu.UErrorCode err = Icu.UErrorCode.U_ZERO_ERROR; Icu.GetLanguageCode(sICU, out sLang, out err); if (sLang.Length > 3 && sLang.StartsWith("x")) { sLang = sLang.Insert(1, "-"); } Icu.GetScriptCode(sICU, out sScript, out err); Icu.GetCountryCode(sICU, out sCountry, out err); Icu.GetVariantCode(sICU, out sVariant, out err); if (sVariant == "IPA") { sVariant = "fonipa"; } Debug.Assert(err == Icu.UErrorCode.U_ZERO_ERROR); string sKeyboard; if (String.IsNullOrEmpty(lgws.KeymanKeyboard)) { sKeyboard = GetKeyboardName(lgws.Locale); } else { sKeyboard = lgws.KeymanKeyboard; } string sSortUsing = null; string sSortRules = null; if (lgws.CollationsOS.Count > 0) { try { ILgCollation coll = lgws.CollationsOS[0]; string sResName = coll.IcuResourceName; string sRules = coll.ICURules; int lcid = coll.WinLCID; if (!String.IsNullOrEmpty(sRules)) { sSortUsing = "CustomICU"; sSortRules = sRules; } else if (!String.IsNullOrEmpty(sResName)) { sSortUsing = "OtherLanguage"; sSortRules = sResName; } else if (lcid != 0) { sSortUsing = "OtherLanguage"; System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(lcid); sSortRules = ci.Name; } } catch { // This try-catch shouldn't be needed, but as LT-9545 points out, creating // the collation can crash for some non-repeatable reason. It's happened // twice on my machine, and once each for a couple of testers. } } WritingSystemDefinition wsd = new WritingSystemDefinition(sLang); wsd.Script = sScript; wsd.Region = sCountry; wsd.Variant = sVariant; wsd.LanguageName = lgws.Name.UserDefaultWritingSystem; wsd.Abbreviation = lgws.Abbr.UserDefaultWritingSystem; wsd.RightToLeftScript = lgws.RightToLeft; wsd.DefaultFontName = lgws.DefaultSerif; wsd.DefaultFontSize = 12; // pure guesswork - we need a stylesheet or a model change! wsd.Keyboard = sKeyboard; if (!String.IsNullOrEmpty(sSortUsing)) { wsd.SortUsing = sSortUsing; wsd.SortRules = sSortRules; } //wsd.NativeName = null; //wsd.SpellCheckingId = null; //wsd.StoreID = null; //wsd.VersionDescription = null; //wsd.VersionNumber = null; try { ldmlstore.SaveDefinition(wsd); } catch (Exception ex) { Debug.WriteLine(String.Format("Error writing LDML file: lgws.RFC={0}, wsd.RFC={1}; error: {2}", lgws.RFC4646bis, wsd.RFC4646, ex.Message)); } }
/// <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); }