Skip to content

Commit

Permalink
[i18n] Initial multilingual support for redirects with ja example
Browse files Browse the repository at this point in the history
  • Loading branch information
chalin committed Jun 14, 2024
1 parent 3b44fbf commit a908332
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
11 changes: 11 additions & 0 deletions content/ja/docs/specs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Specifications
linkTitle: Specs ↗
description: _Redirect page_
weight: 960
# _build: { render: link }
redirect: /docs/specs # TODO: this will need to be /en/...
redirects: [{ from: '*', to: ':splat' }]
---

Netlify redirect target: [{{% param "title" %}}]({{% param "redirect" %}}).
4 changes: 4 additions & 0 deletions layouts/index.redirects
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@
{{ end }}
{{ return $result }}
{{ end }}

{{/* Multilingual support */ -}}

{{ partial "redirects/sites.redirects" . | partial "func/trim-lines.html" -}}
3 changes: 3 additions & 0 deletions layouts/partials/func/trim-lines.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ range split . "\n" -}}
{{ trim . " \t" }}
{{ end -}}
39 changes: 39 additions & 0 deletions layouts/partials/redirects/sites.redirects
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{{/* Redirect for default language when .LanguagePrefix is empty. */ -}}

{{ $defaultLang := "" -}}
{{ with .Site.Sites.Default -}}
{{ if eq .LanguagePrefix "" -}}
{{ with .Language.Lang -}}
{{ $defaultLang = . -}}
/{{ . }} /
/{{ . }}/* /:splat
{{ end -}}
{{ end -}}
{{ end -}}

{{/* Process non-default languages. */ -}}

{{ range after 1 .Sites -}}

{{ $siteLang := .Language.Lang -}}

# Site localization {{ $siteLang }}
{{ range $p := .Pages -}}

{{ range $p.Params.redirects -}}
{{ $fallbackPage := partial "i18n/fallback-page.html" $p -}}
{{ if or (eq $siteLang $defaultLang) (not $fallbackPage) -}}
{{ $from := cond (strings.HasPrefix .from "/")
.from
(print $p.RelPermalink .from) -}}
{{ $to := cond (strings.HasPrefix .to "/")
.to
(print $p.RelPermalink .to) -}}
{{ $from | printf "%-35s" }} {{ $to }}
{{ else -}}
{{/* # {{ $p.RelPermalink }} */ -}}
{{ end -}}
{{ end -}}
{{ end -}}

{{ end -}}

0 comments on commit a908332

Please sign in to comment.