Multi-language routes in express.js?

后端 未结 5 1536
终归单人心
终归单人心 2020-12-29 07:30

I\'m wondering if there is a best practise example on how to implement multi-lanuage routes in express.js. i want to use the accept-language header to get the b

5条回答
  •  再見小時候
    2020-12-29 07:50

    I'd just serve up the content in the detected language directly.

    For example, example.com/home serves up the home page in the best available Accept-Language (possibly overridden by cookie if you provide a language selection option on the site itself).

    You'd want to make sure that your response's Vary: header includes Accept-Language.

    IMO, including language codes in the URI is an ugly hack. The RFC's intent is that a single resource (your home page) is universally represented by a single URI. The entity returned for a URI can vary based on other information, such as language preferences.

    Consider what happens when a German-speaking user copies a URL and sends it to an English-speaking user. That recipient would prefer to see your site in English, but because he has received a link that points to example.com/de/startseite, he goes straight to the German version.

    Obviously, this isn't ideal for full internationalization of what the user sees in the address bar (since home is English), but it's more in line with the RFCs' intent, and I'd argue it works better for users, especially as links get spread around email/social/whatever.

提交回复
热议问题