This is the third and final article in a series of blog posts about the Best Practices for Sitecore Internationalization. During this third installment, we’ll focus on two main topics: identifying visitors’ country and language and how to generate SEO-friendly URLs.
Identifying Visitors’ Country & Language
You may need to identify which locale is suitable for a visitor. In other words, depending on the location of the user, you may want to display a specific country/language version of your website.
There are multiple ways to solve this, which are identified below.
The Accept-Language HTTP header is provided by the browser when a user requests a document. The value sent in this header is defined by BCP 47, typically as a two or three letter code (ex: de for German).
This header is a good starting point to determine a user’s locale, but it still has some limitations:
- Inferred locale is not appropriate: When installed, browsers determine the default value from this based on some settings in the device. These settings can still be wrong and cause a wrong locale to be assumed.
- Region is not specified: Sometimes the browser can define a language, but not the region. For example, you may have configured es (for Spanish) by default, but this doesn’t reflect if it’s Spanish for Spain (es-ES) or Argentina (es-AR).
There are some posts available that explain how to create a processor that inherits from LanguageResolver class. This processor can be added to the httpRequestBegin pipeline. The behavior is used to detect the value in this header and then redirect the request to a specific locale in Sitecore. A good example of this can be found here.
Another option to determine a user’s location is the GeoIP lookup. Based on the user’s IP address, you can determine the location of the user and, based on that, infer the language. There are different options out there. One of them is the Sitecore IP Geolocation Service, which is a paid service and has official support from Sitecore. Another option is the Free GeoLite2 databases by MaxMind, which is a free service and needs to be implemented and maintained yourself.
The default behavior of Sitecore LanguageResolver consists of using the querystring sc_lang=en or the URL path /en/ to determine the language. When specifying a language in the URL, a cookie on the user’s side is set and it keeps record of the language. So, if you hit any other URL without a language code specified on it, Sitecore will use this cookie to determine which language to display. This is, sometimes, the best practice to follow, keep the LanguageResolver as is and add a language dropdown to your website to let users pick the language they want.
The simplest way of generating SEO-friendly URLs is by using a combination of settings in the Sitecore LinkManager. Some of the recommended settings are:
- addAspxExtension=”false”: this one removes the extension at the end of the URLs.
- alwaysIncludeServerUrl=”false”: this one generates absolute URLs.
- encodeNames=”true”: this option will encode the names of items when displaying them in the URLs.
- languageEmbedding=”always”: this will always include the language in the URLs.
- languageLocation=”filePath”: this will locate the language code in the path of the URL (not in the querystring).
- lowercaseUrls=”true”: this will always generate lower case URLs.
- shortenUrls=”true”: this will create shortened version of URLs.
- useDisplayName=”true”: this will use the display name of an item to create the URL.