Exemplo n.º 1
0
 protected virtual async Task <string> GetContentOrNullAsync(TemplateDefinition templateDefinition)
 {
     return(await TemplateContentProvider.GetContentOrNullAsync(templateDefinition));
 }
Exemplo n.º 2
0
        public virtual async Task <string> GetContentOrNullAsync(
            [NotNull] TemplateDefinition templateDefinition,
            [CanBeNull] string cultureName = null,
            bool tryDefaults = true,
            bool useCurrentCultureIfCultureNameIsNull = true)
        {
            Check.NotNull(templateDefinition, nameof(templateDefinition));

            if (!Options.ContentContributors.Any())
            {
                throw new AbpException(
                          $"No template content contributor was registered. Use {nameof(AbpTextTemplatingOptions)} to register contributors!"
                          );
            }

            using (var scope = ServiceScopeFactory.CreateScope())
            {
                string templateString = null;

                if (cultureName == null && useCurrentCultureIfCultureNameIsNull)
                {
                    cultureName = CultureInfo.CurrentUICulture.Name;
                }

                var contributors = CreateTemplateContentContributors(scope.ServiceProvider);

                //Try to get from the requested culture
                templateString = await GetContentOrNullAsync(
                    contributors,
                    new TemplateContentContributorContext(
                        templateDefinition,
                        scope.ServiceProvider,
                        cultureName
                        )
                    );

                if (templateString != null)
                {
                    return(templateString);
                }

                if (!tryDefaults)
                {
                    return(null);
                }

                //Try to get from same culture without country code
                if (cultureName != null && cultureName.Contains("-")) //Example: "tr-TR"
                {
                    templateString = await GetContentOrNullAsync(
                        contributors,
                        new TemplateContentContributorContext(
                            templateDefinition,
                            scope.ServiceProvider,
                            CultureHelper.GetBaseCultureName(cultureName)
                            )
                        );

                    if (templateString != null)
                    {
                        return(templateString);
                    }
                }

                if (templateDefinition.IsInlineLocalized)
                {
                    //Try to get culture independent content
                    templateString = await GetContentOrNullAsync(
                        contributors,
                        new TemplateContentContributorContext(
                            templateDefinition,
                            scope.ServiceProvider,
                            null
                            )
                        );

                    if (templateString != null)
                    {
                        return(templateString);
                    }
                }
                else
                {
                    //Try to get from default culture
                    if (templateDefinition.DefaultCultureName != null)
                    {
                        templateString = await GetContentOrNullAsync(
                            contributors,
                            new TemplateContentContributorContext(
                                templateDefinition,
                                scope.ServiceProvider,
                                templateDefinition.DefaultCultureName
                                )
                            );

                        if (templateString != null)
                        {
                            return(templateString);
                        }
                    }
                }
            }

            //Not found
            return(null);
        }