How can I determine the preferred language of the website visitor?

The recommended method is to use the Accept-Language HTTP header to get the visitor’s language preferences.

As a fallback, you can use the GeoIP2 Country database to first determine the country, then select the language based on the country returned from the database. You should always use the Accept-Language header if it is present. For example, a Canadian living in Japan may have a Japanese IP address, but their browser will probably be configured to request English.