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

Usage with react native for use-intl #1649

Closed
ScreamZ opened this issue Jan 8, 2025 · 2 comments
Closed

Usage with react native for use-intl #1649

ScreamZ opened this issue Jan 8, 2025 · 2 comments
Labels
enhancement New feature or request unconfirmed Needs triage.

Comments

@ScreamZ
Copy link

ScreamZ commented Jan 8, 2025

Is your feature request related to a problem? Please describe.

In react native you don't have Intl.PluralRules yet.

https://formatjs.github.io/docs/polyfills/intl-pluralrules/#react-native might help, but still after

// https://formatjs.github.io/docs/polyfills/intl-pluralrules/
import "@formatjs/intl-pluralrules/polyfill-force";
import "@formatjs/intl-pluralrules/locale-data/en";
import "@formatjs/intl-pluralrules/locale-data/fr";

I'm getting an error (NOBRIDGE) ERROR [Error: FORMATTING_ERROR: Cannot read property 'prototype' of undefined] [Component Stack]

here my full setup

import * as Localization from "expo-localization";
import { Settings } from "luxon";
import type { IntlConfig } from "use-intl";
import fr from "./locales/fr.json";
import en from "./locales/en.json";

// https://formatjs.github.io/docs/polyfills/intl-pluralrules/
import "@formatjs/intl-pluralrules/polyfill-force";
import "@formatjs/intl-pluralrules/locale-data/en";
import "@formatjs/intl-pluralrules/locale-data/fr";

Settings.defaultLocale = Localization.getLocales()[0].languageCode ?? "en";
Settings.defaultZone =
	Localization.getCalendars()[0].timeZone ?? "Europe/Paris";

const Translations = {
	fr,
	en,
} as const satisfies Record<string, unknown>;

export const IntlContextConfig: IntlConfig = {
	locale: Settings.defaultLocale,
	messages:
		Translations[Settings.defaultLocale as keyof typeof Translations] ??
		Translations.en,
};

type Messages = (typeof Translations)["en"];

declare global {
	// Use type safe message keys with `use-intl`
	interface IntlMessages extends Messages {}
}

Describe the solution you'd like

I would like a working solution for react native with expo.

Describe alternatives you've considered

Not using plural ? 😅

@ScreamZ ScreamZ added enhancement New feature or request unconfirmed Needs triage. labels Jan 8, 2025
@ScreamZ
Copy link
Author

ScreamZ commented Jan 8, 2025

import "intl-pluralrules";

This library seems to work. Not sure if same performances problems occurs though...

@amannn
Copy link
Owner

amannn commented Jan 9, 2025

Yep, the runtime requirements are documented.

Please see the existing issues on this topic:

  1. use-intl is not working properly with React Native #957
  2. React Native: Error: INVALID_MESSAGE: Cannot read property 'prototype' of undefined #415

I also found this: expo/config-plugins#73 (comment)

I'm not up to date with what the best polyfill for React Native is, but maybe the users who have opened the previous threads can help!

@amannn amannn closed this as completed Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request unconfirmed Needs triage.
Projects
None yet
Development

No branches or pull requests

2 participants