PHP language detection

前端 未结 9 2233
陌清茗
陌清茗 2020-12-14 16:32

I\'m trying to build multilangual site.

I use this piece of code to detect users language. If you havent chosen a language, it will include your language file based

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 17:14

    Your code looks just fine. You might want to add a final else default choice if the visitor asks for a language you aren't providing. Also, if the visitor himself selects a language you should save that choice in a persistent cookie and check its value, giving it precedence over HTTP_ACCEPT_LANGUAGE.

    As far as I can tell Youtube does use HTTP_ACCEPT_LANGUAGE, but at the same time uses IP geolocation to suggest a change in language if the langauge of the visitor's country doesn't match that. Definitely annoying.

    Just nitpicking: if you're gonna add languages to the list a switch() statement might be more readable.

提交回复
热议问题