開源日報 每天推薦一個 GitHub 優質開源項目和一篇精選英文科技或編程文章原文,堅持閱讀《開源日報》,保持每日學習的好習慣。
今日推薦開源項目:《兩手準備 degoogle》
今日推薦英文原文:《How to Pick the Right Language to Display on Your Website》
開源日報第693期:《兩手準備 degoogle》
今日推薦開源項目:《兩手準備 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: 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/