コード例 #1
0
 public void IsValidIso3166Region_fonipa_ReturnsFalse()
 {
     Assert.IsFalse(StandardTags.IsValidIso3166Region("fonipa"));
 }
コード例 #2
0
 public void IsValidIso3166Region_US_ReturnsTrue()
 {
     Assert.IsTrue(StandardTags.IsValidIso3166Region("US"));
 }
コード例 #3
0
 public void IsValidIso3166Region_QM_ReturnsTrue()
 {
     Assert.IsTrue(StandardTags.IsValidIso3166Region("QM"));
     Assert.IsTrue(StandardTags.IsValidIso3166Region("qm"));
 }
コード例 #4
0
        public void Clean()
        {
            // Migrate legacy ISO3 language codes to IANA 2 letter language codes, if there's a match.
            // Do this before we look for valid codes, otherwise the 3-letter ones come up as invalid and
            // get moved to private use. However, only do this to languages not identified as private-use.
            if (!Language.StartsWith("x-", StringComparison.OrdinalIgnoreCase))
            {
                string migrateFrom = "";
                string migrateTo   = "";
                foreach (string part in _languageSubTag.AllParts)
                {
                    if (part.Equals("x", StringComparison.OrdinalIgnoreCase))
                    {
                        break;                         // don't migrate language code parts already explicitly marked private-use.
                    }
                    if (String.IsNullOrEmpty(migrateFrom))
                    {
                        foreach (var code in StandardTags.ValidIso639LanguageCodes)
                        {
                            if (code.ISO3Code.Equals(part))
                            {
                                migrateFrom = part;
                                migrateTo   = code.Code;
                                break;
                            }
                        }
                    }
                }
                if (!String.IsNullOrEmpty(migrateFrom))
                {
                    _languageSubTag.RemoveParts(migrateFrom);
                    _languageSubTag.AddToSubtag(migrateTo);
                }
            }
            // The very next thing, before anything else gets moved to private use, is to move the parts whose position we
            // care about to the appropriate position in the private use section.
            // In the process we may remove anything non-alphanumeric, since otherwise we may move a marker that later
            // disappears (pathologically).
            MoveFirstPartToPrivateUseIfNecessary(_languageSubTag, StandardTags.IsValidIso639LanguageCode, "qaa", true);
            MoveFirstPartToPrivateUseIfNecessary(_scriptSubTag, StandardTags.IsValidIso15924ScriptCode, "Qaaa", false);
            MoveFirstPartToPrivateUseIfNecessary(_regionSubTag, StandardTags.IsValidIso3166Region, "QM", false);
            //This fixes a bug where the LdmlAdaptorV1 was writing out Zxxx as part of the variant to mark an audio writing system
            if (_variantSubTag.Contains(WellKnownSubTags.Audio.Script))
            {
                MoveTagsMatching(_variantSubTag, _scriptSubTag, tag => tag.Equals(WellKnownSubTags.Audio.Script));
                _privateUseSubTag.AddToSubtag(WellKnownSubTags.Audio.PrivateUseSubtag);
            }
            // Fixes various legacy problems.
            if (Language.Equals("cmn", StringComparison.OrdinalIgnoreCase))
            {
                Language = "zh";
            }
            if (Language.Equals("pes", StringComparison.OrdinalIgnoreCase))
            {
                Language = "fa";
            }
            if (Language.Equals("arb", StringComparison.OrdinalIgnoreCase))
            {
                Language = "ar";
            }
            if (Language.Equals("zh", StringComparison.OrdinalIgnoreCase) && String.IsNullOrEmpty(Region))
            {
                Region = "CN";
            }

            // If the language tag contains an x- , then move the string behind the x- to private use
            MovePartsToPrivateUseIfNecessary(_languageSubTag);

            // Move script, region, and variant present in the langauge tag to their proper subtag.
            MoveTagsMatching(_languageSubTag, _scriptSubTag, StandardTags.IsValidIso15924ScriptCode, StandardTags.IsValidIso639LanguageCode);
            MoveTagsMatching(_languageSubTag, _regionSubTag, StandardTags.IsValidIso3166Region, StandardTags.IsValidIso639LanguageCode);
            MoveTagsMatching(_languageSubTag, _variantSubTag, StandardTags.IsValidRegisteredVariant, StandardTags.IsValidIso639LanguageCode);

            // Move all other tags that don't belong to the private use subtag.

            //keep track of everything that we moved
            var tempSubTag = new SubTag();

            MoveTagsMatching(
                _languageSubTag, tempSubTag, tag => !StandardTags.IsValidIso639LanguageCode(tag)
                );
            //place all the moved parts in private use.
            foreach (var part in tempSubTag.AllParts)
            {
                _privateUseSubTag.AddToSubtag(part);
                //if it looks like we moved a custom script set the subtag to mark that we've moved it
                if (_scriptSubTag.IsEmpty &&
                    part.Length == 4 &&                     //potential custom script tag
                    !WellKnownSubTags.Ipa.PhonemicPrivateUseSubtag.EndsWith(part) &&
                    !WellKnownSubTags.Ipa.PhoneticPrivateUseSubtag.EndsWith(part))
                {
                    _scriptSubTag = new SubTag("Qaaa");
                }
            }

            MoveTagsMatching(
                _scriptSubTag, _privateUseSubTag, tag => !StandardTags.IsValidIso15924ScriptCode(tag)
                );
            MoveTagsMatching(
                _regionSubTag, _privateUseSubTag, tag => !StandardTags.IsValidIso3166Region(tag)
                );
            MoveTagsMatching(
                _variantSubTag, _privateUseSubTag, tag => !StandardTags.IsValidRegisteredVariant(tag)
                );

            _languageSubTag.KeepFirstAndMoveRemainderTo(_privateUseSubTag);
            _scriptSubTag.KeepFirstAndMoveRemainderTo(_privateUseSubTag);
            _regionSubTag.KeepFirstAndMoveRemainderTo(_privateUseSubTag);

            if (_privateUseSubTag.Contains(WellKnownSubTags.Audio.PrivateUseSubtag))
            {
                // Move every tag that's not a Zxxx to private use
                if (!_scriptSubTag.IsEmpty && !_scriptSubTag.Contains(WellKnownSubTags.Audio.Script))
                {
                    MoveTagsMatching(_scriptSubTag, _privateUseSubTag, tag => !_privateUseSubTag.Contains(tag));
                }
                // If we don't have a Zxxx already, set it. This protects tags already present, but with unusual case
                if (!_scriptSubTag.Contains(WellKnownSubTags.Audio.Script))
                {
                    _scriptSubTag = new SubTag(WellKnownSubTags.Audio.Script);
                }
            }

            //These two methods may produce duplicates that will subsequently be removed. Do we care? - TA 29/3/2011
            _privateUseSubTag.TruncatePartsToNumCharacters(8);
            _privateUseSubTag.RemoveNonAlphaNumericCharacters();

            _variantSubTag.RemoveDuplicates();
            _privateUseSubTag.RemoveDuplicates();
            // Any 'x' in the other tags will have arrived in the privateUse tag, so remove them.
            _privateUseSubTag.RemoveParts("x");

            // if language is empty, we need to add qaa, unless only a privateUse is present (e.g. x-blah is a valid rfc5646 tag)
            if ((_languageSubTag.IsEmpty && (!_scriptSubTag.IsEmpty || !_regionSubTag.IsEmpty || !_variantSubTag.IsEmpty)) ||
                (_languageSubTag.IsEmpty && _scriptSubTag.IsEmpty && _regionSubTag.IsEmpty && _variantSubTag.IsEmpty && _privateUseSubTag.IsEmpty))
            {
                _languageSubTag.AddToSubtag("qaa");
            }

            // Two more legacy problems. We don't allow -etic or -emic without fonipa, so insert if needed.
            // If it has some other standard variant we won't be able to fix it...not sure what the right answer would be.
            // At least we catch the more common case.
            foreach (var part in _privateUseSubTag.AllParts)
            {
                if (string.IsNullOrEmpty(Variant) &&
                    (part.Equals("etic", StringComparison.OrdinalIgnoreCase) || part.Equals("emic", StringComparison.OrdinalIgnoreCase)))
                {
                    Variant = "fonipa";
                }
            }
        }