2017-08-08 146 views
-1

我正在ASP.NET MVC中构建一个简单的APP。任何人都可以为我提供任何帮助,如何做多语言网站,在会话中存储语言,使用会话,链接,帮助或教程,在服务器端更改语言,我在该CMS发布新闻,所以当我按下英语时,它应该显示我的英文,西班牙文等文本..Asp.net多语言网络应用程序

+0

使用代码项目https://www.codeproject.com/Articles/526827/MVC-Basic-Site-Step-Multilingual-Site-Skeleton对多语言有基本的了解,然后根据您的要求探索先进的功能 –

+0

我需要使用会话并与控制器通信。 – ttgg

回答

0

(这是在VB中完成,但如果C#是期望的代码转换器是可用的,但原则是相同的) 这可以用一个带有这样的结构的xml文件。

<globalization> 
    <!-- the lang attribute is a made up attribute. it helps with search 
     further on--> 
    <content lang="fr"> 
     <title>je suis un titre</title> 
     <text>et ceci est un paragraphe</title> 
    </content> 
    <content lang="en"> 
     <title>this is a title</title> 
     <text>and this is a paragraph</title> 
    </content> 
</globalization> 

,所有你需要的是做的就是在你的后台更改HTML取决于所选的语言... HTML的 例如:后端的

<body> 
    <asp:button runat="server" id="btnLangFr" text="Fr"/> 
    <asp:button runat="server" id="btnLangEn" text="En"/> 
    <asp:label runat="server" id="lblTitle" /> 
    <asp:label runat="server" id="lblParagraph" /> 
<body> 

例如:

Protected Sub btnLangEn(sender As Object, e As EventArgs) handles btnLangEn.click 
    Dim xmlDoc As XmlDocument = New XmlDocument() //defines xmldoc 
    xmlDoc.Load(Server.MapPath("globalization.xml")) //gets .xml file 
    Dim root As XmlElement = xmlDoc.DocumentElement //defines root 
    'next line sets a list of all the <content> tags 
    Dim elemList As XmlNodeList = root.GetElementsByTagName("lang") 
    'insert code to change the <asp:label> tags to be equal to the text 
    'between the corresponding xml tags 
End Sub 

要获得正确的content标记,您需要创建一个循环,查找正确的lang属性

一旦找到了,将标签保存在一个变量中(例如, Dim ActiveLang as xmlElement = root.ChildNodes.ItemOf(index of loop)

那么你就可以得到像这样所需标签的文本.. activeLang.GetElementsByTagName("title")(0).InnerText 并行代码是这样的:

lblTitle.text = activeLang.GetElementsByTagName("title")(0).InnerText 
+0

你介意重新格式化你的问题,所以水平滚动条会消失吗? – reporter

+0

我需要使用会话,然后与控制器进行通信。 – ttgg

+0

哦,我的道歉,我不是所有熟悉mvc的人,如果那是你所说的 – Alphanot