link: change cross language URL format #850
Some checks are pending
docs-build-deployment / Run deploy (push) Waiting to run
docs-build / Run build (push) Waiting to run

This commit is contained in:
Sören Weber 2025-02-08 01:55:48 +01:00
parent 881931eb11
commit 5f9fee85c4
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
7 changed files with 86 additions and 51 deletions

View file

@ -488,6 +488,12 @@ disableDefaultRelref = false
# not be servable from the file system.
disableExplicitIndexURLs = false
# Support legacy language links.
# Default: false
# If set to true, you can link to pages in other languages by using
# `/pir/my-page`. This is not recommended, use ``/my-page?lang=pir` instead.
enableLegacyLanguageLinks = false
# How to open external links.
# Default: '_blank'
# For external links you can define how they are opened in your browser. All

View file

@ -1,7 +1,7 @@
+++
categories = ["howto"]
description = "What options are available for links and images"
options = ["disableDefaultRelref", "disableExplicitIndexURLs"]
options = ["disableDefaultRelref", "disableExplicitIndexURLs", "enableLegacyLanguageLinks"]
title = "Linking"
weight = 4
+++
@ -21,6 +21,14 @@ For the file system scenario, you are not allowed to change this value.
disableExplicitIndexURLs = true
{{< /multiconfig >}}
## Cross-Language Links
You can link to pages of different languages by appending the `lang` query parameter with the language code to the URL, e.g. `/my-page?lang=pir`.
In previous releases of the theme you had to prepend the language code to the URL, e.g. `/pir/my-page` to achieve this.
If you still need the old behavior, you can set `enableLegacyLanguageLinks=true` in your `hugo.toml`. Note that this legacy feature may be removed in the future.
## Patching the `relref` Shortcode
{{% badge style="cyan" icon="gears" title=" " %}}Option{{% /badge %}} While the usage of `relref` is obsolete and discouraged by Hugo for a while, existing installations may still use it.

View file

@ -1,7 +1,7 @@
+++
categories = ["howto"]
description = "What options are available for links and images"
options = ["disableDefaultRelref", "disableExplicitIndexURLs"]
options = ["disableDefaultRelref", "disableExplicitIndexURLs", "enableLegacyLanguageLinks"]
title = "Linking"
weight = 4
+++

View file

@ -28,6 +28,6 @@ weight = -12
}
````
- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} Support for languages that are written right to left (like Arabic) is now complete and extended to the menu, the top navigation bar and print. You can experience this in the [pirate translation](/pir). This feature is not available in Internet Explorer 11.
- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} Support for languages that are written right to left (like Arabic) is now complete and extended to the menu, the top navigation bar and print. You can experience this in the [pirate translation](/?lang=pir). This feature is not available in Internet Explorer 11.
- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} The scrollbars are now colored according to their variant color scheme to better fit into the visuals.

View file

@ -10,6 +10,14 @@ weight = -4
### Change
- {{% badge style="note" title=" " %}}Change{{% /badge %}} The way you [link to pages in a different language](configuration/content/linking#cross-language-links) has changed. Until now this was an undocumented feature.
Previously you could prepend an URL with the language code, e.g. `/pir/my-page` to link to pages of other languages. This could cause problems if you have a page with the same name as the language code on the top level of your page structure.
Now you have to set the query parameter `lang` to the language code, e.g. `/my-page?lang=pir`.
As this was previously undocumented, most projects will not be affected. If you still need the old behavior, you can set `enableLegacyLanguageLinks=true` in your `hugo.toml`.
- {{% badge style="note" title=" " %}}Change{{% /badge %}} The `target` parameter of the [`button` shortcode](shortcodes/button#parameter) was deprecated.
Use [link effects](authoring/markdown#link-effects) on the `href` parameter instead. You don't need to change anything as the old parameter still works but may generate warnings.

View file

@ -1,48 +1,61 @@
{{- $page := .page }}
{{- $linkObject := "" }}
{{- $u := urls.Parse .url }}
{{- $path := strings.TrimPrefix "./" $u.Path }}
{{- $searchLocal := .searchLocal | default true }}
{{- $searchGlobal := .searchGlobal | default true }}
{{- $searchPage := .searchPage | default true }}
{{- $searchResource := .searchResource | default true }}
{{- if $path }}
{{- with or
(and $searchLocal $searchPage ($page.Page.GetPage $path))
(and $searchLocal $searchPage ($page.Page.GetPage (strings.TrimRight "/" $path)))
(and $searchGlobal $searchPage (site.GetPage $path))
(and $searchGlobal $searchPage (site.GetPage (strings.TrimRight "/" $path)))
(and $searchLocal $searchResource ($page.Page.Resources.Get $path))
(and $searchGlobal $searchResource (resources.Get $path))
}}
{{- $linkObject = . }}
{{- else }}
{{- /* is it a link into another translation? */}}
{{- if strings.HasPrefix $path "/" }}
{{- range $page.AllTranslations }}
{{- $lang := .Language.Lang }}
{{- $prefix := printf "/%s" $lang }}
{{- $suffix := strings.TrimPrefix $prefix $path | default "/" }}
{{- /* with the second check we check if the prefix was finished;
eg. /pir/index.html vs. /pirate/index.html, were the latter is
an external address outside of this site */}}
{{- if and (strings.HasPrefix $path $prefix) (strings.HasPrefix $suffix "/") }}
{{- with or
(and $searchLocal $searchPage (.GetPage $suffix))
(and $searchLocal $searchPage (.GetPage (strings.TrimRight "/" $suffix)))
(and $searchGlobal $searchPage (site.GetPage $path))
(and $searchGlobal $searchPage (site.GetPage (strings.TrimRight "/" $path)))
(and $searchLocal $searchResource (.Resources.Get $suffix))
(and $searchGlobal $searchResource (resources.Get $suffix))
}}
{{- $linkObject = . }}
{{- break }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- else }}
{{- $linkObject = $page.Page }}
{{- end }}
{{- $linkObject := "" }}
{{- $u := urls.Parse .url }}
{{- $path := strings.TrimPrefix "./" $u.Path }}
{{- $searchLocal := .searchLocal | default true }}
{{- $searchGlobal := .searchGlobal | default true }}
{{- $searchPage := .searchPage | default true }}
{{- $searchResource := .searchResource | default true }}
{{- $site := .page.Site }}
{{- $page := .page }}
{{- $transSite := "" }}
{{- $transPage := "" }}
{{- $lang := $u.Query.Get "lang" }}
{{- if and $site.Params.enableLegacyLanguageLinks (not $lang) }}
{{- /* is it a legacy link into another translation? */}}
{{- if strings.HasPrefix $path "/" }}
{{- range $site.Sites }}
{{- $lang = .Language.Lang }}
{{- $prefix := printf "/%s" $lang }}
{{- $suffix := strings.TrimPrefix $prefix $path | default "/" }}
{{- /* with the second check we check if the prefix was finished;
eg. /pir/index.html vs. /pirate/index.html, were the latter is
an external address outside of this site */}}
{{- if and (strings.HasPrefix $path $prefix) (strings.HasPrefix $suffix "/") }}
{{- $path = $suffix }}
{{- $transSite = . }}
{{- break }}
{{- end }}
{{- end }}
{{- end }}
{{- if not $transSite }}
{{ $lang = $site.Language.Lang }}
{{ $transSite = $site }}
{{- end }}
{{- else if $lang }}
{{- with where $site.Sites "Language.Lang" "eq" $lang }}
{{- with index . 0 }}
{{- $transSite = . }}
{{- end }}
{{- end }}
{{- else }}
{{ $lang = $site.Language.Lang }}
{{ $transSite = $site }}
{{- end }}
{{- if $transSite }}
{{- $transPage = $transSite.GetPage $page.Path }}
{{- if $path }}
{{- with or
(and $searchLocal $searchPage $transPage ($transPage.GetPage $path))
(and $searchLocal $searchPage $transPage ($transPage.GetPage (strings.TrimRight "/" $path)))
(and $searchGlobal $searchPage ($transSite.GetPage $path))
(and $searchGlobal $searchPage ($transSite.GetPage (strings.TrimRight "/" $path)))
(and $searchLocal $searchResource $transPage ($transPage.Resources.Get $path))
(and $searchGlobal $searchResource (resources.Get $path))
}}
{{- $linkObject = . }}
{{- end }}
{{- else if $searchPage }}
{{- $linkObject = $transPage }}
{{- end }}
{{- end }}
{{- return $linkObject }}

View file

@ -1 +1 @@
7.3.2+109d094dc2ee4e311e20da415bc8d18c1424dc75
7.3.2+881931eb115fdf80beeaa72bb39825805d36a77c