private void ValidateVariant() { var invalidPart = _variant.AllParts.FirstOrDefault(part => !StandardTags.IsValidRegisteredVariant(part)); if (!String.IsNullOrEmpty(invalidPart)) { throw new ValidationException( String.Format("'{0}' is not a valid registered variant code.", invalidPart) ); } _variant.ThrowIfSubtagContainsDuplicates(); }
///<summary>Constructor method to parse a valid RFC5646 tag as a string ///</summary> ///<param name="inputString">valid RFC5646 string</param> ///<returns>RFC5646Tag object</returns> public static RFC5646Tag Parse(string inputString) { var tokens = inputString.Split(new[] { '-' }); var rfc5646Tag = new RFC5646Tag(); bool haveX = false; for (int position = 0; position < tokens.Length; ++position) { var token = tokens[position]; if (token == "x") { haveX = true; continue; } if (haveX) { //This is the case for RfcTags consisting only of a private use subtag if (position == 1) { rfc5646Tag = new RFC5646Tag(String.Empty, String.Empty, String.Empty, String.Empty, token); continue; } rfc5646Tag.AddToPrivateUse(token); continue; } if (position == 0) { rfc5646Tag.Language = token; continue; } if (position <= 1 && StandardTags.IsValidIso15924ScriptCode(token)) { rfc5646Tag.Script = token; continue; } if (position <= 2 && StandardTags.IsValidIso3166Region(token)) { rfc5646Tag.Region = token; continue; } if (StandardTags.IsValidRegisteredVariant(token)) { rfc5646Tag.AddToVariant(token); continue; } throw new ValidationException(String.Format("The RFC tag '{0}' could not be parsed.", inputString)); } return(rfc5646Tag); }
private void ValidateRegion() { if (String.IsNullOrEmpty(_region)) { return; } if (_region.Contains("-")) { throw new ValidationException("The region tag may not contain dashes or underscores. I.e. there may only be a single iso 639 tag in this subtag"); } if (!StandardTags.IsValidIso3166Region(_region)) { throw new ValidationException(String.Format("'{0}' is not a valid ISO-3166 region code.", _region)); } }
private void ValidateScript() { if (String.IsNullOrEmpty(_script)) { return; } if (_script.Contains("-")) { throw new ValidationException("The script tag may not contain dashes or underscores. I.e. there may only be a single iso 639 tag in this subtag"); } if (!StandardTags.IsValidIso15924ScriptCode(_script)) { throw new ValidationException(String.Format("'{0}' is not a valid ISO-15924 script code.", _script)); } }
private void ValidateLanguage() { if (String.IsNullOrEmpty(_language)) { return; } if (_language.Contains("-")) { throw new ValidationException( "The language tag may not contain dashes. I.e. there may only be a single iso 639 tag in this subtag" ); } if (!StandardTags.IsValidIso639LanguageCode(_language)) { throw new ValidationException(String.Format("'{0}' is not a valid ISO-639 language code.", _language)); } }
public void ConvertToPalasoConformPrivateUseRfc5646Tag(string flexConformPrivateUseRfc5646Tag) { string language = String.Empty; string script = String.Empty; string region = String.Empty; string variant = String.Empty; string privateUse = String.Empty; var tokens = flexConformPrivateUseRfc5646Tag.Split(new[] { '-' }); for (int position = 0; position < tokens.Length; ++position) { string currentToken = tokens[position]; if (position == 0) { if (!currentToken.Equals("x", StringComparison.OrdinalIgnoreCase)) { throw new ValidationException(String.Format("The rfctag {0} does not start with 'x-' or 'X-'.", flexConformPrivateUseRfc5646Tag)); } language = currentToken; } else if (position == 1 && !StandardTags.IsValidIso15924ScriptCode(currentToken)) { language = language + '-' + currentToken; continue; } else if (StandardTags.IsValidIso15924ScriptCode(currentToken)) { if (!String.IsNullOrEmpty(region) || !String.IsNullOrEmpty(variant)) { throw new ValidationException( String.Format( "The rfctag '{0}' contains a misplaced Script subtag (i.e. it was preceded by a region or variant subtag.", flexConformPrivateUseRfc5646Tag)); } script = currentToken; } else if (StandardTags.IsValidIso3166Region(currentToken)) { if (!String.IsNullOrEmpty(variant)) { throw new ValidationException( String.Format( "The rfctag '{0}' contains a misplaced Region subtag (i.e. it was preceded by a variant subtag.", flexConformPrivateUseRfc5646Tag)); } region = currentToken; } else if (StandardTags.IsValidRegisteredVariant(currentToken)) { variant = variant + currentToken; } else { privateUse = String.IsNullOrEmpty(privateUse) ? currentToken : privateUse + '-' + currentToken; } } variant = WritingSystemDefinition.ConcatenateVariantAndPrivateUse(variant, privateUse); ConvertToPalasoConformPrivateUseRfc5646Tag(language, script, region, variant); }