From b210fe9d76e2b0c9fb44d6ec08f3330a9d3545ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 7 Aug 2016 22:01:55 +0200 Subject: [PATCH] Improve language handling in URLs The current "rendering language" is needed outside of Site. This commit moves the Language type to the helpers package, and then used to get correct correct language configuration in the markdownify template func. This commit also adds two new template funcs: relLangURL and absLangURL. See #2309 --- content/content/multilingual.md | 2 +- content/templates/functions.md | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/content/content/multilingual.md b/content/content/multilingual.md index f93738f9f..35dd1382e 100644 --- a/content/content/multilingual.md +++ b/content/content/multilingual.md @@ -135,7 +135,7 @@ This uses a definition like this one in `i18n/en-US.yaml`: ### Multilingual Themes support -To support Multilingual mode in your themes, some considerations must be taken for the URLs in the templates. If there are more than one language, URLs must either come from the built-in `.Permalink` or `.URL`, be constructed with `relURL` or `absURL` -- or prefixed with `{{.LanguagePrefix }}`. +To support Multilingual mode in your themes, some considerations must be taken for the URLs in the templates. If there are more than one language, URLs must either come from the built-in `.Permalink` or `.URL`, be constructed with `relLangURL` or `absLangURL` template funcs -- or prefixed with `{{.LanguagePrefix }}`. If there are more than one language defined, the`LanguagePrefix` variable will equal `"/en"` (or whatever your `CurrentLanguage` is). If not enabled, it will be an empty string, so it is harmless for single-language sites. diff --git a/content/templates/functions.md b/content/templates/functions.md index aa55ced4f..5fb540065 100644 --- a/content/templates/functions.md +++ b/content/templates/functions.md @@ -787,6 +787,13 @@ e.g.: `{{ i18n "translation_id" }}` * `{{ mul 1000 (time "2016-05-28T10:30:00.00+10:00").Unix }}` → 1464395400000 (Unix time in milliseconds) ## URLs +### absLangURL, relLangURL +These are similar to the `absURL` and `relURL` relatives below, but will add the correct language prefix when the site is configured with more than one language. + +So for a site `baseURL` set to `http://mysite.com/hugo/` and the current language is `en`: + +* `{{ "blog/" | absLangURL }}` → "http://mysite.com/hugo/en/blog/" +* `{{ "blog/" | relLangURL }}` → "/hugo/en/blog/" ### absURL, relURL