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

refactor: 使用插件式配置 #8

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

refactor: 使用插件式配置 #8

wants to merge 105 commits into from

Conversation

MarvelSQ
Copy link
Owner

@MarvelSQ MarvelSQ commented Apr 16, 2023

the Next Configuration

// extract-locale.config.mjs
import { defineConfig } from "extract-locale";
import { createMatcher } from "extract-locale/matcher";
import { createAssignee } from "extract-locale/assignee";
import HookHelper from "extract-locale/helper/hook";

const dict = new Map();

export default defineConfig({
  matcher: createMatcher({
    // chinese character
    test: /[\u4e00-\u9fa5]/,
  }),
  assignee: createAssignee({
    getLocaleKey(text) {
      if (!dict.has(text)) {
        const key = `LOCALE_TEXT_${dict.size}`;
        dict.set(text, key);
      }
      return dict.get(text);
    },
  }),
  plugin: [
    HookHelper(
      {
        importSource: "./Intl/index",
        name: "useIntl",
        isDefault: false,
      },
      {
        result: "formatMessage",
      }
    ),
  ],
});

Matcher

match any StringLiteral, TemplateLiteral, JSXText and JSXAttributeText that match matcher.test RegExp provide

Assignee

assign the matched text to a key and return the key.
eg: you can use Map to store the text and key, and return the key.

Plugin

plugin will help you to replace the matched text with the key that assignee return.

HookHelper

HookHelper accept two arguments, the first is the importSource and name of the useIntl hook, the second is the result of the useIntl hook.

@MarvelSQ MarvelSQ added the enhancement New feature or request label Apr 16, 2023
@MarvelSQ MarvelSQ self-assigned this Apr 16, 2023
@vercel
Copy link

vercel bot commented Apr 16, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
extract-local ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 22, 2023 1:31am
extract-locale ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 22, 2023 1:31am

@codecov-commenter
Copy link

codecov-commenter commented Apr 16, 2023

Codecov Report

❗ No coverage uploaded for pull request base (main@d28bd9d). Click here to learn what that means.
Patch has no changes to coverable lines.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@           Coverage Diff           @@
##             main       #8   +/-   ##
=======================================
  Coverage        ?   79.18%           
=======================================
  Files           ?        8           
  Lines           ?     1201           
  Branches        ?      137           
=======================================
  Hits            ?      951           
  Misses          ?      250           
  Partials        ?        0           

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

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
Status: No status
2 participants