2017-08-08 109 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