/// <summary> /// Obtain the branding entry for the target culture. /// </summary> /// <param name="ci"></param> /// <returns>Returns the localized branding entry, as specified by user in the config file. If no entry is specified for the /// target culture, returns the English branding name. This corresponds to the logic of the config file where no localized entry means /// that the target branding name is the same as English.</returns> public BrandingEntry GetLocalizedBrandingEntry(CultureInfo ci) { BrandingEntry ret = EnglishBrandingEntry; if (!Object.ReferenceEquals(translationMapping, null)) { if (!translationMapping.TryGetValue(ci, out ret)) { ret = EnglishBrandingEntry; } } return(ret); }
public IndexEntry(BrandingEntry englishBrandingEntry, Dictionary <CultureInfo, BrandingEntry> translationMapping) { EnglishBrandingEntry = englishBrandingEntry; this.translationMapping = translationMapping; }
/// <summary> /// Prepare the rule for first use. /// </summary> protected override void Init() { base.Init(); //Loading check config from an XML file //TODO: Implement XML Schema validation when loading XDocument xDoc; try { xDoc = XDocument.Load(Environment.ExpandEnvironmentVariables(pathToCheckConfig)); } catch (Exception e) { throw new Microsoft.Localization.OSLEBot.Exceptions.InitializingRuleException("Failed to load config file for check.", e); } var defaultNS = (xDoc.Root.GetDefaultNamespace()).NamespaceName; var brandingNames = xDoc.Root.Descendants(XName.Get("BrandingName", defaultNS)).ToArray(); brandingNameIndex = new IndexEntry[brandingNames.Length]; for (int i = 0; i < brandingNames.Length; i++) { var bNameNode = brandingNames[i]; var englishName = bNameNode.Attribute(XName.Get("Value")).Value; var englishRegexAttribute = bNameNode.Attribute(XName.Get("RegularExpression")); string englishNameRegexPattern = null; if (englishRegexAttribute == null || String.IsNullOrEmpty(englishRegexAttribute.Value)) { // create regex based on the englishName provided by user englishNameRegexPattern = GetRegexPattern(englishName); } else { //user provided a regex pattern, so use it as is englishNameRegexPattern = englishRegexAttribute.Value; } var enBrandingEntry = new BrandingEntry { BrandingName = englishName, Regex = new Regex(englishNameRegexPattern, RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.Singleline) }; // do the same for all language translations Dictionary <CultureInfo, BrandingEntry> translationMapping = null; var localizedBrandingNames = bNameNode.Descendants(XName.Get("LocalizedBrandingName", defaultNS)).ToArray(); if (localizedBrandingNames.Length > 0) { translationMapping = new Dictionary <CultureInfo, BrandingEntry>(localizedBrandingNames.Length); foreach (var lbNameNode in localizedBrandingNames) { var locName = lbNameNode.Attribute(XName.Get("Translation")).Value; var locRegexAttribute = lbNameNode.Attribute(XName.Get("RegularExpression")); string locNameRegexPattern = null; if (locRegexAttribute == null || String.IsNullOrEmpty(locRegexAttribute.Value)) { // create regex based on the localized name provided by user locNameRegexPattern = GetRegexPattern(locName); } else { locNameRegexPattern = locRegexAttribute.Value; } var locBrandingEntry = new BrandingEntry { BrandingName = locName, Regex = new Regex(locNameRegexPattern, RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.Singleline) }; var targetCulture = new CultureInfo(lbNameNode.Attribute(XName.Get("Culture")).Value); translationMapping.Add(targetCulture, locBrandingEntry); } } brandingNameIndex[i] = new IndexEntry(enBrandingEntry, translationMapping); } }