Introduction

In my previous post I showed you how to keep the culture in a cookie and read from it and set the Context.Culture in each request. So after I wrote that the very handsome Jonathan Channon told me not to because Nancy has this already built in.

The code

Simple. First you remove the bootstrapper file I had in the previous post. And then you set the CurrentCulture cookie.

Imports System.Globalization
Imports Nancy

Namespace Modules
    Public Class HomeModule
        Inherits NancyModule

        Public Sub New()
            [Get]("/") = Function(parameters)
                             Return View("Home")
                         End Function
            [Get]("/{locale}") = Function(parameters)
                                     Try
                                         Context.Culture = New CultureInfo(parameters.locale.ToString)
                                     Catch ex As Exception
                                         Context.Culture = New CultureInfo("en-US")
                                     End Try
                                     Return View("Home").WithCookie(New Cookies.NancyCookie("CurrentCulture", Context.Culture.Name))
                                 End Function
        End Sub

    End Class
End Namespace

Nancy reads this cookie and will set the culture from that by default.

You can see how that work in code on the github repo.

Conclusion

There is always a better way that I don’t know about.