Beispiel #1
0
        public IEnumerable <MenuItem> Filter(IEnumerable <MenuItem> menuItems)
        {
            string currentCulture = _cultureManager.GetCurrentCulture(_workContextAccessor.GetContext().HttpContext);

            foreach (MenuItem menuItem in menuItems)
            {
                ILocalizableAspect localizationPart = menuItem.Content.Is <ContentMenuItemPart>()
                                                          ? menuItem.Content.As <ContentMenuItemPart>().Content.As <ILocalizableAspect>()
                                                          : menuItem.Content.As <ILocalizableAspect>();
                if (localizationPart == null ||
                    localizationPart.Culture == null ||
                    localizationPart.Culture == currentCulture)
                {
                    yield return(menuItem);
                }
            }
        }
        //Finds localized route part for the specified content and culture
        //Returns true if localized url for content and culture exists; otherwise - false
        public bool TryFindLocalizedRoute(ContentItem routableContent, string cultureName, out AutoroutePart localizedRoute)
        {
            if (!routableContent.Parts.Any(p => p.Is <ILocalizableAspect>()))
            {
                localizedRoute = null;
                return(false);
            }

            //var siteCulture = _cultureManager.GetCultureByName(_cultureManager.GetSiteCulture());
            IEnumerable <LocalizationPart> localizations = _localizationService.GetLocalizations(routableContent, VersionOptions.Published);

            ILocalizableAspect localizationPart = null, siteCultureLocalizationPart = null;

            foreach (LocalizationPart l in localizations)
            {
                if (l.Culture.Culture == cultureName)
                {
                    localizationPart = l;
                    break;
                }
                if (l.Culture == null && siteCultureLocalizationPart == null)
                {
                    siteCultureLocalizationPart = l;
                }
            }

            //try get localization part for default site culture
            if (localizationPart == null)
            {
                localizationPart = siteCultureLocalizationPart;
            }

            if (localizationPart == null)
            {
                localizedRoute = null;
                return(false);
            }

            ContentItem localizedContentItem = localizationPart.ContentItem;

            localizedRoute = localizedContentItem.Parts.Single(p => p is AutoroutePart).As <AutoroutePart>();
            return(true);
        }