简体   繁体   中英

How a server get to know the incoming request from a specific region to serve a web page in the locale language

服务器如何知道来自特定区域的传入请求,以使用该特定区域的语言环境语言提供网页。

The server looks at the Accept-Language header on the incoming HTTP request. The value of the header indicates the language and/or country that the user expects to be applied in the response. For example:

Accept-Language: en-us,fr;q=0.5

There may some esoteric techniques for determining location from IP address, but these are not generally needed.

Although I agree that using Accept-Language is the easiest technique I believe this is not exactly what OP needs since he is asking about region, ie geo location.

The problem with Accept-Language is that it can contain a list of supported languages according to languages installed on the system. Moreover the default (the first) language does not always mean that this is the language you want to use to present your web content.

Decision made on geo location basic is very popular. It is based on IP address of client browser. You can obtain the remote address from HttpRequest by invoking getRemoteAddress() or getRemoteHost() .

The next problem is to distinguish the user's country. Use one of available solutions. Search "geolocation by ip api" and choose one. There are hosted services with rest api and stand-alone solution that provide you a database of IP addresses and often API to search in this database.

The next problem is to determine language from country. I do not know ready solution for this. You need to create DB that maps country to a list of languages used there. Do not forget about fallback if your application still does not support required language.

To chose among multiple languages you indeed can use Accept-Language HTTP header. For example there are 2 official languages in Canada: EN and FR. If Accept-Language looks like

Accept-Language: en-us,fr;q=0.5

choose English for canadian user. If header looks like

Accept-Language: fr,en;q=0.5

choose French. However if header looks like

Accept-Language: de,el;q=0.5

choose default (that is probably English in most cases).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM