Skip to content

Commit 240ae53

Browse files
authored
feat: [refactor] i18n config vs hardcoded (#59)
En esta PR, refactor: * Se sustituyen los valores hardcoded relacionados con la i18n por los valores de la configuración del proyecto. * Se quitan los strings traducibles del componente para llevarlos a la carpeta de traducciones.
1 parent 4c42e7f commit 240ae53

3 files changed

Lines changed: 23 additions & 19 deletions

File tree

src/components/LanguagePicker.astro

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
---
2+
import { i18n } from 'astro:config/client'
23
import { texts } from '../i18n/components/LanguagePicker'
34
45
// Idiomas disponibles
5-
const languages = [
6-
{ code: 'es', label: 'es', ariadesc: 'Idioma español' },
7-
{ code: 'en', label: 'en', ariadesc: 'English language' },
8-
{ code: 'ca', label: 'ca', ariadesc: 'Idioma català' },
9-
]
6+
const languages = i18n.locales as string[]
107
118
interface Props {
129
lang: string
1310
}
1411
15-
const { lang = 'es' } = Astro.params
12+
const { lang = i18n.defaultLocale } = Astro.params
1613
const t = texts[lang as keyof typeof texts]
1714
1815
// Idioma actual detectado desde la URL
1916
const pathname = Astro.url.pathname
20-
const currentLang = pathname.split('/')[1] || 'es'
17+
const currentLang = pathname.split('/')[1] || i18n.defaultLocale
2118
2219
// Función para construir la URL destino
2320
const getPathForLang = (lang) => {
2421
const parts = pathname.split('/').filter(Boolean)
2522
2623
// Si ya hay idioma, lo reemplaza
27-
if (languages.some((l) => l.code === parts[0])) {
24+
if (languages.some((l) => l === parts[0])) {
2825
parts[0] = lang
2926
} else {
3027
parts.unshift(lang)
@@ -39,14 +36,14 @@ const getPathForLang = (lang) => {
3936
languages.map((lang) => (
4037
<li>
4138
<a
42-
href={getPathForLang(lang.code)}
43-
hreflang={lang.code}
44-
aria-current={lang.code === currentLang ? 'page' : undefined}
45-
aria-label={lang.ariadesc}
46-
lang={lang.code}
47-
class={lang.code === currentLang ? 'font-bold underline underline-offset-4' : ''}
39+
href={getPathForLang(lang)}
40+
hreflang={lang}
41+
aria-current={lang === currentLang ? 'page' : undefined}
42+
aria-label={t['labelariadesc']}
43+
lang={lang}
44+
class={lang === currentLang ? 'font-bold underline underline-offset-4' : ''}
4845
>
49-
{lang.label}
46+
{t['label'][lang]}
5047
</a>
5148
</li>
5249
))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
export const texts = {
22
es: {
33
alttext: 'Selector de idioma',
4+
label: { es: 'es', en: 'en', ca: 'ca' },
5+
labelariadesc: 'Idioma español',
46
},
57
en: {
68
alttext: 'Language selector',
9+
label: { es: 'es', en: 'en', ca: 'ca' },
10+
labelariadesc: 'English language',
711
},
812
ca: {
913
alttext: "Selector d'idioma",
14+
label: { es: 'es', en: 'en', ca: 'ca' },
15+
labelariadesc: 'Idioma català',
1016
},
1117
} as const

src/pages/index.astro

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
---
2+
import { i18n } from 'astro:config/client'
23
import IndexPage from '../components/index.astro'
34
---
45

56
<script is:inline>
67
// Get browser language
78
const lang = navigator.language.split('-')[0]
8-
const supported = ['es', 'en', 'ca']
9-
const target = supported.includes(lang) ? lang : 'es'
9+
const supported = i18n.locales
10+
const target = supported.includes(lang) ? lang : i18n.defaultLocale
1011

1112
// Redirect immediately
12-
if (target !== 'es') {
13+
if (target !== i18n.defaultLocale) {
1314
window.location.href = `/${target}/`
1415
}
1516
</script>
1617

17-
<IndexPage lang="es" />
18+
<IndexPage lang={i18n.defaultLocale} />

0 commit comments

Comments
 (0)