-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Support language redirection for any docs pages #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| --- | ||
| /** | ||
| * 言語切り替えレイアウト - 言語検出とリダイレクト | ||
| * | ||
| * Accept-Languageヘッダーを確認し、適切な言語ページにリダイレクト。 | ||
| * SSRが必要な場合はクライアントサイドで処理。 | ||
| */ | ||
|
|
||
| interface Props { | ||
| targetPath: string; | ||
| availableLanguages?: ('en' | 'ja')[]; | ||
| defaultLanguage?: 'en' | 'ja'; | ||
| } | ||
|
|
||
| const { | ||
| targetPath, | ||
| availableLanguages = ['en', 'ja'], | ||
| defaultLanguage = 'en', | ||
| } = Astro.props; | ||
| --- | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Vivliostyle Documentation</title> | ||
| <meta name="robots" content="noindex"> | ||
| <link rel="canonical" href={`${Astro.url.origin}/${defaultLanguage}${targetPath}`}> | ||
| <link rel="stylesheet" href="/styles/global.css"> | ||
| <script data-target-path={targetPath} data-supported-languages={availableLanguages.join(',')} data-default-language={defaultLanguage}> | ||
| // 言語検出とリダイレクト | ||
| (function() { | ||
| const supportedLangs = document.currentScript.getAttribute('data-supported-languages').split(','); | ||
| const defaultLang = document.currentScript.getAttribute('data-default-language'); | ||
|
|
||
| // ブラウザの言語設定を取得 | ||
| const browserLang = navigator.language || ''; | ||
| const langCode = browserLang.split('-')[0].toLowerCase(); | ||
|
|
||
| // サポートされている言語かチェック | ||
| const targetLang = supportedLangs.includes(langCode) ? langCode : defaultLang; | ||
|
|
||
| // リダイレクト | ||
| const targetPath = document.currentScript.getAttribute('data-target-path'); | ||
| window.location.replace(`/${targetLang}${targetPath}`); | ||
| })(); | ||
| </script> | ||
| <noscript> | ||
| <meta http-equiv="refresh" content={`0; url=/${defaultLanguage}${targetPath}`}> | ||
| </noscript> | ||
| </head> | ||
| <body> | ||
| <noscript> | ||
| <meta http-equiv="refresh" content={`0; url=/${defaultLanguage}${targetPath}`}> | ||
| </noscript> | ||
| <p style="font-size: 0">Welcome to the documentation. Please navigate to <a href={`/${defaultLanguage}${targetPath}`}>English documentation</a>.</p> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| --- | ||
| import LanguageSwitchLayout from '../layouts/LanguageSwitchLayout.astro'; | ||
|
|
||
| export async function getStaticPaths() { | ||
| const languages = ['en', 'ja'] as const; | ||
| const pageComponents = Object.entries( | ||
| import.meta.glob<{ | ||
| getStaticPaths?: () => Promise< | ||
| { params: { slug: string | undefined } }[] | ||
| >; | ||
| }>('./**/*.astro', { eager: true }) | ||
| ).flatMap((it) => { | ||
| const [key, value] = it; | ||
| const { getStaticPaths } = value; | ||
| const matched = /^\.\/(\w+)\/(.+\/)[^/]+\.astro$/.exec(key); | ||
| if (!getStaticPaths || !matched) { | ||
| return []; | ||
| } | ||
| const [, language, path] = matched; | ||
| if (!languages.includes(language as (typeof languages)[number])) { | ||
| return []; | ||
| } | ||
| return [ | ||
| { | ||
| getStaticPaths, | ||
| language: language as (typeof languages)[number], | ||
| path, | ||
| } as const, | ||
| ]; | ||
| }); | ||
|
|
||
| const pages = ( | ||
| await Promise.all( | ||
| pageComponents.map(async ({ getStaticPaths, ...rest }) => ({ | ||
| ...rest, | ||
| staticPaths: await getStaticPaths(), | ||
| })) | ||
| ) | ||
| ).flatMap(({ staticPaths, path, language }) => | ||
| staticPaths.map(({ params }) => ({ | ||
| language, | ||
| slug: `${path}${params.slug ?? ''}`, | ||
| })) | ||
| ); | ||
| const allSlugs = new Set(pages.map(({ slug }) => slug)); | ||
| return Array.from(allSlugs).map((slug) => { | ||
| const availableLanguages = pages.flatMap((page) => | ||
| page.slug === slug ? [page.language] : [] | ||
| ); | ||
| return { | ||
| params: { slug }, | ||
| props: { | ||
| availableLanguages, | ||
| defaultLanguage: availableLanguages.includes('en') | ||
| ? 'en' | ||
| : availableLanguages[0], | ||
| }, | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| const { availableLanguages, defaultLanguage } = Astro.props; | ||
| --- | ||
|
|
||
| <LanguageSwitchLayout | ||
| targetPath={`/${Astro.params.slug}/`} | ||
| availableLanguages={availableLanguages} | ||
| defaultLanguage={defaultLanguage} | ||
| /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,5 @@ | ||
| --- | ||
| /** | ||
| * ルートページ - 言語検出とリダイレクト | ||
| * | ||
| * Accept-Languageヘッダーを確認し、適切な言語ページにリダイレクト。 | ||
| * SSRが必要な場合はクライアントサイドで処理。 | ||
| */ | ||
| import LanguageSwitchLayout from '../layouts/LanguageSwitchLayout.astro'; | ||
| --- | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Vivliostyle Documentation</title> | ||
| <script> | ||
| // 言語検出とリダイレクト | ||
| (function() { | ||
| const supportedLangs = ['en', 'ja']; | ||
| const defaultLang = 'en'; | ||
|
|
||
| // ブラウザの言語設定を取得 | ||
| const browserLang = navigator.language || ''; | ||
| const langCode = browserLang.split('-')[0].toLowerCase(); | ||
|
|
||
| // サポートされている言語かチェック | ||
| const targetLang = supportedLangs.includes(langCode) ? langCode : defaultLang; | ||
|
|
||
| // リダイレクト | ||
| window.location.replace('/' + targetLang + '/'); | ||
| })(); | ||
| </script> | ||
| <noscript> | ||
| <meta http-equiv="refresh" content="0; url=/en/"> | ||
| </noscript> | ||
| </head> | ||
| <body> | ||
| <noscript> | ||
| <meta http-equiv="refresh" content="0; url=/en/"> | ||
| </noscript> | ||
| <p>Welcome to the documentation. Please navigate to <a href="/en/">English documentation</a>.</p> | ||
| </body> | ||
| </html> | ||
|
|
||
| <LanguageSwitchLayout targetPath="/" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.