feat(i18n): localize pt-BR exercise names
This commit is contained in:
1 parent
dc3fafc6ef
commit
448c66752f
21 files changed
+1615
-53
No files matched your search
@@ -1,5 +1,6 @@
|
||||
// Runtime-agnostic core of the i18n module: state, constants and readers (t, dateLocale,
|
||||
// instrFor, getLang). Plain Node-loadable — the browser-only pieces (import.meta.glob lazy
|
||||
// instrFor, exerciseNameFor, getLang). Plain Node-loadable — the browser-only pieces
|
||||
// (import.meta.glob lazy
|
||||
// loads, the React subscription hook) live in i18n.js and re-export from here.
|
||||
|
||||
export const LANGS = {
|
||||
@@ -9,6 +10,7 @@ export const LANGS = {
|
||||
ko: '한국어', hi: 'हिन्दी'
|
||||
}
|
||||
export const INSTR_LANGS = ['en', 'es', 'fr', 'it', 'tr', 'ru', 'zh', 'hi', 'pl', 'ko', 'pt-BR']
|
||||
export const EXERCISE_NAME_LANGS = ['pt-BR']
|
||||
export const DATE_LOCALES = {
|
||||
en: 'en-GB', de: 'de-DE', es: 'es-ES', fr: 'fr-FR', it: 'it-IT', pt: 'pt-PT', 'pt-BR': 'pt-BR',
|
||||
pl: 'pl-PL', tr: 'tr-TR', ru: 'ru-RU', zh: 'zh-CN', ko: 'ko-KR', hi: 'hi-IN'
|
||||
@@ -17,6 +19,7 @@ export const DATE_LOCALES = {
|
||||
let lang = 'en' // set only by _setLangState, called from i18n.js setLang
|
||||
let dict = {} // current locale pack (empty = English fallback)
|
||||
let instr = null // { exId: [steps] } for the current language, null = English
|
||||
let exerciseNames = null // { exId: translated name }, null = original catalogue name
|
||||
let version = 0 // bumped on every setLang; drives the React subscription selector
|
||||
|
||||
export const getLang = () => lang
|
||||
@@ -33,13 +36,32 @@ export function t(s, ...args) {
|
||||
// Instructions for an exercise in the current language (English steps as fallback).
|
||||
export const instrFor = ex => (instr && instr[ex.id]) || ex.st || []
|
||||
|
||||
// Built-in catalogue names are bilingual when a complete translated name pack is active.
|
||||
// User-created exercises have no entry in the pack and keep their exact chosen name.
|
||||
export const exerciseNameFor = ex => {
|
||||
const translated = exerciseNames && ex && exerciseNames[ex.id]
|
||||
if (!translated) return ex?.n || ''
|
||||
// Some names (Burpee, Pilates, brand/model terms) are the established pt-BR term too.
|
||||
// Repeating an identical loanword in parentheses adds noise rather than context.
|
||||
return translated.toLocaleLowerCase('pt-BR') === ex.n.toLocaleLowerCase('en')
|
||||
? translated
|
||||
: `${translated} (${ex.n})`
|
||||
}
|
||||
|
||||
// Search both the localized and canonical English title without changing persisted data.
|
||||
export const exerciseNameSearchText = ex => {
|
||||
const translated = exerciseNames && ex && exerciseNames[ex.id]
|
||||
return translated ? `${translated} ${ex.n}` : (ex?.n || '')
|
||||
}
|
||||
|
||||
// Called by i18n.js's setLang once the locale pack has been loaded — kept here rather than
|
||||
// exported as setLang because loading packs requires import.meta.glob, which is Vite-only.
|
||||
// Both `dict` and `instr` may be null to reset to English.
|
||||
export function _setLangState(newLang, newDict, newInstr) {
|
||||
// `dict`, `instr` and `exerciseNames` may be null to reset to their English fallbacks.
|
||||
export function _setLangState(newLang, newDict, newInstr, newExerciseNames) {
|
||||
lang = LANGS[newLang] ? newLang : 'en'
|
||||
dict = lang === 'en' ? {} : (newDict || {})
|
||||
instr = lang === 'en' || !INSTR_LANGS.includes(lang) ? null : (newInstr || null)
|
||||
exerciseNames = lang === 'en' || !EXERCISE_NAME_LANGS.includes(lang) ? null : (newExerciseNames || null)
|
||||
version++
|
||||
return version
|
||||
}
|
||||
+17
-10
@@ -5,16 +5,20 @@
|
||||
|
||||
import { useSyncExternalStore } from 'react'
|
||||
import {
|
||||
LANGS, INSTR_LANGS, DATE_LOCALES,
|
||||
getLang, dateLocale, t, instrFor, getVersion, _setLangState
|
||||
LANGS, INSTR_LANGS, EXERCISE_NAME_LANGS, DATE_LOCALES,
|
||||
getLang, dateLocale, t, instrFor, exerciseNameFor, exerciseNameSearchText, getVersion, _setLangState
|
||||
} from './i18n-core.js'
|
||||
|
||||
export { LANGS, INSTR_LANGS, DATE_LOCALES, getLang, dateLocale, t, instrFor }
|
||||
export {
|
||||
LANGS, INSTR_LANGS, EXERCISE_NAME_LANGS, DATE_LOCALES,
|
||||
getLang, dateLocale, t, instrFor, exerciseNameFor, exerciseNameSearchText
|
||||
}
|
||||
|
||||
// Vite code-splits each locale pack into its own chunk via import.meta.glob; instructions use
|
||||
// the same mechanism in src/instr/. Both are lazy, so the production bundle ships English only.
|
||||
// Vite code-splits locale, instruction and exercise-name packs via import.meta.glob. They are
|
||||
// lazy, so the production bundle ships English only until another language is selected.
|
||||
const localePacks = import.meta.glob('../locales/*.js')
|
||||
const instrPacks = import.meta.glob('../instr/*.js')
|
||||
const exerciseNamePacks = import.meta.glob('../exercise-names/*.js')
|
||||
|
||||
// React subscription bookkeeping — kept here, not in core, so core has zero React coupling.
|
||||
const subs = new Set()
|
||||
@@ -23,12 +27,15 @@ const notify = () => { subs.forEach(f => f()) }
|
||||
export async function setLang(l) {
|
||||
if (!LANGS[l]) l = 'en'
|
||||
if (l === getLang() && getVersion() > 0) return
|
||||
let dict = {}, instr = null
|
||||
let dict = {}, instr = null, exerciseNames = null
|
||||
try { dict = l === 'en' ? {} : (await localePacks['../locales/' + l + '.js']()).default } catch (e) { dict = {} }
|
||||
try { instr = l === 'en' || !INSTR_LANGS.includes(l) ? null : (await instrPacks['../instr/' + l + '.js']()).default } catch (e) { instr = null }
|
||||
try {
|
||||
dict = l === 'en' ? {} : (await localePacks['../locales/' + l + '.js']()).default
|
||||
instr = l === 'en' || !INSTR_LANGS.includes(l) ? null : (await instrPacks['../instr/' + l + '.js']()).default
|
||||
} catch (e) { dict = {}; instr = null }
|
||||
_setLangState(l, dict, instr)
|
||||
exerciseNames = l === 'en' || !EXERCISE_NAME_LANGS.includes(l)
|
||||
? null
|
||||
: (await exerciseNamePacks['../exercise-names/' + l + '.js']()).default
|
||||
} catch (e) { exerciseNames = null }
|
||||
_setLangState(l, dict, instr, exerciseNames)
|
||||
notify()
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
import { EXIDX, isBodyweightEq } from './exercises.js'
|
||||
import { modeOf, fmtSec, isBw, isPerSide, sideReps } from './history.js'
|
||||
import { uid, todayISO, DAYN, fmtNum, exCount } from './format.js'
|
||||
import { t } from './i18n-core.js'
|
||||
import { t, exerciseNameFor } from './i18n-core.js'
|
||||
|
||||
const PLAN_FMT = 1
|
||||
const WEEK_ORDER = [1, 2, 3, 4, 5, 6, 0] // Mon-first, matching the Plan screen
|
||||
@@ -172,7 +172,7 @@ function routineHTML(r, unit) {
|
||||
const rows = units(r.ex).map(u => {
|
||||
const items = u.map(e => {
|
||||
const ex = EXIDX[e.id]
|
||||
const name = ex ? ex.n : t('Unknown exercise')
|
||||
const name = ex ? exerciseNameFor(ex) : t('Unknown exercise')
|
||||
const part = ex && ex.bp && ex.bp !== 'cardio' ? `<span class="part">${esc(ex.bp)}</span>` : ''
|
||||
return `<div class="ex"><div class="ex-n">${esc(name)}${part}</div><div class="ex-s">${esc(scheme(e, unit))}</div></div>`
|
||||
}).join('')
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import { afterEach, describe, expect, test } from 'vitest'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import ptBR from '../exercise-names/pt-BR.js'
|
||||
import { EXDB } from './exercises-data.js'
|
||||
import {
|
||||
EXERCISE_NAME_LANGS, _setLangState, exerciseNameFor, exerciseNameSearchText
|
||||
} from './i18n-core.js'
|
||||
|
||||
describe('Brazilian Portuguese exercise names', () => {
|
||||
const source = JSON.parse(readFileSync(new URL('../../../scripts/exercise-name-sources/pt-BR.json', import.meta.url), 'utf8'))
|
||||
afterEach(() => _setLangState('en', {}, null, null))
|
||||
|
||||
test('matches the curated source and covers the complete built-in catalogue', () => {
|
||||
expect(Object.keys(ptBR)).toHaveLength(EXDB.length)
|
||||
expect(ptBR).toEqual(source)
|
||||
expect(EXERCISE_NAME_LANGS).toContain('pt-BR')
|
||||
})
|
||||
|
||||
test('contains a non-empty translation for every known exercise', () => {
|
||||
for (const exercise of EXDB) {
|
||||
expect(ptBR[exercise.id]?.trim(), exercise.id).toBeTruthy()
|
||||
expect(ptBR[exercise.id], exercise.id).not.toMatch(/(?:^|[^\p{L}])(?:abdómen|anca|gémeos|ecrã|ginásio|banda|piso|omoplata|pegada inversa|pegada invertida)(?=$|[^\p{L}])/iu)
|
||||
}
|
||||
})
|
||||
|
||||
test('preserves identity-changing qualifiers and equipment', () => {
|
||||
const rules = [
|
||||
[/assisted/iu, /assistid/iu],
|
||||
[/weighted/iu, /(?:peso|carga|lastro|ponderad)/iu],
|
||||
[/(?:^|[^\p{L}])male(?=$|[^\p{L}])/iu, /masculin/iu],
|
||||
[/(?:^|[^\p{L}])female(?=$|[^\p{L}])/iu, /feminin/iu],
|
||||
[/barbell/iu, /barra/iu],
|
||||
[/dumbbell/iu, /halter/iu],
|
||||
[/kettlebell/iu, /kettlebell/iu],
|
||||
[/smith/iu, /smith/iu],
|
||||
[/stability ball/iu, /bola de estabilidade/iu],
|
||||
[/medicine ball/iu, /bola medicinal/iu],
|
||||
]
|
||||
for (const exercise of EXDB) {
|
||||
for (const [english, portuguese] of rules) {
|
||||
if (english.test(exercise.n)) expect(ptBR[exercise.id], `${exercise.id}: ${english}`).toMatch(portuguese)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
test('shows Portuguese first and preserves the canonical English title', () => {
|
||||
const exercise = EXDB[0]
|
||||
_setLangState('pt-BR', {}, null, ptBR)
|
||||
expect(exerciseNameFor(exercise)).toBe(`${ptBR[exercise.id]} (${exercise.n})`)
|
||||
expect(exerciseNameSearchText(exercise)).toContain(ptBR[exercise.id])
|
||||
expect(exerciseNameSearchText(exercise)).toContain(exercise.n)
|
||||
})
|
||||
|
||||
test('never translates custom exercises or changes other languages', () => {
|
||||
const custom = { id: 'custom-1', n: 'Meu exercício' }
|
||||
_setLangState('pt-BR', {}, null, ptBR)
|
||||
expect(exerciseNameFor(custom)).toBe('Meu exercício')
|
||||
_setLangState('en', {}, null, null)
|
||||
expect(exerciseNameFor(EXDB[0])).toBe(EXDB[0].n)
|
||||
})
|
||||
})
|
||||
@@ -11,6 +11,7 @@ import { STRENGTH_FULL_MS, STRENGTH_HALF_LIFE_MS, STRENGTH_FLOOR, halfLifeDecay
|
||||
import { musclesOf } from './muscles.js'
|
||||
import { EXIDX } from './exercises.js'
|
||||
import { isWarmupRow } from './workout-model.js'
|
||||
import { exerciseNameFor } from './i18n-core.js'
|
||||
|
||||
const round1 = value => Math.round(value * 10) / 10
|
||||
|
||||
@@ -61,11 +62,11 @@ export function primaryMuscleOf(entry) {
|
||||
return best
|
||||
}
|
||||
|
||||
function exerciseName(entry) {
|
||||
function resolvedExerciseName(entry) {
|
||||
// Imported history often has no name snapshot (entries are { id, sets, topW }) - the
|
||||
// catalogue (or the registered custom) is the canonical name source.
|
||||
const ex = entry && typeof entry === 'object' ? EXIDX[entry.id] : null
|
||||
if (ex?.n) return ex.n
|
||||
if (ex?.n) return exerciseNameFor(ex)
|
||||
if (entry?.muscleSnapshot?.n) return entry.muscleSnapshot.n
|
||||
return entry && typeof entry === 'object' && entry.n ? entry.n : null
|
||||
}
|
||||
@@ -97,7 +98,7 @@ export function strengthExerciseRows(S, now) {
|
||||
const decay = lastAt == null ? STRENGTH_FLOOR : strengthFromAge(Number(now) - lastAt)
|
||||
rows.push({
|
||||
id,
|
||||
name: exerciseName(entry) || id,
|
||||
name: resolvedExerciseName(entry) || id,
|
||||
est: best.est,
|
||||
estDate: best.d,
|
||||
primary: primaryMuscleOf(entry) ? primaryMuscleOf(entry).slug : null,
|
||||
@@ -129,7 +130,7 @@ export function strengthExerciseRowsForMuscle(S, now, slug) {
|
||||
const primary = primaryMuscleOf(entry)
|
||||
seen.set(entry.id, {
|
||||
id: entry.id,
|
||||
name: exerciseName(entry) || entry.id,
|
||||
name: resolvedExerciseName(entry) || entry.id,
|
||||
weight,
|
||||
primary: primary ? primary.slug : null,
|
||||
est: best.est,
|
||||
|
||||
Reference in new issue
Block a user