Skip to content

Frontend: add Indonesian/English UI with language selector and location-based default - #29

Merged
mfarim merged 1 commit into
mainfrom
feat/i18n-id-en
Nov 25, 2026
Merged

mfarim merged 1 commit into
mainfrom
feat/i18n-id-en

Conversation

@mfarim

@mfarim mfarim commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Summary

The UI is now available in Indonesian and English. Users switch with an ID / EN selector. The first visit picks the language from the visitor's location: Indonesian in Indonesia, English everywhere else.

How the language is chosen

  1. Saved choice. Once the user picks a language with the selector, it is stored in localStorage and always wins.
  2. IP country. The frontend nginx reads Cloudflare's CF-IPCountry header and passes it to the SPA as a geo_country cookie. ID → Indonesian, any other country → English. The language is correct on the first render, with no extra request.
  3. Time zone fallback. When the country is unknown (local runs, no Cloudflare, XX): a browser time zone of Asia/Jakarta, Asia/Pontianak, Asia/Makassar or Asia/Jayapura → Indonesian, otherwise English.

nginx details (frontend/nginx.conf)

  • The cookie is only set on page navigations, not on /api, /ws or static files. A Set-Cookie on assets would stop Cloudflare from caching them. The check uses $request_uri, because try_files rewrites $uri to /index.html.
  • Only a two-letter country code can reach the cookie value.
  • add_header is at server level next to the existing security headers. An empty value sends no header.

Implementation

  • src/i18n/: a small in-house i18n module, no new dependencies.
    • useI18n() returns t, lang, locale and setLang. The language lives in a zustand store, so switching re-renders the app.
    • A non-hook t() is for code outside components: the utils/files.ts alert, and event listeners/timers in ExamRunner.
    • {name} placeholders.
  • src/i18n/messages/: one namespace per page, plus common, nav and auth. defineMessages() makes a key present in only one language a type error, so npm run build fails on untranslated text.
  • LanguageSwitcher is placed in:
    • the navbar,
    • the login page,
    • the change-password page,
    • the exam runner's error and result screens. It is hidden while an exam is running, so it does not distract students.
  • All pages are converted: text, placeholders, title / aria-label / alt, alert / confirm, error fallbacks, status and role badges, and option labels.
  • Dates and times use the active locale (id-ID / en-US) instead of the browser default.
  • Raw enum values that were shown as-is (exam and attempt status, announcement target, forum user role, proctoring violation codes) are now mapped to translated labels at render time. The values sent to and stored by the backend are unchanged.

Not in this PR

  • Backend messages. API error messages (response.data.message) are shown as the backend returns them, which is mixed Indonesian and English. A follow-up could send Accept-Language and localize them on the server.
  • index.html metadata. The meta description and Open Graph tags stay as they are. <html lang> is set at runtime.
  • Mixed-language originals. Several pages were partly written in English. Their Indonesian text is new wording rather than copied from the existing UI. Native-speaker review is welcome.

Deployment notes

  • Cloudflare IP Geolocation must be on (Network settings; on by default).
  • The host nginx must forward request headers to the frontend container. This is the default with proxy_pass.
  • No new environment variables. No backend or database changes.

Testing

  • tsc -b and vite build pass.
  • oxlint: no new warnings. The 4 existing warnings in ExamMonitor / MaterialsList are unchanged.
  • A missing English key fails the type check.
  • Detection logic: 12 cases covering country cookie, XX, malformed cookie, and Indonesian / non-Indonesian time zones.
  • Headless Chrome, /login:
    • Jakarta time zone → Indonesian
    • Berlin time zone → English
    • geo_country=ID with Berlin time zone → Indonesian
    • geo_country=US with Jakarta time zone → English
    • clicking EN → English, saved
    • no page errors
  • nginx: curl -sI -H 'CF-IPCountry: ID' http://localhost/login returns Set-Cookie: geo_country=ID, and /assets/* / /api/* responses do not.
  • Manual pass through the logged-in pages in both languages (admin, teacher, student), including the exam runner and exam monitor.

@mfarim mfarim self-assigned this Nov 25, 2026
@mfarim mfarim added the enhancement New feature or request label Nov 25, 2026
@mfarim
mfarim merged commit 1056d4e into main Nov 25, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant