Bogo has a function bogo_http_accept_languages() that returns an array of languages that the user can accept, in the user’s preference order, but it isn’t actually used in Bogo.
You can use the function to get the most preferred language (the first one in the array). The last thing you need to do is redirecting to the translated page in the language.
But it is not so simple because it’s uncertain if there is a translation of the current page. About that, you can use bogo_language_switcher() function defined in includes/link-template.php as a reference.
- Find right hook before site is rendered
- Check if the site is the users entry point (the rewrite should not happen when the user intentionally switched the language by a link)
- If it’s the entry point: check if the current translation is the closest available in comparison with
bogo_http_accept_languages()
- If other URL found do the URL rewrite
Any quick ideas for 1, 2 (maybe using HTTP-REFERER) and 4?
I already wrote 3 for the menu switch language item.
PS: I think this would be a nice feature for Bogo to find the closest translation and automatically switch to this URL. It would improve the UX
Find right hook before site is rendered
The template_redirect action hook would fit.
Check if the site is the users entry point (the rewrite should not happen when the user intentionally switched the language by a link)
Checking the HTTP-REFERER header is the best, but HTTP-REFERER is not reliable.
If other URL found do the URL rewrite
You can use wp_safe_redirect().
I think this would be a cool feature (loading the language site in the user’s browser preferences by default). Did anyone actually try out code in their theme’s functions.php yet? I’d love to see an actual code snippet so I don’t have to figure it all out myself…