Skip to content
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

Feature/add next intl for localization #456

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

sseshaheen
Copy link

This PR introduces robust localization and multi-language support to the nextjs-starter-medusa project using next-intl. The implementation builds on the work of el-buku but aligns with the latest main branch of nextjs-starter-medusa. It also incorporates several improvements, including updated deprecated functions.

Here is a summary of what was done:

1. Integration of next-intl:

  • Introduced next-intl to enable dynamic translations and locale-aware routing across the app.
  • Updated imports for Link, redirect, usePathname and useRouter to use centralized utilities from @lib/i18n/navigation for consistent locale handling.

2. Dynamic Translations for Text Strings:

  • Replaced all hardcoded text strings with t(k.KEY_NAME) to enable dynamic translations for user-facing text.
  • Added imports:
    • import k from "@lib/i18n/translations/keys"
    • import { useSafeTranslations } from "@lib/i18n/use-safe-translations" for client-side components, or import { getTranslations } from "next-intl/server" for server-side components.
  • Ensured type safety and consistent translation usage with centralized keys.

3. Updated Folder Structure:

  • Moved [countryCode] folder to [locale] for better organization and alignment with locale-aware routing.

4. File Additions:
- @lib/i18n/settings.ts: Provides core localization settings, including supported languages (languages), fallback language (fallbackLng), and cookie-based locale detection.
- @lib/i18n/use-safe-translations.ts: Custom hook wrapping useTranslations from next-intl, adding graceful handling of missing translations and optional debugging with NEXT_PUBLIC_TRANSLATION_HELPERS.
- @lib/i18n/navigation.ts: Centralized navigation utilities for locale-aware routing
- @lib/i18n/config-callback.ts: Handles dynamic locale resolution and translation loading
- @lib/i18n/request-config.js: Centralized locale configurations
- @lib/i18n/translations/keys.ts: Centralized translation keys for consistency and type safety
- @lib/i18n/translations/locales/*.ts: Locale-specific translation files for dynamic text handling

Copy link

vercel bot commented Jan 26, 2025

Someone is attempting to deploy a commit to the medusajs Team on Vercel.

A member of the Team first needs to authorize it.

@sseshaheen sseshaheen mentioned this pull request Jan 26, 2025
@sseshaheen sseshaheen changed the title Feature/add next intl Feature/add next intl for localization Jan 26, 2025
Copy link

@amannn amannn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @sseshaheen,

thank you so much for the email you wrote me yesterday, asking for feedback on this PR! Medusa looks really cool, would be nice to have support for multiple languages!

I think you're on a good track, but there are a few areas where I think you could simplify the code significantly by aligning closer with what next-intl suggests. If in doubt, you might want to refer to the docs for getting started advice etc.

Hope this helps, let me know if you have a question!

As a side note (or rather a shameless plug), I'm currently working on learn.next-intl.dev. The course will be based on an ecommerce store, so quite related to this starter template and could be of help. It's not out yet though, just as a hint for the future!

src/lib/i18n/request-config.js Outdated Show resolved Hide resolved
src/lib/i18n/request-config.js Outdated Show resolved Hide resolved
src/lib/i18n/settings.ts Outdated Show resolved Hide resolved
src/lib/i18n/translations/keys.ts Outdated Show resolved Hide resolved
next.config.js Outdated Show resolved Hide resolved
src/lib/i18n/translations/locales/en.ts Outdated Show resolved Hide resolved
import createIntlMiddleware from "next-intl/middleware"
import { fallbackLng, intlConfig, languages } from "./lib/i18n/settings"

const intlMiddleware = createIntlMiddleware(intlConfig)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, routing is done based on a pattern like [locale]/[countryCode].

For e-commerce stores, it's common to have either only the country code or optionally a locale following the country (e.g. /uk, /ch/fr, …).

I'd recommend to use custom prefixes in order to map a country code to a locale that can be used internally.

You'd have to change the currently used [countryCode] segment to [locale], but the public routes would stay the same. Additionally, you can retrieve the country code from the locale in your app code:

const locale = new Intl.Locale('fr-CH');
locale.region; // 'CH')

src/modules/account/components/overview/index.tsx Outdated Show resolved Hide resolved
</span>
<span className="font-semibold">
Total amount
{t(k.TOTAL_AMOUNT)}
</span>
<span data-testid="order-created-date">
{new Date(order.created_at).toDateString()}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next-intl has utilities for formatting dates, numbers and more via useFormatter. I'd really recommend using those as they will help to align formatting with the given locale.

tsconfig.json Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants