csharp 用于网址翻译的LocalizationAttribute。视图中的类本地化,routeconfig和链接。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 用于网址翻译的LocalizationAttribute。视图中的类本地化,routeconfig和链接。相关的知识,希望对你有一定的参考价值。

 @Html.RouteLink(@LanguageResources.Admin_Index_Dashboard, "Admin", new { Area = "", page = LanguageResources.Url_Translate_Admin })
routes.MapRoute(
                    name: "Property",
                    url: "{lang}/{pageProperty}/{agencyName}/{propertyReference}/{urlSafeTitle}",
                    constraints: new { lang = @"(\w{2})", pageProperty = "property|propiedad|immobilie" },
                    defaults: new { lang = ConfigurationManager.AppSettings["languageDefault"].ToString(), controller = "Property", action = "GetProperty" },
                    namespaces: new string[] { "chilliIS.propertyMallorca.Web.Controllers" }
                );
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
//using System.Web.Http.Filters;
using System.Web.Mvc;

namespace chilliIS.propertyMallorca.Web
{
    public class LocalizationAttribute : ActionFilterAttribute
    {
        private string _DefaultLanguage = ConfigurationManager.AppSettings["languageDefault"].ToString();
        private string _Cultures = ConfigurationManager.AppSettings["languages"].ToString();

        public LocalizationAttribute(string defaultLanguage)
        {
            _DefaultLanguage = defaultLanguage;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            const string routeDataKey = "lang";

            string lang = _DefaultLanguage;

            if (filterContext.RouteData.Values[routeDataKey] == null)
            {
                if (HttpContext.Current.Session["language"] != null)
                {
                    lang = HttpContext.Current.Session["language"].ToString();

                    filterContext.RouteData.Values[routeDataKey] = lang;
                }
            }
            else
            {
                lang = filterContext.RouteData.Values[routeDataKey].ToString();
            }

            if (!string.IsNullOrEmpty(lang) && _Cultures.Split(new char[] {','}).Contains(lang))
            {
                try
                {
                    HttpContext.Current.Session["language"] = lang;
                    LanguageResources.LanguageResources.Culture = new CultureInfo(lang);
                    Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
                }
                catch (Exception e)
                {
                    throw new NotSupportedException(String.Format("ERROR: Invalid language code '{0}'.", lang));
                }
            }
        }
    }
}

以上是关于csharp 用于网址翻译的LocalizationAttribute。视图中的类本地化,routeconfig和链接。的主要内容,如果未能解决你的问题,请参考以下文章

csharp 网址

csharp 将图像转换为数据网址

csharp Tranform翻译

csharp 团结/变换/翻译

csharp 服务器翻译

csharp 服务器翻译