开源日报 每天推荐一个 GitHub 优质开源项目和一篇精选英文科技或编程文章原文,坚持阅读《开源日报》,保持每日学习的好习惯。
今日推荐开源项目:《两手准备 degoogle》
今日推荐英文原文:《How to Pick the Right Language to Display on Your Website》

今日推荐开源项目:《两手准备 degoogle》传送门:GitHub链接
推荐理由:不要把所有的鸡蛋放在一个篮子里,为日常使用的各种工具准备替代品能让你不会在出错时手忙脚乱——也可以让你的选择不会被限制。这个项目收集了一系列能够代替 Google 服务的替代品,不管是希望在 Google 坏掉的时候寻找 Plan B,还是不再信任 Google 而希望寻找新服务,这都能派上用场。
今日推荐英文原文:《How to Pick the Right Language to Display on Your Website》作者:Tom Feron
原文链接:https://medium.com/better-programming/how-to-pick-the-right-language-to-display-on-your-website-37ec0338a659
推荐理由:大道至简,比起自己猜要显示什么语言,还是让用户自己选择语言更快

How to Pick the Right Language to Display on Your Website

Localisation on the web can be frustrating sometimes. Sadly, this is a problem you only encounter if you travel, you live abroad, or if you live in a multi-lingual country. That covers a lot of people but not necessarily those in charge of a web project.

It seems that the default choice made is to select a language based on the country the user is in — this, in turn, is based on their IP address. Even though this works if you’re an English-speaker living in Palo Alto, it fails miserably in a lot of other cases.

Indeed, it relies on the assumption that countries have one language and all people in said country speak that language as their primary language (or at all). This is utterly wrong. For example, in the U.S., only about 80% of the population uses English as their first language and about 13% uses Spanish instead. In Belgium, we have three official languages, namely Dutch (55% of the population), French (38%), and German (0.4%).

Not only does the default choice gives wrong answers, but it’s also overly complicated. In order to implement it, you would probably have to integrate with an external API, making your application more complex than it has to be and maybe less responsive. All of that for a suboptimal solution.

HTTP Has You Covered

The HTTP protocol specifies the header Accept-Language for that specific purpose. With every request, the browser sends a list of languages with optional weights representing the preferences of the user.

Here are some examples from MDN’s documentation:
Accept-Language: de
Accept-Language: de-CH
Accept-Language: en-US,en;q=0.5
By default, browsers send the language of their graphical interface, which is quite likely to be the user’s preferred language.

It’s not a silver bullet, though, and should be used as a last resort when we have no other ways of knowing the user’s preference in this particular instance. For example, my browser is in English but I would rather see the website in French, my mother tongue, when I log in to the Belgian tax website.

So how else could we know what language to choose?

Explicit and Implicit Choices

Of course, if the user made an explicit choice by picking a language in a menu or in their settings if they’re logged in, then this must be the language they’re shown. But there are also implicit choices.

Most of the time, traffic is coming from somewhere else rather than having the visitor type the URL in the address bar. This can give you a clue as to the language preferred by the user. If I Google something and see a result in English, I want to see the English version when I click on the link.

Some websites hide the language in a cookie or in the session. It’s better to put it straight into the URL. That way, any link already contains an implicit choice. Search results will lead to the correct version and so will links on social media or any other pages.

Fallback Strategy

OK, but maybe someone actually types the domain name in the address bar and has changed the browser’s settings to send Accept-Language: * or something like that. What do you do then?

It might be tempting to guess based on IP geolocation, but there is another dead-simple solution. So simple and obvious, in fact, that I don’t understand why more websites don’t use it.

Just ask.

You might not know what language the user speaks and might not be able to make a good guess, but the user knows.

After all this, there is still a possibility that you made the wrong choice. Make it easy for users to change the language. It’s not rare to see websites where it’s genuinely difficult to find the language menu. Most often it’s because the menu is written in the current language (which the user might not speak) without any visual cue. Use flags or some icon that can be understood by everyone.

In summary, a more reasonable way of picking a language goes like this:
  • If the user has made another explicit choice, use it. Try putting the language in the URL for any page that might be indexed by search engines or shared.
  • If there is an implicit choice — e.g., from the URL — use that language.
  • Otherwise, use Accept-Language.
  • If none of this works, just ask the user.

下载开源日报APP:https://openingsource.org/2579/
加入我们:https://openingsource.org/about/join/
关注我们:https://openingsource.org/about/love/