Esempio n. 1
0
        public bool TryGetKeyboard(string id, out IKeyboardDefinition keyboard)
        {
            if (string.IsNullOrEmpty(id))
            {
                keyboard = null;
                return(false);
            }

            KeyboardDescription kd;

            if (_keyboards.TryGet(id, out kd))
            {
                keyboard = kd;
                return(true);
            }

            string[] parts = id.Split('|');
            if (parts.Length == 2)
            {
                // This is the way Paratext stored IDs in 7.4-7.5 while there was a temporary bug-fix in place)
                keyboard = GetKeyboard(parts[0], parts[1]);
                if (keyboard != NullKeyboard)
                {
                    return(true);
                }

                keyboard = null;
                return(false);
            }

            // Handle old Palaso IDs
            parts = id.Split('-');
            if (parts.Length > 1)
            {
                for (int i = 1; i < parts.Length; i++)
                {
                    keyboard = GetKeyboard(string.Join("-", parts.Take(i)), string.Join("-", parts.Skip(i)));
                    if (keyboard != NullKeyboard)
                    {
                        return(true);
                    }
                }
            }

            keyboard = null;
            return(false);
        }
Esempio n. 2
0
        public override bool Create(string ietfLanguageTag, out WritingSystemDefinition ws)
        {
            if (_writingSystems.TryGet(ietfLanguageTag, out ws))
            {
                return(true);
            }

            ws = ConstructDefinition(ietfLanguageTag);
            return(true);
        }
Esempio n. 3
0
        internal static IKeyedCollection <string, SldrLanguageTagInfo> ParseAllTags(string allTagsContent)
        {
            string[] allTags = allTagsContent.Replace("\r\n", "\n").Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
            var      tags    = new KeyedList <string, SldrLanguageTagInfo>(info => info.LanguageTag, StringComparer.InvariantCultureIgnoreCase);

            foreach (string line in allTags)
            {
                string tagsStr = line;
                // trim off the explicit inheritance relationship information, we don't care about inheritance
                int index = line.LastIndexOf('>');
                if (index != -1)
                {
                    tagsStr = line.Substring(0, index).Trim();
                }
                // split the the line into groups of equivalent language tags
                // the bar character is used to show implicit inheritance relationships between tags,
                // we don't care about inheritance
                string[] equivalentTagsStrs = tagsStr.Split('|');
                foreach (string equivalentTagsStr in equivalentTagsStrs)
                {
                    // split each group of equivalent language tags into individual language tags
                    string[] tagStrs = equivalentTagsStr.Split('=');
                    for (int i = 0; i < tagStrs.Length; i++)
                    {
                        tagStrs[i] = tagStrs[i].Trim();
                    }
                    // check if language tag is available in the SLDR
                    bool isAvailable = tagStrs[0].StartsWith("*");
                    if (isAvailable)
                    {
                        tagStrs[0] = tagStrs[0].Substring(1);
                    }
                    string sldrLangTag = tagStrs[0];
                    // check if a tag with a script code is equivalent to a tag without a script code
                    // this tells us that the script is implicit
                    string langTag, implicitStringCode;
                    if (equivalentTagsStrs.Length == 1 && tagStrs.Length == 1)
                    {
                        // special case where there is a single tag on a line
                        // if it contains a script code, then the script is implicit
                        string[] components = tagStrs[0].Split('-');
                        if (components.Length == 2 && components[1].Length == 4)
                        {
                            langTag            = components[0];
                            implicitStringCode = components[1];
                        }
                        else
                        {
                            langTag            = tagStrs[0];
                            implicitStringCode = null;
                        }
                    }
                    else
                    {
                        var minTag = tagStrs.Select(t => new { Tag = t, Components = t.Split('-') }).MinBy(t => t.Components.Length);
                        langTag            = minTag.Tag;
                        implicitStringCode = null;
                        // only look for an implicit script code if the minimal tag has no script code
                        if (minTag.Components.Length < 2 || minTag.Components[1].Length != 4)
                        {
                            foreach (string tagStr in tagStrs)
                            {
                                string[] components = tagStr.Split('-');
                                if (components.Length == minTag.Components.Length + 1 && components[1].Length == 4)
                                {
                                    implicitStringCode = components[1];
                                }
                            }
                        }
                    }
                    SldrLanguageTagInfo existingTag;
                    if (tags.TryGet(langTag, out existingTag))
                    {
                        // alltags.txt can contain multiple lines that contain the same language tag.
                        // if one of the lines contains information on an implicit script for a language tag,
                        // we don't want to lose that information, so we preserve it by not replacing the
                        // SldrLanguageTagInfo object for this language tag.
                        if (existingTag.ImplicitScriptCode != null)
                        {
                            continue;
                        }
                        tags.Remove(langTag);
                    }
                    tags.Add(new SldrLanguageTagInfo(langTag, implicitStringCode, sldrLangTag, isAvailable));
                }
            }

            return(tags);
        }
Esempio n. 4
0
		internal static IKeyedCollection<string, SldrLanguageTagInfo> ParseAllTags(string allTagsContent)
		{
			string[] allTags = allTagsContent.Replace("\r\n", "\n").Split(new[] {"\n"}, StringSplitOptions.RemoveEmptyEntries);
			var tags = new KeyedList<string, SldrLanguageTagInfo>(info => info.LanguageTag, StringComparer.InvariantCultureIgnoreCase);
			foreach (string line in allTags)
			{
				string tagsStr = line;
				// trim off the explicit inheritance relationship information, we don't care about inheritance
				int index = line.LastIndexOf('>');
				if (index != -1)
					tagsStr = line.Substring(0, index).Trim();
				// split the the line into groups of equivalent language tags
				// the bar character is used to show implicit inheritance relationships between tags,
				// we don't care about inheritance
				string[] equivalentTagsStrs = tagsStr.Split('|');
				foreach (string equivalentTagsStr in equivalentTagsStrs)
				{
					// split each group of equivalent language tags into individual language tags
					string[] tagStrs = equivalentTagsStr.Split('=');
					for (int i = 0; i < tagStrs.Length; i++)
						tagStrs[i] = tagStrs[i].Trim();
					// check if language tag is available in the SLDR
					bool isAvailable = tagStrs[0].StartsWith("*");
					if (isAvailable)
						tagStrs[0] = tagStrs[0].Substring(1);
					string sldrLangTag = tagStrs[0];
					// check if a tag with a script code is equivalent to a tag without a script code
					// this tells us that the script is implicit
					string langTag, implicitStringCode;
					if (equivalentTagsStrs.Length == 1 && tagStrs.Length == 1)
					{
						// special case where there is a single tag on a line
						// if it contains a script code, then the script is implicit
						string[] components = tagStrs[0].Split('-');
						if (components.Length == 2 && components[1].Length == 4)
						{
							langTag = components[0];
							implicitStringCode = components[1];
						}
						else
						{
							langTag = tagStrs[0];
							implicitStringCode = null;
						}
					}
					else
					{
						var minTag = tagStrs.Select(t => new {Tag = t, Components = t.Split('-')}).MinBy(t => t.Components.Length);
						langTag = minTag.Tag;
						implicitStringCode = null;
						// only look for an implicit script code if the minimal tag has no script code
						if (minTag.Components.Length < 2 || minTag.Components[1].Length != 4)
						{
							foreach (string tagStr in tagStrs)
							{
								string[] components = tagStr.Split('-');
								if (components.Length == minTag.Components.Length + 1 && components[1].Length == 4)
									implicitStringCode = components[1];
							}
						}
					}
					SldrLanguageTagInfo existingTag;
					if (tags.TryGet(langTag, out existingTag))
					{
						// alltags.txt can contain multiple lines that contain the same language tag.
						// if one of the lines contains information on an implicit script for a language tag, 
						// we don't want to lose that information, so we preserve it by not replacing the
						// SldrLanguageTagInfo object for this language tag.
						if (existingTag.ImplicitScriptCode != null)
							continue;
						tags.Remove(langTag);
					}
					tags.Add(new SldrLanguageTagInfo(langTag, implicitStringCode, sldrLangTag, isAvailable));
				}
			}

			return tags;
		}