protected virtual TblLanguages GetLanguageFromBrowserSettings() { if (HttpContext?.Request?.UserLanguages == null) { return(null); } // Get user preferred language from Accept-Language header (ignore English) var userLanguage = HttpContext?.Request.UserLanguages.FirstOrDefault(p => !p.StartsWith("en", StringComparison.OrdinalIgnoreCase)) ?? HttpContext?.Request.UserLanguages.FirstOrDefault(); if (string.IsNullOrEmpty(userLanguage) || userLanguage.Length < 2) { return(null); } var language = _languagesService .GetAsEnumerable() .FirstOrDefault(l => userLanguage.Substring(0, 2).Trim().Equals(l.IsoCode, StringComparison.InvariantCultureIgnoreCase)); if (language != null && language.Published) { return(language); } return(null); }
public static void DeletePluginLocaleResource(this BasePlugin plugin, ILocalizationService localizationService, ILanguagesService languageService, string resourceName) { //actually plugin instance is not required if (plugin == null) { throw new ArgumentNullException(nameof(plugin)); } if (localizationService == null) { throw new ArgumentNullException(nameof(localizationService)); } if (languageService == null) { throw new ArgumentNullException(nameof(languageService)); } foreach (var lang in languageService.GetAsEnumerable()) { var lsr = localizationService.FindByName(resourceName, lang.Id); if (lsr != null) { localizationService.Delete(lsr.Id); } } }
public static void AddOrUpdatePluginLocaleResource(this BasePlugin plugin, ILocalizationService localizationService, ILanguagesService languageService, string resourceName, string resourceValue, string languageISO = null) { //actually plugin instance is not required if (plugin == null) { throw new ArgumentNullException(nameof(plugin)); } if (localizationService == null) { throw new ArgumentNullException(nameof(localizationService)); } if (languageService == null) { throw new ArgumentNullException(nameof(languageService)); } foreach (var lang in languageService.GetAsEnumerable()) { if (!string.IsNullOrEmpty(languageISO) && !languageISO.Equals(lang.IsoCode)) { continue; } var lsr = localizationService.FindByName(resourceName, lang.Id); if (lsr == null) { lsr = new TblLocalizedStrings() { LanguageId = lang.Id, ResourceName = resourceName, ResourceValue = resourceValue }; localizationService.Add(lsr); } else { lsr.ResourceValue = resourceValue; localizationService.Update(lsr); } } }
public virtual async Task <ActionResult> Index() { var items = new List <SitemapItem>(); var languagesList = _languagesService.GetAsEnumerable().Where(p => p.Published) .OrderByDescending(p => p.IsDefault); foreach (var language in languagesList) { items.Add(new SitemapItem( Url.Action("Index", "Home", new { lang = language.IsoCode }, Request.Url.Scheme), DateTime.Now, SitemapChangeFrequency.Daily, 1)); items.AddRange((await _pagesService.GetAsEnumerableAsync()).Where(p => p.Published).Select(page => new SitemapItem( Url.Action("Index", "Page", new { slug = page.Slug, lang = language.IsoCode }, Request.Url.Scheme), DateTime.Now, SitemapChangeFrequency.Daily, 1))); items.AddRange((await _tagsService.GetAsEnumerableAsync()).Select(tag => new SitemapItem( Url.Action("Tag", "Search", new { tag = tag.GetLocalized(x => x.Tag, language.Id), lang = language.IsoCode }, Request.Url.Scheme), DateTime.Now, SitemapChangeFrequency.Daily, 0.9)).DistinctBy(p => p.Url)); items.AddRange((await _categoriesService.GetAsEnumerableAsync()).Select(category => new SitemapItem( Url.Action("FilterByCategory", "Product", new { slug = category.Slug, lang = language.IsoCode }, Request.Url.Scheme), DateTime.Now, SitemapChangeFrequency.Daily, 0.8))); items.AddRange((await _categoriesService.GetAsEnumerableAsync()).Select(category => new SitemapItem( Url.Action("FilterByCategory", "Blog", new { slug = category.Slug, lang = language.IsoCode }, Request.Url.Scheme), DateTime.Now, SitemapChangeFrequency.Daily, 0.8))); foreach (var post in _postService.GetNewItemsForSiteMap()) { Uri url = new Uri(Url.Action("Index", "Search", new { lang = language.IsoCode, OrderBy = SearchResultSortType.Score, SearchPlace = SearchPlace.Title, Query = post.Title }, Request.Url.Scheme)); if (post.PostType == PostType.BlogPost) { url = new Uri(Url.Action("Post", "Blog", new { slug = post.Slug, lang = language.IsoCode }, Request.Url.Scheme)); } if (post.PostType == PostType.Product) { url = new Uri(Url.Action("Index", "Product", new { slug = post.Slug, lang = language.IsoCode }, Request.Url.Scheme)); } items.Add(new SitemapItem(url.ToString(), post.LastUpDate ?? post.PublishDate, SitemapChangeFrequency.Weekly, 0.7)); } } return(Content(_sitemapGenerator.GenerateSiteMap(items).ToString(), "text/xml", Encoding.UTF8)); }
public virtual long CreateIndex() { try { var watch = new System.Diagnostics.Stopwatch(); watch.Start(); if (System.IO.Directory.Exists(_indexFilesPath)) { try { using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath))) directory.ClearLock(directory.GetLockId()); } catch { } FileUtils.DeleteDirRecursively(new DirectoryInfo(_indexFilesPath)); } System.IO.Directory.CreateDirectory(_indexFilesPath); if (System.IO.Directory.Exists(_spellFilesPath)) { try { using (var directory = FSDirectory.Open(new DirectoryInfo(_spellFilesPath))) directory.ClearLock(directory.GetLockId()); } catch { } FileUtils.DeleteDirRecursively(new DirectoryInfo(_spellFilesPath)); } System.IO.Directory.CreateDirectory(_spellFilesPath); var allPosts = _postService.GetAsQueryable().Where(p => p.Published) .Include(p => p.Descriptions) .Include(p => p.Tags) .Include(p => p.Categories); var languages = _languagesService.GetAsEnumerable(); var analyzer = new StandardAnalyzer(Version); using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath))) { using (var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED)) { foreach (var post in allPosts) { writer.AddDocument(MapPost(post, post.PostType));//Default values foreach (var language in languages.OrderByDescending(p => p.IsDefault)) { var localizedMap = MapPost(post, language, post.PostType); if (localizedMap != null) { writer.AddDocument(localizedMap); //Localized values } } } writer.Optimize(); writer.Commit(); } } using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath))) { using (var spellDirectory = FSDirectory.Open(new DirectoryInfo(_spellFilesPath))) { using (var indexReader = IndexReader.Open(directory, readOnly: true)) { using (var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(spellDirectory)) { // Create SpellChecker Index spellChecker.ClearIndex(); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Title")); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Description")); spellChecker.Close(); } } } } watch.Stop(); MethodCache.ExpireTag(CacheTags.Search); return(watch.ElapsedMilliseconds); } catch (Exception e) { MethodCache.ExpireTag(CacheTags.Search); _eventPublisher.Publish(new CreateSearchIndexesFailEvent(e)); throw; } }