-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
i18n-unused.config.js
35 lines (32 loc) · 1.46 KB
/
i18n-unused.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const path = require("path");
/**
* # Regex for translation keys
* This regex matches the following function calls:
* 1. `t("<some_key>")`,
* 2. `t("<some_key>", {key: "<some_string>"})`, or
* 3. `<Trans i18nKey="home_hero_subtitle" t={t}>`
*
* It also ensure that we don't match any other similar function calls (e.g. `format("dddd")`).
*
* ## Explanation of the regex
* - (?<!\w): negative lookbehind to ensure that there is no word character before "t" or "i18nKey"
* - (?: ... ): non-capturing group
* - t: for the use of `t("..")` OR `t("..", {key: ".."})`
* - \(("[^"]*"): captures a string enclosed in double quotes followed by an opening parenthesis
* - (?:,\s*\{[^}]*\})?: optional non-capturing group so that we match the optional interpolation object
* - i18nKey=".+"[^\w]: for the usage of `<Trans i18nKey="home_hero_subtitle" t={t}>`
*
* */
const translationKeyRegex = /(?<!\w)(?:t\(("[^"]*")(?:,\s*\{[^}]*\})?\)|i18nKey=".+"[^\w])/gi;
/** @type {import("i18n-unused/src/types/index.ts").RunOptions} */
const config = {
// localesPath: localePath, // uncomment to run on all locales (to calculate kb savings)
localesPath: path.join("./apps/website", "/public/static/locales", "/en"),
srcPath: "./apps/website",
srcExtensions: ["ts", "tsx"],
translationContextSeparator: ":",
translationKeyMatcher: translationKeyRegex,
missedTranslationParser: translationKeyRegex,
ignorePaths: ["node_modules", ".next"],
};
module.exports = config;