I have a multilangual website running with PHP/Symfony2.

I would like to change the html lang attribute when the user switches language (= switching locale).

What is the best way to achieve this?

PS: my ultimate goal is to be able to switch font for different languages (chinese looks just too bad with the font I chose for english). I am thinking of using the CSS :lang() pseudo selector: html:lang(zh)

有帮助吗?

解决方案

Asuming you are using html5 and twig as template engine:

<!doctype html>

<html lang="{{ app.request.locale }}">

...

其他提示

The recommended way of storing locales is language code underscore country code, e.g. en_GB.

The term locale refers roughly to the user's language and country. It can be any string that your application uses to manage translations and other format differences (e.g. currency format). The ISO 639-1 language code, an underscore (_), then the ISO 3166-1 alpha-2 country code (e.g. fr_FR for French/France) is recommended. http://symfony.com/doc/current/book/translation.html

Therefore, a safe way of using the locale in the lang attribute would be:

<html lang="{{ app.request.locale|split('_')[0] }}">

This works with both an en_GB locale and an en locale.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top