2.9 KiB
2.9 KiB
title, description, categories, keywords, params
| title | description | categories | keywords | params | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Language | Returns the Language object for the given page. |
|
The Language method on a Page object returns the Language object for the given page, derived from the language definition in your project configuration.
You can also use the Language method on a Site object. See details.
Methods
Use these methods on the Language object.
The examples below assume the following language definition.
{{< code-toggle file=hugo >}} [languages.de] direction = 'ltr' label = 'Deutsch' locale = 'de-DE' weight = 2 {{< /code-toggle >}}
Direction- {{< new-in 0.158.0 />}}
- (
string) Returns thedirectionfrom the language definition.{{ .Language.Direction }} → ltr IsDefault- {{< new-in 0.153.0 />}}
- (
bool) Reports whether this is the default language.{{ .Language.IsDefault }} → true Label- {{< new-in 0.158.0 />}}
- (
string) Returns thelabelfrom the language definition.{{ .Language.Label }} → Deutsch Lang- {{<deprecated-in 0.158.0 />}}
- Use
Nameinstead. LanguageCode- {{<deprecated-in 0.158.0 />}}
- Use
Localeinstead. LanguageDirection- {{<deprecated-in 0.158.0 />}}
- Use
Directioninstead. LanguageName- {{<deprecated-in 0.158.0 />}}
- Use
Labelinstead. Locale- {{< new-in 0.158.0 />}}
- (
string) Returns thelocalefrom the language definition, falling back toName.{{ .Language.Locale }} → de-DE Name- {{< new-in 0.153.0 />}}
- (
string) Returns the language tag as defined by RFC 5646. This is the lowercased key from the language definition.{{ .Language.Name }} → de Weight- {{<deprecated-in 0.158.0 />}}
Example
Use the code below to create a language selector, allowing users to navigate between the different translated versions of the current page.
{{ with .Rotate "language" }}
<nav class="language-selector">
<ul>
{{ range . }}
{{ if eq .Language $.Language }}
<li class="active">
<a aria-current="page" href="{{ .Permalink }}" hreflang="{{ .Language.Locale }}">{{ .Language.Label }}</a>
</li>
{{ else }}
<li>
<a href="{{ .Permalink }}" hreflang="{{ .Language.Locale }}">{{ .Language.Label }}</a>
</li>
{{ end }}
{{ end }}
</ul>
</nav>
{{ end }}