Debian Codegraph @

docs/content/en/functions/urls/RelLangURL.md

raw
1 ---
2 title: urls.RelLangURL
3 description: Returns a relative URL with a language prefix, if any.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: [relLangURL]
9 returnType: string
10 signatures: [urls.RelLangURL INPUT]
11 aliases: [/functions/rellangurl]
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 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 {{ relLangURL "" }} → /en/
39 {{ relLangURL "articles" }} → /en/articles
40 {{ relLangURL "style.css" }} → /en/style.css
41 {{ relLangURL "https://example.org" }} → https://example.org
42 {{ relLangURL "https://example.org/" }} → /en
43 {{ relLangURL "https://www.example.org" }} → https://www.example.org
44 {{ relLangURL "https://www.example.org/" }} → https://www.example.org/
45 ```
46
47 When rendering the `en` site with `baseURL = https://example.org/docs/`
48
49 ```go-html-template
50 {{ relLangURL "" }} → /docs/en/
51 {{ relLangURL "articles" }} → /docs/en/articles
52 {{ relLangURL "style.css" }} → /docs/en/style.css
53 {{ relLangURL "https://example.org" }} → https://example.org
54 {{ relLangURL "https://example.org/" }} → https://example.org/
55 {{ relLangURL "https://example.org/docs" }} → https://example.org/docs
56 {{ relLangURL "https://example.org/docs/" }} → /docs/en
57 {{ relLangURL "https://www.example.org" }} → https://www.example.org
58 {{ relLangURL "https://www.example.org/" }} → https://www.example.org/
59 ```
60
61 ## Input begins with a slash
62
63 If the input begins with a slash, the resulting URL will be relative to the protocol+host of the `baseURL` in your project configuration.
64
65 When rendering the `en` site with `baseURL = https://example.org/`
66
67 ```go-html-template
68 {{ relLangURL "/" }} → /en/
69 {{ relLangURL "/articles" }} → /en/articles
70 {{ relLangURL "/style.css" }} → /en/style.css
71 ```
72
73 When rendering the `en` site with `baseURL = https://example.org/docs/`
74
75 ```go-html-template
76 {{ relLangURL "/" }} → /en/
77 {{ relLangURL "/articles" }} → /en/articles
78 {{ relLangURL "/style.css" }} → /en/style.css
79 ```
80
81 > [!NOTE]
82 > 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.