• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

yasinkuyu/Localization: Create multi-language structure with ASP.NET MVC

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

yasinkuyu/Localization

开源软件地址(OpenSource Url):

https://github.com/yasinkuyu/Localization

开源编程语言(OpenSource Language):

C# 100.0%

开源软件介绍(OpenSource Introduction):

Localization

Create multi-language structure with ASP.NET MVC @yasinkuyu

PM> Install-Package Localization

Usage

- <li>@Html.ActionLinkLocalization("English", "Index", "Locales", new { lang = "en_US" })</li>
- <li>@Html.ActionLinkLocalization("Türkçe", "Index", "Locales", new { lang = "tr_TR" })</li>
and more...
Or
- <a href="Locales/?lang=en_US">English</a>
- <a href="Locales/?lang=tr_TR">Türkçe</a>
and more...

Index.cshtml

@Html.Get("homepage") or @Html.Localize("homepage")

Video Tutorial

ASP.NET MVC Localization Tutorial vimeo

ASP.NET MVC Localization Tutorial youtube

Requirements

Localization

  • en_US.xml
  • tr_TR.xml
  • and more...

Bin

  • Insya.Localization.dll
  • System.ComponentModel.DataAnnotations

Default locale

public void DefaultLocale( )
{
    HttpCookie cookie = Request.Cookies.Get("CacheLang");
    if ( cookie == null)
    {
        HttpCookie newCookie = new HttpCookie("CacheLang");
        newCookie.Value = "en_US";
        Response.Cookies.Add(newCookie);
    }
}

Structure

Views

Xml file
<item id="homepage">Home Page</item>

Razor
     @Html.Localize("homepage")
     Or
     @Html.Get("homepage")
  
Code Behind
     Localization.Localize("homepage")
     Or
     Localization.Get("homepage")

@Html.ActionLinkLocalization()
@Html.ActionLocalization()

Inline Localization

@Html.Get(new Inline(en: "book", tr: "kitap"))
@Html.Localize(new Inline(en: "book", tr: "kitap"))

Controllers

public ActionResult Index(string lang = "en_US")
{
  Response.Cookies["CacheLang"].Value = lang;
  
  if (Request.UrlReferrer != null)
  Response.Redirect(Request.UrlReferrer.ToString());
  
  var message = Localization.Get("changedlng");
  
  return Content(message);
}

Models

Attribute Localize
  Display Attribute 
    [DisplayLocalize("username")]
    public string Username { get; set; }
  
  String Length Attribute 
    [StringLengthLocalize(20, MinimumLength = 4)]
    public string DisplayName { get; set; }
  
  Required Attribute 
    [RequiredLocalized]
    public string DisplayName { get; set; }
  
  Description Attribute 
    [DescriptionLocalize]
    public string DisplayName { get; set; }

Intellisense in razor files (Views/web.config)

  • Views
    • web.config
      • system.web.webPages.razor
        • pages
          • namespaces
            • add namespace="Insya.Localization"
            • add namespace="Insya.Localization.Helpers"

Example

<system.web.webPages.razor>
  <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <pages pageBaseType="System.Web.Mvc.WebViewPage">
          <namespaces>
          <add namespace="System.Web.Mvc" />
          <add namespace="System.Web.Mvc.Ajax" />
          <add namespace="System.Web.Mvc.Html" />
          <add namespace="System.Web.Optimization"/>
          <add namespace="System.Web.Routing" />
          ## <add namespace="Insya.Localization" />
          ## <add namespace="Insya.Localization.Helpers" />
          </namespaces>
      </pages>
</system.web.webPages.razor>

Note: Nuget package auto insert

Language Codes

https://msdn.microsoft.com/en-us/library/ms533052(v=vs.85).aspx

Contribution

Fork

Roadmap:

  • Localizable Routing
  • All the XML language files editing tool on one screen.
  • Testing tools



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap