Пример #1
0
        /// <summary>
        /// process localize tag helper
        /// </summary>
        /// <param name="context"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            var content = await output.GetChildContentAsync();

            if (!string.IsNullOrWhiteSpace(content.GetContent()))
            {
                var str = content.GetContent().Trim();

                LocalizedHtmlString _localStr;

                if (!string.IsNullOrEmpty(Culture) && ResourceSource != null)
                {
                    //localize from specified culture and resource type
                    _localStr = _loc.GetLocalizedHtmlString(ResourceSource, Culture, str, Args);
                }

                else if (!string.IsNullOrEmpty(Culture) && ResourceSource == null)
                {
                    //localize from specified culture and default view localization resource
                    //type that is defined in startup in .AddExpressLocalization<T1, T2>
                    //where T2 is the view localization resource
                    _localStr = _loc.GetLocalizedHtmlString(Culture, str, Args);
                }

                else if (string.IsNullOrEmpty(Culture) && ResourceSource != null)
                {
                    //localize from specified resource type using CultureInfo.CurrentCulture.Name
                    _localStr = _loc.GetLocalizedHtmlString(ResourceSource, str, Args);
                }
                else
                {
                    //use default localization
                    _localStr = _loc.GetLocalizedHtmlString(str, Args);
                }

                output.Content.SetHtmlContent(_localStr);
            }
        }
Пример #2
0
        /// <summary>
        /// process localize tag helper
        /// </summary>
        /// <param name="context"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (Localize || output.Attributes.Any(x => x.Name.StartsWith("localize-")))
            //if (Localize || (Args != null && Args.Length > 0) || !string.IsNullOrEmpty(Culture) || ResourceSource != null)
            {
                //list of attributes to be localized e.g. localize-att-title="Image title"
                var addAttributes = new List <TagHelperAttribute>();

                //list of attributes to be removed
                //technically, the attribute will be renamed by removing localize-att- prefix and the rest will be kept
                //e.g. localize-att-title="Image title" will be title="Resim başlığı"
                var removeAttributes = new List <TagHelperAttribute>();

                foreach (var att in output.Attributes)
                {
                    //fin all custom attributes that starts with localize-att-*
                    if (att.Name.StartsWith(_LocalizeAtt))
                    {
                        //get localized attribute value
                        var localAttValue = _loc.GetLocalizedHtmlString(att.Value.ToString());

                        //add new ttribute with new name and locized value to the list
                        addAttributes.Add(new TagHelperAttribute(att.Name.Replace(_LocalizeAtt, ""), localAttValue));

                        //add the attribute to the remove list
                        removeAttributes.Add(att);
                    }
                }

                if (addAttributes.Count > 0)
                {
                    //add new attibutes to the output
                    addAttributes.ForEach(x => output.Attributes.Add(x));
                }

                if (removeAttributes.Count > 0)
                {
                    //remove old attributes from the output
                    removeAttributes.ForEach(x => output.Attributes.Remove(x));
                }

                await base.ProcessAsync(context, output);
            }
        }