Created
July 16, 2017 10:02
-
-
Save davidknipe/f6850df913e450a284ac32b39db5766b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using EPiServer.Find.Framework.BestBets; | |
| using EPiServer.Globalization; | |
| using System; | |
| using System.Globalization; | |
| using System.Linq; | |
| namespace BestBetExtension.Business.CustomPhraseCriterion | |
| { | |
| public class LocaleAwarePhraseCriterion : DefaultPhraseCriterion, IPhraseCriterion | |
| { | |
| private const string localeIdenifier = "#"; | |
| public override bool Match(string searchPhrase) | |
| { | |
| if (string.IsNullOrWhiteSpace(searchPhrase) || (Phrase == null)) | |
| { | |
| return false; | |
| } | |
| if (Phrase.Contains(localeIdenifier)) | |
| { | |
| // Get current Episerver locale | |
| CultureInfo currentCulture = ContentLanguage.PreferredCulture; | |
| string languageBranch = currentCulture.Name; | |
| // Get matches for the current culture | |
| string[] bestBetPhrases = ParsePhrasesForLocale(Phrase, languageBranch); | |
| string[] searchPhrases = ParsePhrases(searchPhrase); | |
| return bestBetPhrases.Any<string>(p => searchPhrases.Contains<string>(p, StringComparer.OrdinalIgnoreCase)); | |
| } | |
| return base.Match(searchPhrase); | |
| } | |
| internal static string[] ParsePhrases(string value) | |
| { | |
| if (!string.IsNullOrWhiteSpace(value)) | |
| { | |
| string[] separator = new string[] { "," }; | |
| return (from e in value.Split(separator, StringSplitOptions.RemoveEmptyEntries) | |
| where !string.IsNullOrWhiteSpace(e) | |
| select e.Trim()).ToArray<string>(); | |
| } | |
| return new string[0]; | |
| } | |
| internal static string[] ParsePhrasesForLocale(string value, string locale) | |
| { | |
| string localeHash = localeIdenifier + locale; | |
| // Parse out locales that do not match | |
| var phrases = ParsePhrases(value); | |
| if (phrases.Length > 0) | |
| { | |
| return (from e in phrases | |
| where e.EndsWith(localeIdenifier + locale, StringComparison.OrdinalIgnoreCase) | |
| select e.Trim().Replace(localeHash, string.Empty).Trim()).ToArray(); | |
| } | |
| return phrases; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment