@nkzw/eslint-plugin-fbtee 1.2.5 → 1.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,19 @@
2
2
 
3
3
  _**fbtee** (Far Better Translations, Extended Edition) is an internationalization framework for JavaScript & React designed to be **powerful**, **flexible**, and **intuitive**._
4
4
 
5
- ## Why **fbtee**?
5
+ fbtee features inline translations and supports dynamic content, pluralization, lists, and more. It is built on top of Facebook's `fbt` library, which has been used in production for over a decade, serving billions of users.
6
+
7
+ ```tsx
8
+ const Greeting = ({ name }) => (
9
+ <div>
10
+ <fbt desc="Greeting">
11
+ Hello, <Name name={name} />!
12
+ </fbt>
13
+ </div>
14
+ );
15
+ ```
16
+
17
+ ## Why choose **fbtee**?
6
18
 
7
19
  - **Inline translations for Better Developer Experience:** Embed translations directly into your code. No need to manage translation keys or wrap your code with `t()` functions. **fbtee** uses a compiler to extract strings from your code and prepare them for translation providers.
8
20
  - **Proven in Production:** Built on Facebook's `fbt`, with over a decade of production usage, serving billions of users and two years of production usage in [Athena Crisis](https://athenacrisis.com).
@@ -227,6 +239,41 @@ if (getLocale() === 'en_US') {
227
239
 
228
240
  A full example of using `setupLocaleContext` to build your own `LocaleContext` abstraction can be found in the [Athena Crisis](https://github.com/nkzw-tech/athena-crisis/blob/main/hera/i18n/LocaleContext.tsx#L41) repository.
229
241
 
242
+ ### Gender Variations
243
+
244
+ `createLocaleContext` and `setupLocaleContext` also support setting the user's gender:
245
+
246
+ ```tsx
247
+ createLocaleContext({
248
+ …
249
+ gender: 'female', // 'male', 'female' or 'unknown' are supported.
250
+ });
251
+ ```
252
+
253
+ If you need to adjust the user's gender dynamically, you can use the `setGender` function provided by the `useLocaleContext` hook:
254
+
255
+ ```tsx
256
+ import { useLocaleContext } from 'fbtee';
257
+
258
+ const GenderSelector = () => {
259
+ const { gender, setGender } = useLocaleContext();
260
+
261
+ return (
262
+ <div>
263
+ <button onClick={() => setGender('male')}>
264
+ <fbt desc="Male gender">Male</fbt>
265
+ </button>
266
+ <button onClick={() => setGender('female')}>
267
+ <fbt desc="Female gender">Female</fbt>
268
+ </button>
269
+ <button onClick={() => setGender('unknown')}>
270
+ <fbt desc="Unknown gender">Unknown</fbt>
271
+ </button>
272
+ </div>
273
+ );
274
+ };
275
+ ```
276
+
230
277
  #### Full Customization
231
278
 
232
279
  Finally, if you are using something other than React, or need even more control over how **fbtee** is configured, you can use the `setupFbtee` function. This function allows you to set up **fbtee** with custom hooks and translations:
@@ -500,6 +547,62 @@ By default, **fbtee** outputs the generated translations in `src/translations`.
500
547
 
501
548
  After generating the translation files, your app is ready to display translated content in other languages. Since the `src/translations` folder is auto-generated, it should be ignored by version control and be part of your build process.
502
549
 
550
+ ## Setting the initial locale
551
+
552
+ If you know the user's locale ahead of time, you can set it when creating the `LocaleContext` by passing the locale as the first item in the `clientLocales` array:
553
+
554
+ ```tsx
555
+ // Read the user locale from localStorage etc.
556
+ const userLocale = localStorage.getItem('locale');
557
+ const LocaleContext = createLocaleContext({
558
+ availableLanguages,
559
+ clientLocales: [userLocale, navigator.language, ...navigator.languages],
560
+ loadLocale,
561
+ });
562
+ ```
563
+
564
+ Note that if you are picking the locale ahead of time like this, you need to manually load the translations. This is because the `loadLocale` function is only called when the locale changes, and not when the context is first created. This gives you full control over your app's startup:
565
+
566
+ ```tsx
567
+ const loadLocale = async (locale: string) => {
568
+ if (locale === 'ja_JP') {
569
+ return (await import('./translations/ja_JP.json')).default.ja_JP;
570
+ }
571
+ return {};
572
+ };
573
+
574
+ const translations = {
575
+ [userLocale]: await loadLocale(userLocale),
576
+ };
577
+
578
+ const LocaleContext = createLocaleContext({
579
+ availableLanguages,
580
+ clientLocales: [userLocale, navigator.language, ...navigator.languages],
581
+ loadLocale,
582
+ translations,
583
+ });
584
+ ```
585
+
586
+ If you have to wait for data from your server to determine the user's locale, you can use the `setLocale` function provided by `useLocaleContext` to set the locale after the initial render:
587
+
588
+ ```tsx
589
+ import { useLocaleContext } from 'fbtee';
590
+
591
+ const Root = () => {
592
+ // Fetch the locale from the server.
593
+ const userLocale = useUserLocale();
594
+ const { locale, setLocale } = useLocaleContext();
595
+
596
+ useEffect(() => {
597
+ if (locale !== userLocale) {
598
+ setLocale(userLocale);
599
+ }
600
+ }, [locale, setLocale, userLocale]);
601
+
602
+ return children;
603
+ };
604
+ ```
605
+
503
606
  ## ESLint Plugin
504
607
 
505
608
  You can install the optional eslint plugin to catch common mistakes and enforce best practices when using **fbtee**:
package/lib/index.cjs CHANGED
@@ -25,7 +25,7 @@ const __typescript_eslint_utils = __toESM(require("@typescript-eslint/utils"));
25
25
 
26
26
  //#region package.json
27
27
  var name = "@nkzw/eslint-plugin-fbtee";
28
- var version = "1.2.5";
28
+ var version = "1.2.7";
29
29
  var description = "";
30
30
  var keywords = [];
31
31
  var repository = {
@@ -41,8 +41,8 @@ var exports$1 = {
41
41
  };
42
42
  var main = "lib/index.cjs";
43
43
  var scripts = { "build": "tsdown src/index.tsx -d lib --target=node22 --format=esm,cjs --clean --dts" };
44
- var dependencies = { "@typescript-eslint/utils": "^8.35.1" };
45
- var devDependencies = { "@typescript-eslint/rule-tester": "^8.35.1" };
44
+ var dependencies = { "@typescript-eslint/utils": "^8.36.0" };
45
+ var devDependencies = { "@typescript-eslint/rule-tester": "^8.36.0" };
46
46
  var package_default = {
47
47
  name,
48
48
  version,
package/lib/index.js CHANGED
@@ -2,7 +2,7 @@ import { ESLintUtils } from "@typescript-eslint/utils";
2
2
 
3
3
  //#region package.json
4
4
  var name = "@nkzw/eslint-plugin-fbtee";
5
- var version = "1.2.5";
5
+ var version = "1.2.7";
6
6
  var description = "";
7
7
  var keywords = [];
8
8
  var repository = {
@@ -18,8 +18,8 @@ var exports = {
18
18
  };
19
19
  var main = "lib/index.cjs";
20
20
  var scripts = { "build": "tsdown src/index.tsx -d lib --target=node22 --format=esm,cjs --clean --dts" };
21
- var dependencies = { "@typescript-eslint/utils": "^8.35.1" };
22
- var devDependencies = { "@typescript-eslint/rule-tester": "^8.35.1" };
21
+ var dependencies = { "@typescript-eslint/utils": "^8.36.0" };
22
+ var devDependencies = { "@typescript-eslint/rule-tester": "^8.36.0" };
23
23
  var package_default = {
24
24
  name,
25
25
  version,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nkzw/eslint-plugin-fbtee",
3
- "version": "1.2.5",
3
+ "version": "1.2.7",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "repository": {
@@ -16,10 +16,10 @@
16
16
  },
17
17
  "main": "lib/index.cjs",
18
18
  "dependencies": {
19
- "@typescript-eslint/utils": "^8.35.1"
19
+ "@typescript-eslint/utils": "^8.36.0"
20
20
  },
21
21
  "devDependencies": {
22
- "@typescript-eslint/rule-tester": "^8.35.1"
22
+ "@typescript-eslint/rule-tester": "^8.36.0"
23
23
  },
24
24
  "scripts": {
25
25
  "build": "tsdown src/index.tsx -d lib --target=node22 --format=esm,cjs --clean --dts"