| 1 |
--- |
| 2 |
title: urls.AbsLangURL |
| 3 |
description: Returns an absolute URL with a language prefix, if any. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [absLangURL] |
| 9 |
returnType: string |
| 10 |
signatures: [urls.AbsLangURL INPUT] |
| 11 |
aliases: [/functions/abslangurl] |
| 12 |
--- |
| 13 |
|
| 14 |
Use this function with both monolingual and multilingual configurations. The URL returned by this function depends on: |
| 15 |
|
| 16 |
- Whether the input begins with a slash (`/`) |
| 17 |
- The `baseURL` in your project configuration |
| 18 |
- The language prefix, if any |
| 19 |
|
| 20 |
This is the project configuration for the examples that follow: |
| 21 |
|
| 22 |
{{< code-toggle file=hugo >}} |
| 23 |
defaultContentLanguage = 'en' |
| 24 |
defaultContentLanguageInSubdir = true |
| 25 |
[languages.en] |
| 26 |
weight = 1 |
| 27 |
[languages.es] |
| 28 |
weight = 2 |
| 29 |
{{< /code-toggle >}} |
| 30 |
|
| 31 |
## Input does not begin with a slash |
| 32 |
|
| 33 |
If the input does not begin with a slash, the path in the resulting URL will be relative to the `baseURL` in your project configuration. |
| 34 |
|
| 35 |
When rendering the `en` site with `baseURL = https://example.org/` |
| 36 |
|
| 37 |
```go-html-template |
| 38 |
{{ absLangURL "" }} → https://example.org/en/ |
| 39 |
{{ absLangURL "articles" }} → https://example.org/en/articles |
| 40 |
{{ absLangURL "style.css" }} → https://example.org/en/style.css |
| 41 |
``` |
| 42 |
|
| 43 |
When rendering the `en` site with `baseURL = https://example.org/docs/` |
| 44 |
|
| 45 |
```go-html-template |
| 46 |
{{ absLangURL "" }} → https://example.org/docs/en/ |
| 47 |
{{ absLangURL "articles" }} → https://example.org/docs/en/articles |
| 48 |
{{ absLangURL "style.css" }} → https://example.org/docs/en/style.css |
| 49 |
``` |
| 50 |
|
| 51 |
## Input begins with a slash |
| 52 |
|
| 53 |
If the input begins with a slash, the path in the resulting URL will be relative to the protocol+host of the `baseURL` in your project configuration. |
| 54 |
|
| 55 |
When rendering the `en` site with `baseURL = https://example.org/` |
| 56 |
|
| 57 |
```go-html-template |
| 58 |
{{ absLangURL "/" }} → https://example.org/en/ |
| 59 |
{{ absLangURL "/articles" }} → https://example.org/en/articles |
| 60 |
{{ absLangURL "/style.css" }} → https://example.org/en/style.css |
| 61 |
``` |
| 62 |
|
| 63 |
When rendering the `en` site with `baseURL = https://example.org/docs/` |
| 64 |
|
| 65 |
```go-html-template |
| 66 |
{{ absLangURL "/" }} → https://example.org/en/ |
| 67 |
{{ absLangURL "/articles" }} → https://example.org/en/articles |
| 68 |
{{ absLangURL "/style.css" }} → https://example.org/en/style.css |
| 69 |
``` |
| 70 |
|
| 71 |
> [!NOTE] |
| 72 |
> As illustrated by the previous example, using a leading slash is rarely desirable and can lead to unexpected outcomes. In nearly all cases, omit the leading slash. |