Beispiel #1
0
 /// <summary/>
 public bool ValidateScriptCode(string scriptCode)
 {
     return(IetfLanguageTag.IsValidScriptCode(scriptCode));
 }
Beispiel #2
0
 public void IsValidScriptCode_InvalidScriptCode_ReturnsFalse()
 {
     Assert.That(IetfLanguageTag.IsValidScriptCode("abc"), Is.False);
     Assert.That(IetfLanguageTag.IsValidScriptCode("abc1"), Is.False);
     Assert.That(IetfLanguageTag.IsValidScriptCode("abcde"), Is.False);
 }
Beispiel #3
0
 public void IsValidScriptCode_ValidScriptCode_ReturnsTrue()
 {
     Assert.That(IetfLanguageTag.IsValidScriptCode("abcd"), Is.True);
     Assert.That(IetfLanguageTag.IsValidScriptCode("ABCD"), Is.True);
 }
Beispiel #4
0
        /// <summary>
        /// Check that the contents of the control are valid. If not, report the error
        /// to the user and return false. This should prevent the user from closing the
        /// containing form using OK, but not from cancelling.
        /// </summary>
        public bool CheckValid()
        {
            CheckDisposed();

            string caption = FwCoreDlgControls.kstidError;

            ScriptSubtag scriptSubtag = ScriptSubtag;

            // Can't allow a script name without an abbreviation.
            if (scriptSubtag == null && !string.IsNullOrEmpty(m_scriptName.Text.Trim()))
            {
                MessageBox.Show(FindForm(), FwCoreDlgControls.kstidMissingScrAbbr, caption);
                return(false);
            }
            if (scriptSubtag != null && scriptSubtag.IsPrivateUse)
            {
                if (StandardSubtags.RegisteredScripts.Contains(scriptSubtag.Code))
                {
                    MessageBox.Show(FindForm(), FwCoreDlgControls.kstidDupScrAbbr, caption);
                    return(false);
                }
                if (!IetfLanguageTag.IsValidScriptCode(scriptSubtag.Code))
                {
                    MessageBox.Show(FindForm(), FwCoreDlgControls.kstidInvalidScrAbbr, caption);
                    return(false);
                }
            }

            RegionSubtag regionSubtag = RegionSubtag;

            // Can't allow a country name without an abbreviation.
            if (regionSubtag == null && !string.IsNullOrEmpty(m_regionName.Text.Trim()))
            {
                MessageBox.Show(FindForm(), FwCoreDlgControls.kstidMissingRgnAbbr, caption);
                return(false);
            }
            if (regionSubtag != null && regionSubtag.IsPrivateUse)
            {
                if (StandardSubtags.RegisteredRegions.Contains(regionSubtag.Code))
                {
                    MessageBox.Show(FindForm(), FwCoreDlgControls.kstidDupRgnAbbr, caption);
                    return(false);
                }
                if (!IetfLanguageTag.IsValidRegionCode(regionSubtag.Code))
                {
                    MessageBox.Show(FindForm(), FwCoreDlgControls.kstidInvalidRgnAbbr, caption);
                    return(false);
                }
            }

            VariantSubtag[] variantSubtags = VariantSubtags.ToArray();
            // Can't allow a variant name without an abbreviation.
            if (string.IsNullOrEmpty(m_variantAbbrev.Text.Trim()) && !string.IsNullOrEmpty(m_variantName.Text.Trim()))
            {
                MessageBox.Show(FindForm(), FwCoreDlgControls.kstidMissingVarAbbr, caption);
                return(false);
            }

            if (variantSubtags.Length > 0)
            {
                foreach (VariantSubtag variantSubtag in variantSubtags)
                {
                    if (variantSubtag.IsPrivateUse)
                    {
                        if (StandardSubtags.RegisteredVariants.Contains(variantSubtag.Code))
                        {
                            MessageBox.Show(FindForm(), FwCoreDlgControls.kstidDupVarAbbr, caption);
                            return(false);
                        }
                        if (!IetfLanguageTag.IsValidPrivateUseCode(variantSubtag.Code))
                        {
                            MessageBox.Show(FindForm(), FwCoreDlgControls.kstidInvalidVarAbbr, caption);
                            return(false);
                        }
                    }
                }

                List <string> parts = variantSubtags.Select(v => v.Code).ToList();
                // If these subtags are private use, the first element of each must also be distinct.
                if (m_ws.Language.IsPrivateUse)
                {
                    parts.Add(m_ws.Language.Code.Split('-').First());
                }
                if (scriptSubtag != null && scriptSubtag.IsPrivateUse)
                {
                    parts.Add(scriptSubtag.Code.Split('-').First());
                }
                if (regionSubtag != null && regionSubtag.IsPrivateUse)
                {
                    parts.Add(regionSubtag.Code.Split('-').First());
                }
                var uniqueParts = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                foreach (string part in parts)
                {
                    if (uniqueParts.Contains(part))
                    {
                        MessageBox.Show(FindForm(), String.Format(FwCoreDlgControls.kstidDuplicateParts, part), caption);
                        return(false);
                    }
                    uniqueParts.Add(part);
                }
            }

            return(true);
        }