2017-07-06 119 views
0

我通過使用Asp.net MVC中的資源文件實現了multi-language website。 所有工作正常,但我的問題是在layout。如何在我的佈局中使用動態資源view-bag如何在Asp.net MVC佈局中使用資源文件

我需要寫像這樣的地方在我的佈局,但我不知道什麼是做到這一點的正確方法:

@{ var langu = ViewBag.lang;} 
@Resources.langu.App_Name; 

它當然有誤差。

這是我的頁面佈局,如果需要:

<!DOCTYPE html> 
<html> 
<head> 
    ..... 
</head> 

<body> 
    <div class="preloader"> 
     <div class="spinner"> 
      <div class="double-bounce1"></div> 
      <div class="double-bounce2"></div> 
     </div> 
    </div> 

    <!-- Header 
    ============================================ --> 
    <div class="header"> 

     <div class="container relativesaz"> 
      <div class="row"> 
       <div class="col-sm-12"> 
        <!-- Navbar Header --> 
        <div class="navbar-header"> 
         <!-- Menu Toggle --> 
         <button class="menu-toggle"><i class="fa fa-navicon"></i></button> 
         <!-- Logo --> 
         <a class="iconvrf logo navbar-brand relativesaz" href="" target="_blank"> 

          <img src="" alt="" /> 
          <span class="fontsmall"> 

          </span> 
         </a> 
        </div> 
        <!-- Menu --> 
        <div class="menu"> 
         <nav> 
          <ul> 
           <li class="active"><a href="#hero-area">Home</a></li> 
           <li><a href="#about-area">About</a></li> 
           <li><a href="#feature-area">Feature</a></li> 
           <li><a href="#pricing-area">Description</a></li> 
           <li><a href="#screenshot-area">Screenshot</a></li> 
           <li><a href="#team-area">AW </a></li> 
           <li> 
            <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown"> 
             @ViewBag.ActiveMenu 

             <span class="caret"></span> 
            </button> 
             <ul class="dropdown-menu"> 
              <li> 
               <ul> 
                <li><a class="colorblack" href="@Url.Action("ru", "Home")">Russian</a></li> 
                <li><a class="colorblack" href="@Url.Action("cn", "Home")">Chinese</a></li> 
                <li><a class="colorblack" href="@Url.Action("jp", "Home")">Japanese</a></li> 
                <li><a class="colorblack" href="@Url.Action("nl", "Home")">Dutch</a></li> 
                <li><a class="colorblack" href="@Url.Action("dk", "Home")">Danish</a></li> 
                <li><a class="colorblack" href="@Url.Action("de", "Home")">German</a></li> 
                <li><a class="colorblack" href="@Url.Action("fr", "Home")">French</a></li> 
                <li><a class="colorblack" href="@Url.Action("en", "Home")">English </a></li> 
               </ul> 
              </li> 
              <li> 
               <ul> 
                <li><a class="colorblack" href="@Url.Action("tr", "Home")">Turkish</a></li> 

                <li><a class="colorblack" href="@Url.Action("th", "Home")">Tahi</a></li> 
                <li><a class="colorblack" href="@Url.Action("sv", "Home")">Swedish</a></li> 
                <li><a class="colorblack" href="@Url.Action("pt", "Home")">Portuguese</a></li> 
                <li><a class="colorblack" href="@Url.Action("no", "Home")">Norwegian</a></li> 
                <li><a class="colorblack" href="@Url.Action("kr", "Home")">Korean</a></li> 
                <li><a class="colorblack" href="@Url.Action("es", "Home")">Spanish</a></li> 
                <li><a class="colorblack" href="@Url.Action("it", "Home")">Italian </a></li> 
               </ul> 
              </li> 
             </ul> 
           </li> 
          </ul> 
         </nav> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
    @RenderBody() 
    <script src="~/Scripts/plugins.js"></script> 
    <script src="~/Scripts/main.js"></script> 
</body> 
</html> 

回答

0

您可以直接使用resx沒有viewbag這樣的:

解決方案

佈局和兩個RESX。

Solution

_Layout.cshtml

<header> 
    <div class="content-wrapper"> 
    <div class="float-left"> 
     <p class="site-title"> 
     @if (System.Globalization.CultureInfo.CurrentCulture.ToString() == "fa-IR") 
      { 
       <a href="~/Home/">@Resource_fa_IR.App_Name</a> 
      } 
      else 
      { 
       <a href="~/Home/">@Resource.App_Name</a> 
      } 
     </p> 
    </div>   
    </div> 
</header> 

Resource.Designer.cs和Resource.fa.IR.Designer.cs

public static string App_Name { 
      get { 
       return ResourceManager.GetString("App_Name", resourceCulture); 
      } 
     } 

你應該改變internalpublic有類。