Skip to content

Commit

Permalink
[i18n] Initial multilingual support for redirects with ja example (#4687
Browse files Browse the repository at this point in the history
)
  • Loading branch information
chalin authored Jun 14, 2024
1 parent c33dfdb commit 499eab8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .htmltest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ IgnoreURLs: # list of regexs of paths or URLs to be ignored
- ^https://wikipedia.org/wiki/(S.M.A.R.T|Hop_)
# TODO move into content/en/blog/2023/contributing-to-otel/index.md once https://github.com/open-telemetry/opentelemetry.io/issues/3889 is implemented
- ^https://shorturl.at/vLYZ0$
# TODO remove the following temporary ignore rule (@chalin)
- ^/en/docs/specs$
12 changes: 12 additions & 0 deletions content/ja/docs/specs/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: Specifications
linkTitle: Specs ↗
description: _Redirect page_
weight: 960
# _build: { render: link }
redirect: /en/docs/specs 301!
redirects: [{ from: '*', to: '/en/docs/specs/:splat' }]
default_lang_commit: 3b44fbfa49ced919daea01123abfaed836d2d0ec
---

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 -}}
41 changes: 41 additions & 0 deletions layouts/partials/redirects/sites.redirects
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{{/* 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. */ -}}

/ja/docs/specs /en/docs/specs/ 301!

{{ 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 499eab8

Please sign in to comment.