SIL.CoreImpl.PalasoWritingSystemManager.GetValidLangTagForNewLang C# (CSharp) Method

GetValidLangTagForNewLang() public method

Returns an ISO 639 language tag that is guaranteed to be valid and unique for both the local and the global writing system store. NOTE: This method should only be used for writing systems that are custom (i.e. not defined in the current version of the ethnologue). The returned code will *not* have the 'x-' prefix denoting a user-defined writing system, but it will check that an existing user-defined writing system does not exist with the returned language tag. This method also does not worry about regions, variants, etc. as it's use is restricted to the language tag for a custom writing system.
public GetValidLangTagForNewLang ( string langName ) : string
langName string The full name of the language.
return string
		public string GetValidLangTagForNewLang(string langName)
		{
			string nameD = langName.Normalize(NormalizationForm.FormD); // Get the name in NFD format
			StringBuilder builder = new StringBuilder(nameD.ToLowerInvariant());
			int index = 0;
			while (index < builder.Length)
			{
				char c = builder[index];
				bool charValid = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
				if (!charValid)
				{
					// Found an invalid character, so remove it.
					builder.Remove(index, 1);
					continue;
				}
				index++;
			}

			string isoCode = builder.ToString().Substring(0, Math.Min(3, builder.Length));
			if (LangTagUtils.IsLanguageCodeValid(isoCode) && !LangTagInUse("qaa-x-" + isoCode))
				return isoCode; // The generated code is valid and not in use by the local or global store

			// We failed to generate a valid, unused language tag from the language name so
			// find one that isn't taken starting with 'aaa' and incrementing ('aab', 'aac', etc.)
			builder.Remove(0, builder.Length); // Clear the builder
			builder.Append("aaa");
			while (LangTagInUse("qaa-x-" + builder))
			{
				char newCharLast = (char)(builder[2] + 1);
				if (newCharLast > 'z')
				{
					// Incremented the last letter too far so reset it back to 'a' and increment the middle letter
					newCharLast = 'a';
					char newCharMiddle = (char)(builder[1] + 1);
					if (newCharMiddle > 'z')
					{
						// Incremented the middle letter too far so reset it back to 'a' and increment the first letter
						// Assume we won't ever have more then 4096 (26^3) custom writing systems
						newCharMiddle = 'a';
						builder[0] = (char)(builder[0] + 1);
					}
					builder[1] = newCharMiddle;
				}
				builder[2] = newCharLast;
			}
			return builder.ToString();
		}

Usage Example

        public void GetValidLangCodeForNewLang()
        {
            string storePath       = PrepareTempStore("Store");
            string globalStorePath = PrepareTempStore("GlobalStore");

            EnsureDirectoryIsEmpty(storePath);
            EnsureDirectoryIsEmpty(globalStorePath);

            var globalStore = new GlobalFileWritingSystemStore(globalStorePath);
            var wsManager   = new PalasoWritingSystemManager(
                new LocalFileWritingSystemStore(storePath, globalStore), globalStore);

            Assert.AreEqual("qip", wsManager.GetValidLangTagForNewLang("Qipkey"));
            Assert.AreEqual("sn", wsManager.GetValidLangTagForNewLang("Sn"));
            Assert.AreEqual("eba", wsManager.GetValidLangTagForNewLang("\u00CBbashlish"));             // \u00CB == E with diacritic
            Assert.AreEqual("eee", wsManager.GetValidLangTagForNewLang("\u00CB\u00CB\u00CBlish"));
            // \u00CB == E with diacritic
            Assert.AreEqual("aaa", wsManager.GetValidLangTagForNewLang("U"));

            LanguageSubtag subtag = new LanguageSubtag("qip", "Qipkey", true, null);
            IWritingSystem newWs  = wsManager.Create(subtag, null, null, null);

            wsManager.Set(newWs);
            Assert.AreEqual("aaa", wsManager.GetValidLangTagForNewLang("Qipsing"), "code for 'qip' should already be taken");

            subtag = new LanguageSubtag("aaa", "Qipsing", true, null);
            newWs  = wsManager.Create(subtag, null, null, null);
            wsManager.Set(newWs);
            Assert.AreEqual("aab", wsManager.GetValidLangTagForNewLang("Qipwest"),
                            "code for 'qip' should already be taken twice");

            // ENHANCE: Ideally, we would want to test incrementing the middle and first character,
            // but that would require at least 677 (26^2 + 1) writing systems be created.
        }
All Usage Examples Of SIL.CoreImpl.PalasoWritingSystemManager::GetValidLangTagForNewLang