Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default defineConfig({
Search: './src/components/Search.astro',
Hero: './src/components/Hero.astro',
SocialIcons: './src/components/SocialIcons.astro',
MarkdownContent: './src/components/MarkdownContent.astro',
},

social: [
Expand Down
3 changes: 3 additions & 0 deletions scripts/check-translations.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const checkedLocales = stagingLocale ? [stagingLocale] : localeCodes
for (const locale of checkedLocales) {
const directory = stagingDirectory ?? join(root, locale)
const copy = stagingDirectory ? JSON.parse(readFileSync(join(directory, 'locale.json'), 'utf8')) : readCopy(locale)
for (const key of ['message', 'linkLabel']) {
if (typeof copy.translationNotice?.[key] !== 'string' || !copy.translationNotice[key].trim()) errors.push(`${locale}: missing translation notice ${key}`)
}
for (const label of sidebarLabels) {
if (typeof copy.sidebar?.[label] !== 'string' || !copy.sidebar[label].trim()) errors.push(`${locale}: missing sidebar translation for ${label}`)
}
Expand Down
7 changes: 7 additions & 0 deletions src/components/MarkdownContent.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
import Default from '@astrojs/starlight/components/MarkdownContent.astro'
import TranslationNotice from './TranslationNotice.astro'
---

<TranslationNotice />
<Default><slot /></Default>
30 changes: 30 additions & 0 deletions src/components/TranslationNotice.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
import { copyForLanguage } from '../localization'

const { head, id, isFallback, lang } = Astro.locals.starlightRoute
const english = head.find(({ tag, attrs }) => tag === 'link' && attrs?.rel === 'alternate'
&& /^en(?:-|$)/i.test(String(attrs.hreflang)))?.attrs?.href
const originalHref = typeof english === 'string' ? new URL(english, Astro.url).pathname : undefined
const showNotice = !/^en(?:-|$)/i.test(lang) && id !== '404' && !isFallback && originalHref
const copy = copyForLanguage(lang).translationNotice
---

{showNotice && <aside class="ginger-translation-notice" data-pagefind-ignore>
<p>{copy.message} <a href={originalHref} hreflang="en" data-ginger-locale="root">{copy.linkLabel}</a></p>
</aside>}

<style>
.ginger-translation-notice {
padding: .875rem 1rem;
border-inline-start: .2rem solid var(--sl-color-accent);
border-radius: .25rem;
background: var(--sl-color-accent-low);
color: var(--sl-color-text);
font-size: var(--sl-text-sm);
line-height: 1.6;
}
p { margin: 0; }
a { color: inherit; font-weight: 600; text-decoration: underline; text-underline-offset: .15em; }
a:hover { color: var(--sl-color-text-accent); }
a:focus-visible { outline: 2px solid currentColor; outline-offset: 3px; border-radius: .1rem; }
</style>
4 changes: 4 additions & 0 deletions src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"label": "Deutsch",
"lang": "de",
"title": "GingerWallet-Dokumentation",
"translationNotice": {
"message": "Diese Seite wurde mithilfe von KI aus dem Englischen übersetzt und kann Fehler enthalten.",
"linkLabel": "Englisches Original lesen"
},
"header": {
"Getting Started": "Erste Schritte",
"Why Ginger?": "Warum Ginger?",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"Ginger Wallet website": "Ginger Wallet website"
},
"sidebar": {},
"translationNotice": {
"message": "This page was translated from English using AI and may contain errors.",
"linkLabel": "Read the English original"
},
"search": {
"levelLabel": "Level",
"scope": "Search in",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"label": "Español",
"lang": "es",
"title": "Documentación de GingerWallet",
"translationNotice": {
"message": "Esta página se ha traducido del inglés con IA y puede contener errores.",
"linkLabel": "Leer el original en inglés"
},
"header": {
"Getting Started": "Primeros pasos",
"Why Ginger?": "¿Por qué Ginger?",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"label": "Français",
"lang": "fr",
"title": "Documentation GingerWallet",
"translationNotice": {
"message": "Cette page a été traduite de l’anglais à l’aide de l’IA et peut contenir des erreurs.",
"linkLabel": "Lire l’original en anglais"
},
"header": {
"Getting Started": "Premiers pas",
"Why Ginger?": "Pourquoi Ginger ?",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/hu.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
"Understanding CoinJoin": "A CoinJoin megértése",
"Self-custody": "Saját kulcskezelés"
},
"translationNotice": {
"message": "Ez az oldal az angol eredeti AI-val készült fordítása, és hibákat tartalmazhat.",
"linkLabel": "Az angol eredeti elolvasása"
},
"search": {
"levelLabel": "Nehézségi szint",
"scope": "Keresés itt",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"label": "Italiano",
"lang": "it",
"title": "Documentazione di GingerWallet",
"translationNotice": {
"message": "Questa pagina è stata tradotta dall’inglese con l’IA e può contenere errori.",
"linkLabel": "Leggi l’originale in inglese"
},
"header": {
"Getting Started": "Per iniziare",
"Why Ginger?": "Perché Ginger?",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/pt-br.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"label": "Português (Brasil)",
"lang": "pt-BR",
"title": "Documentação do GingerWallet",
"translationNotice": {
"message": "Esta página foi traduzida do inglês usando IA e pode conter erros.",
"linkLabel": "Leia o original em inglês"
},
"header": {
"Getting Started": "Primeiros passos",
"Why Ginger?": "Por que Ginger?",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/pt-pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"label": "Português (Portugal)",
"lang": "pt-PT",
"title": "Documentação do GingerWallet",
"translationNotice": {
"message": "Esta página foi traduzida do inglês com recurso a IA e pode conter erros.",
"linkLabel": "Leia o original em inglês"
},
"header": {
"Getting Started": "Primeiros passos",
"Why Ginger?": "Porquê o Ginger?",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"label": "Русский",
"lang": "ru",
"title": "Документация GingerWallet",
"translationNotice": {
"message": "Эта страница переведена с английского с помощью ИИ и может содержать ошибки.",
"linkLabel": "Читать оригинал на английском"
},
"header": {
"Getting Started": "Начало работы",
"Why Ginger?": "Почему Ginger?",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/tr.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"label": "Türkçe",
"lang": "tr",
"title": "GingerWallet belgeleri",
"translationNotice": {
"message": "Bu sayfa yapay zekâ kullanılarak İngilizceden çevrilmiştir ve hatalar içerebilir.",
"linkLabel": "İngilizce aslını okuyun"
},
"header": {
"Getting Started": "Başlangıç",
"Why Ginger?": "Neden Ginger?",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"label": "简体中文",
"lang": "zh-CN",
"title": "GingerWallet 文档",
"translationNotice": {
"message": "本页由 AI 从英文翻译,可能存在错误。",
"linkLabel": "阅读英文原文"
},
"header": {
"Getting Started": "入门",
"Why Ginger?": "为什么选择 Ginger?",
Expand Down
2 changes: 1 addition & 1 deletion src/localization-config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync, readdirSync, readFileSync } from 'node:fs'

/** @typedef {{label: string, lang: string, title: string, header: Record<string, string>, sidebar: Record<string, string>, search: {levelLabel: string, scope: string, everyday: string, advanced: string, all: string, hint: string, loading: string, error: string, retry: string, level: Record<'beginner'|'everyday'|'advanced', string>}}} LocaleCopy */
/** @typedef {{label: string, lang: string, title: string, header: Record<string, string>, sidebar: Record<string, string>, translationNotice: {message: string, linkLabel: string}, search: {levelLabel: string, scope: string, everyday: string, advanced: string, all: string, hint: string, loading: string, error: string, retry: string, level: Record<'beginner'|'everyday'|'advanced', string>}}} LocaleCopy */

const directory = new URL('./locales/', import.meta.url)
/** @type {Record<string, LocaleCopy>} */
Expand Down
4 changes: 4 additions & 0 deletions src/localization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ interface LocaleCopy {
title: string
header: Record<string, string>
sidebar: Record<string, string>
translationNotice: {
message: string
linkLabel: string
}
search: {
levelLabel: string
scope: string
Expand Down
64 changes: 64 additions & 0 deletions tests/translation-notice.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { expect, test } from '@playwright/test'
import { readdirSync, readFileSync } from 'node:fs'

const locales = readdirSync(new URL('../src/locales/', import.meta.url))
.filter((name) => name.endsWith('.json') && name !== 'en.json')
.map((name) => ({ code: name.slice(0, -5), ...JSON.parse(readFileSync(new URL(`../src/locales/${name}`, import.meta.url), 'utf8')) }))

test('translated guides and homepages show a localized notice linking to the same English page', async ({ page }) => {
await page.setViewportSize({ width: 320, height: 844 })
for (const locale of locales) {
for (const original of ['/', '/payments/send/']) {
await page.goto(`/${locale.code}${original}`)
const notice = page.locator('.ginger-translation-notice')
await expect(notice).toBeVisible()
await expect(notice).toContainText(locale.translationNotice.message)
await expect(notice).toHaveAttribute('data-pagefind-ignore', '')
const link = notice.getByRole('link', { name: locale.translationNotice.linkLabel, exact: true })
await expect(link).toHaveAttribute('href', original)
await expect(link).toHaveAttribute('hreflang', 'en')
const bounds = (await notice.boundingBox())!
expect(bounds.x).toBeGreaterThanOrEqual(0)
expect(bounds.x + bounds.width).toBeLessThanOrEqual(320)
expect(await notice.evaluate((element) => element.scrollWidth <= element.clientWidth)).toBe(true)
}
}
})

test('English pages and the error page have no translation notice', async ({ page }) => {
for (const path of ['/', '/payments/send/', '/404.html']) {
await page.goto(path)
await expect(page.locator('.ginger-translation-notice')).toHaveCount(0)
}
})

test.describe('Hungarian visitor', () => {
test.use({ locale: 'hu-HU' })

test('the original link remembers English instead of redirecting back to the translation', async ({ page }) => {
await page.goto('/hu/payments/send/')
await page.locator('.ginger-translation-notice a').click()
await expect(page).toHaveURL(/\/payments\/send\/$/)
await expect(page.locator('html')).toHaveAttribute('lang', 'en')
await expect(page.locator('.ginger-translation-notice')).toHaveCount(0)
await page.reload()
await expect(page.locator('html')).toHaveAttribute('lang', 'en')
await page.goto('/using-ginger/fee/')
await expect(page.locator('html')).toHaveAttribute('lang', 'en')
})
})

test('the notice and original link work without JavaScript', async ({ browser, baseURL }) => {
const context = await browser.newContext({ javaScriptEnabled: false, locale: 'hu-HU' })
const page = await context.newPage()
try {
await page.goto(`${baseURL}/hu/payments/send/`)
await expect(page.locator('.ginger-translation-notice')).toBeVisible()
await page.locator('.ginger-translation-notice a').click()
await expect(page).toHaveURL(/\/payments\/send\/$/)
await expect(page.locator('html')).toHaveAttribute('lang', 'en')
await expect(page.locator('.ginger-translation-notice')).toHaveCount(0)
} finally {
await context.close()
}
})
Loading