多语言.NET cms [英] Multilanguage .NET cms

查看:129
本文介绍了多语言.NET cms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我尝试过的事情:



I am building a simple CMS in ASP.NET MVC. Can anyone provide me any help how to do it as multilanguage site , link or tutorial , to change the language in server side , i post news in that cms , so when i press englisht , it should show me the text in english , spanish and so on..

What I have tried:

I am building a simple CMS in ASP.NET MVC. Can anyone provide me any help how to do it as multilanguage site , to change the language in server side , i post news in that cms , so when i press englisht , it should show me the text in english , spanish and so on.. 

推荐答案

你需要采取一种完全不同的方法,一种更灵活的方法。这是高级示例代码,但应该让您知道您需要做什么。您应该只获取具有该网站当前配置的语言的新闻对象,而不是在您的新闻对象中获取所有可能的语言版本。您一次只能显示一种语言,那么为什么要回复所有语言内容?另外,你的方式使得从解决方案中添加或删除语言变得更加困难。



所以这里有两件事......处理当前语言,然后处理如何用这种语言获取新闻。通过将两者分开,这意味着您可以在其他地方重复使用您的语言选择,它与您的新闻代码没有紧密联系。



我将使用一个视图一切都是为了简单。所以



/指数



将显示找不到文章,但是



/ Index / 1



将以所选语言显示新闻第1项



我们将首先进行语言处理,并将其存储在cookie中,因为它是网站的最佳解决方案。因此,我们将定义一个满足我们语言选择的服务接口和一个使用cookie实现它的具体接口。



You need to take a completely different approach, one that is more flexible. This is high-level sample code but should give you an idea what you need to do. Rather than getting all possible language versions in your news object you should just get a news object that has the language the site is currently configured for. You can only show one language at a time so why get all the language content back? Also your way makes it harder to add or remove languages from your solution.

So there are two things here...handling what the current language is, and then handling how to get the news in that language. By separating the two it means you can re-use your language selection elsewhere, it's not tightly coupled to your news code.

I'm going to use one view for everything which is done for simplicity. So

/Index

will show "Article not found", but

/Index/1

will show news item 1 in the chosen language

We'll do the language handling first, and that will be stored in a cookie as it's the best solution for a website. So we'll define a service interface that satisfies our language selection and a concrete interface that implements it using cookies.

public interface ILanguageService
{
    string GetLanguage();
    void SetLanguage(string lang);
}







public class CookieLanguageService : ILanguageService
{
    public string GetLanguage()
    {
        HttpCookie c = System.Web.HttpContext.Current.Request.Cookies["lang"];
        string lang = c == null ? string.Empty : System.Web.HttpContext.Current.Request.Cookies["lang"].Value;

        if (string.IsNullOrWhiteSpace(lang))
        {
            // default to UK English (the best language in the world) if no language is set
            lang = "en-GB";
        }

        return lang;
    }

    public void SetLanguage(string lang)
    {
        HttpCookie c = new HttpCookie("lang", lang);
        c.Expires = DateTime.Now.AddYears(1);

        System.Web.HttpContext.Current.Response.Cookies.Add(c);
    }
}





这种架构意味着如果你想在其他地方存储你的语言,你可以实现一个适用于您指定存储介质的ILanguageService(例如,您可能希望使用会话,您可能希望从登录用户的设置等驱动它)。



模型是这样的(我只处理标题,你需要为LongDescription,ID等添加属性)





This kind of architecture means if you want to store your language elsewhere you can implement an ILanguageService for your given storage medium (eg you might want to use the Session, you might want to drive it from the settings of the logged-in user etc).

The model is like this (I'm only handling the Title, you'll need to add properties for the LongDescription, ID etc)

public class News
{
    public string Title { get; set; }
}





我们的控制器看起来像





Our controller is going to look like

public class HomeController : Controller
{
    private ILanguageService languageService;

    public HomeController()
        : this(new CookieLanguageService())
    {
        // if you have dependency injection set up you can omit this constructor
        // and allow the DI to handle the creation of your services
    }

    public HomeController(ILanguageService languageService)
    {
        this.languageService = languageService;
    }

    [HttpGet]
    public ActionResult Index(int? id)
    {
        // we'll flesh this action out later
        News m = new News();

        return View(m);
    }

    public ActionResult SetLanguage(string lang)
    {
        this.languageService.SetLanguage(lang);

        return RedirectToAction("Index");
    }
}





视图 - 这里我使用链接选择语言,你可能会想要使用下拉列表或其他东西





The view - here I am using links to select the language, you'll probably want to use a dropdown or something

@model MyProject.Models.News
<div>
    @Html.ActionLink("English", "SetLanguage", "Home", new { lang = "en-GB" }, null) |
    @Html.ActionLink("French", "SetLanguage", "Home", new { lang = "fr-FR" }, null)
</div>
<div>
    <h1>@Model.Title</h1>
</div>





处理语言选择。现在我们需要一个新闻服务,它将根据文章的ID和当前选择的语言从您的数据库中获取给定的新闻文章。所以服务定义





That handles the language selection. Now we need a news service that will get a given news article from your database based on the id of the article and the currently selected language. So the service definition

public interface INewsService
{
    News GetNews(int id);
}





对于实现我正在做新闻标题的硬编码,你显然会得到这个来自您的数据库。





For the implementation I'm doing some hard-coding of the news title, you'll obviously get this from your database.

public class NewsService : INewsService
{
    private ILanguageService languageService;

    public NewsService(ILanguageService languageService)
    {
        this.languageService = languageService;
    }

    public News GetNews(int id)
    {
        // get the current language
        string lang = this.languageService.GetLanguage();

        // get the news item for that language from your database
        // I am hard-coding this data for simplicity

        News news = new News();

        news.Title = string.Format("Article {0} in {1}", id.ToString(), lang);

        return news;
    }
}





现在我们已经实施了语言选择和新闻服务,我们可以充实我们的控制器行动;





Now we have implemented our language selection and our news service we can flesh out our controller action;

[HttpGet]
public ActionResult Index(int? id)
{
    News m;

    // my action allows for the id to be provided or not, in reality you'll
    // have a dedicated "news" page that must have an id supplied or else
    // you'll redirect somewhere

    if (id.HasValue)
    {
        // if you have dependency injection set up you'd treat the INewsService the
        // same way you do ILanguageService.  As INewsService needs an instance of
        // ILanguageService I can creating an instance of NewsService here rather
        // than letting DI handle the creation

        INewsService ns = new NewsService(this.languageService);

        m = ns.GetNews(id.Value);
    }
    else
    {
        m = new News();
        m.Title = "Article not found";
    }

    return View(m);
}





我编写的代码使得它按原样工作,但是你通常会利用依赖注射剂。我添加了评论,以显示DI将使代码更简单,如果你不使用DI,那么我建议你调查一下(这是一个全新的话题)。



我正在使用语言代码来定义语言,但如果您的系统使用ID或其他机制,则根据需要进行更改。如您所见,您编写的任何服务也可以使用给定ILanguageService的注入,以便它可以计算出当前语言。



I have written the code such that it works as-is, but in a way that you normally leverage dependency injection for. I've added comments to show where DI will make the code a little simpler, if you're not using DI then I'd advise you look into it (it's a whole new topic on its own).

I'm using the language code to define language, but if your system uses ids or some other mechanism then change as necessary. As you can see, any service you then write can also use the injection of the given ILanguageService so that it can work out the current language.


这篇关于多语言.NET cms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆