0

I am using database with multiple language support. Now the problem is that I need to enter my language into query in order to get information and it is fine, but what would be optimal way to store that information.

On client side it will be stored in cookies, of course. Now only way I can think of is making global variable on class and then use it in my functions. Is that only way?

Example code

private string lang = Infrastructure.UserSettings.Language(); // I don't have this implemented yet

[HttpGet]
public dynamic List()
{
    string lang = "English"; // That's why I set it here manually for testing

    var items = _db.Items.OrderByDescending(x => x.ID).Select(x => new
    {
        ID = x.ID,
        Price = x.Price,
        Name = x.ItemTranslations.Where(y => y.Language.Name == lang).Select(y => y.Name).SingleOrDefault(),
        Category =  new {
            ID = x.Category.ID,
            Name = x.Category.CategoryTranslations.Where(y => y.Language.Name == lang).Select(y => y.Name).SingleOrDefault()
        }
    });

    return items;
}

My question: Is this good way of doing this or there is some more optimal way?

Stan
  • 25,744
  • 53
  • 164
  • 242
  • You may want to use different URL for different langs (for better SEO, sharing, etc). http://stackoverflow.com/questions/3683404/asp-net-mvc-localized-routes-and-the-default-language-for-the-user/3684864#3684864 – Eduardo Molteni Sep 06 '12 at 16:11

2 Answers2

1

You could make a base controller with a read-only variable, like so:

public class BaseController : Controller
{
    public string UserLanguage
    {
        get
        {
            var cLanguage = HttpContext.Request.Cookies["lang"];
            if (cLanguage != null)
                return cLanguage.Value;
            else
                return "English";
        }
    }
}

Then inherit your base controller, like so:

public class HomeController : BaseController

Then access your variable like so:

var items = _db.Items.OrderByDescending(x => x.ID).Select(x => new
{
    ID = x.ID,
    Price = x.Price,
    Name = x.ItemTranslations.Where(y => y.Language.Name == UserLanguage).Select(y => y.Name).SingleOrDefault(),
    Category =  new {
        ID = x.Category.ID,
        Name = x.Category.CategoryTranslations.Where(y => y.Language.Name == lang).Select(y => y.Name).SingleOrDefault()
    }
});

You would just need to set the cookie at a particular time.

Joey Gennari
  • 2,361
  • 17
  • 26
0

A cookie is sent to the server on every page request. If the setting is available in the cookie, just read the cookie when you need to do a query. There is no performance overhead for reading a cookie that is already present.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Use `HttpContext.Current.Response.Cookies[""].Value` this works everywhare if the the code is executed based on a web request. – Gerald Degeneve Sep 06 '12 at 15:45
  • Yea well basically I just create class in infrastructure that will handle that and get it like in my example? – Stan Sep 06 '12 at 15:47