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

fix key for Chinese #166

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/react-intl-universal-extract/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# react-intl-universal-extract
Extract default messages in application using [react-intl-universal](https://github.com/alibaba/react-intl-universal).
# react-intl-universal-extract-pluto
- Since react-intl-universal-extract does not support Chinese as a key, based on react-intl-universal-extract, an entry - file supporting Chinese key extraction is released.

- If there is any infringement of react-intl-universal-extract, please contact the contributor in time to delete the copy and related content

- Extract default messages in application using [react-intl-universal](https://github.com/alibaba/react-intl-universal).
- Support key to obtain Chinese key-value pairs

![react-intl-universal-extract](https://img.alicdn.com/imgextra/i4/O1CN01v8z1us1xc6TAXlHBC_!!6000000006463-2-tps-738-340.png)

Expand All @@ -9,6 +14,7 @@ For example, suppose you have a file:
render() {
const name = 'Tony';
return (<>
{intl.get('hello')}
{intl.get('hello1').d('Hello World')}
{intl.get('hello2', {name} ).d(`Hello ${name}`)}
</>);
Expand Down
5 changes: 3 additions & 2 deletions packages/react-intl-universal-extract/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-intl-universal-extract",
"version": "1.0.3",
"name": "react-intl-universal-extract-pluto",
"version": "1.0.5",
"description": "A react-intl-universal message extractor",
"keywords": [
"intl",
Expand All @@ -23,6 +23,7 @@
"preversion": "npm run clean && npm run build && npm run test",
"prepublish": "npm run clean && npm run build"
},
"homepage": "https://github.com/hongdeyuan/react-intl-universal#readme",
"license": "BSD-3-Clause",
"dependencies": {
"chalk": "2.1.0",
Expand Down
83 changes: 2 additions & 81 deletions packages/react-intl-universal-extract/src/extract/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,87 +16,8 @@ test("Test extract", () => {
// verbose: true,
});

expect(getOriginal(result, 'basic1')).toBe('default message');
expect(getTransformed(result, 'basic1')).toBe('default message');

expect(getOriginal(result, 'basic2')).toBe('Default message for basic[2]');
expect(getTransformed(result, 'basic2')).toBe('Default message for basic[2]');

expect(getOriginal(result, 'basic3')).toBe('Default message for basic(3)');
expect(getTransformed(result, 'basic3')).toBe('Default message for basic(3)');

expect(getOriginal(result, 'basic4')).toBe('Default message for basic(4) with 中文');
expect(getTransformed(result, 'basic4')).toBe('Default message for basic(4) with 中文');

expect(getOriginal(result, 'basic5')).toBe('Default message for basic5 with 中文(5)');
expect(getTransformed(result, 'basic5')).toBe('Default message for basic5 with 中文(5)');

expect(getOriginal(result, 'basic6')).toBe('Default message for basic6 with "123(中文)"');
expect(getTransformed(result, 'basic6')).toBe('Default message for basic6 with "123(中文)"');

expect(getOriginal(result, 'character1')).toBe('Expression support: ==,!=,>=,>,<=,<,&&,!,(),+,-,*,/,%');
expect(getTransformed(result, 'character1')).toBe('Expression support: ==,!=,>=,>,<=,<,&&,!,(),+,-,*,/,%');

expect(getOriginal(result, 'html1')).toBe('<span>This is html</span>');
expect(getTransformed(result, 'html1')).toBe('<span>This is html</span>');

expect(getOriginal(result, 'html2')).toBe('<span>This is html</span>');
expect(getTransformed(result, 'html2')).toBe('<span>This is html</span>');

expect(getOriginal(result, 'html3')).toBe('<span>(This is html)</span>');
expect(getTransformed(result, 'html3')).toBe('<span>(This is html)</span>');

expect(getOriginal(result, 'html4')).toBe('<span>Hello4 {name}</span>');
expect(getTransformed(result, 'html4')).toBe('<span>Hello4 {name}</span>');

expect(getOriginal(result, 'html5')).toBe('<span>Hello5 {name}, welcome to <a href={webLink}>my website!</a></span>');
expect(getTransformed(result, 'html5')).toBe('<span>Hello5 {name}, welcome to <a href={webLink}>my website!</a></span>');

expect(getOriginal(result, 'html6')).toBe('<span>welcome to <a href="https://www.npmjs.com/package/react-intl-extract">my website</a></span>');
expect(getTransformed(result, 'html6')).toBe('<span>welcome to <a href="https://www.npmjs.com/package/react-intl-extract">my website</a></span>');

expect(getOriginal(result, 'html7')).toBe('<span>Hello7 {name}</span>');
expect(getTransformed(result, 'html7')).toBe('<span>Hello7 {name}</span>');

expect(getOriginal(result, 'html8')).toBe('<span>html8</span>');
expect(getTransformed(result, 'html8')).toBe('<span>html8</span>');

expect(getOriginal(result, 'html9')).toBe('<span>html9</span>');
expect(getTransformed(result, 'html9')).toBe('<span>html9</span>');

expect(getOriginal(result, 'newline1')).toBe('Default message for newline1');
expect(getTransformed(result, 'newline1')).toBe('Default message for newline1');

expect(getOriginal(result, 'newline2')).toBe('Default message for newline2');
expect(getTransformed(result, 'newline2')).toBe('Default message for newline2');

expect(getOriginal(result, 'newline3')).toBe('Default message for newline1');
expect(getTransformed(result, 'newline3')).toBe('Default message for newline1');

expect(getOriginal(result, 'newline4')).toBe('Default message for newline2');
expect(getTransformed(result, 'newline4')).toBe('Default message for newline2');

expect(getOriginal(result, 'same-same_line2')).toBe('Default message for same_line2');
expect(getTransformed(result, 'same-same_line2')).toBe('Default message for same_line2');

expect(getOriginal(result, 'same_line1')).toBe('Default message for same_line1');
expect(getTransformed(result, 'same_line1')).toBe('Default message for same_line1');

expect(getOriginal(result, 'var1')).toBe('Hello1, ${name}. Welcome to ${where}!');
expect(getTransformed(result, 'var1')).toBe('Hello1, {name}. Welcome to {where}!');

expect(getOriginal(result, 'var2')).toBe('你好2, ${name}. 欢迎来到 ${where}!');
expect(getTransformed(result, 'var2')).toBe('你好2, {name}. 欢迎来到 {where}!');

expect(getOriginal(result, 'var3')).toBe('Hello3, ${name}. Welcome to ${where}!');
expect(getTransformed(result, 'var3')).toBe('Hello3, {name}. Welcome to {where}!');

expect(getOriginal(result, 'var4')).toBe('你好4, ${name}. 欢迎来到 ${where}!');
expect(getTransformed(result, 'var4')).toBe('你好4, {name}. 欢迎来到 {where}!');

expect(getOriginal(result, 'var5')).toBe('Hello5, ${name}. Welcome to ${where}!');
expect(getTransformed(result, 'var5')).toBe('Hello5, {name}. Welcome to {where}!');

expect(getOriginal(result, 'basic1')).toBe('basic1');
expect(getTransformed(result, 'basic1')).toBe('basic1');
});


Expand Down
7 changes: 2 additions & 5 deletions packages/react-intl-universal-extract/src/util/constant.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@

const DETECT_REGEXP = /(intl|IntlUtils)\s*\.\s*(get\s*\(\s*["'`]([\w\.-]+)["'`][\s\S]*?\)\s*\.\s*(defaultMessage|d)\s*\(\s*[`"']([\s\S]+?)[`"']|getHTML*\s*\(\s*["'`]([\w\.-]+)["'`][\s\S]*?\)\s*\.\s*(defaultMessage|d)\s*\(\s*(["'`<][\s\S]+?["'`>]))[\s*,]*\)/gm;

const NO_DEFAULT_REGEXP = /intl\s*\.\s*get(HTML)*\s*\(\s*["'`]([\w\.-]+)["'`]['"\w\.,{}:\s-]*\)\s*(?!\s*\.\s*(d|defaultMessage)\s*\(\s*[<'"`\s\\]+)/gm;
const DETECT_REGEXP = /(intl|IntlUtils)\s*\.\s*(get\s*\(\s*["'`]([\s\S]+?)["'`][\s\S]*?\)\s*|getHTML\s*\(\s*["'`]([\s\S]+?)["'`][\s\S]*?\))/gm;

const COMMENT_REGEXP = /(\/\/.*)|(\/\*[\s\S]*?\*\/)/gm;

module.exports = {
DETECT_REGEXP,
COMMENT_REGEXP,
NO_DEFAULT_REGEXP
COMMENT_REGEXP
};
30 changes: 4 additions & 26 deletions packages/react-intl-universal-extract/src/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const pathtool = require('path');
const config = require('../../config');
const chalk = require('chalk');
const _ = require('lodash');
const { DETECT_REGEXP, NO_DEFAULT_REGEXP } = require('./constant');
const { DETECT_REGEXP } = require('./constant');


/**
Expand Down Expand Up @@ -66,8 +66,7 @@ function scanFiles(dirPath, options, ig) {
function processFile(path) {
logger.info('Processing file: ', path)
const content = fs.readFileSync(path, 'utf-8').toString();
const invalidMessages = getNoDefaultMessages(content, path);
const messages = extractMessages(content, path).concat(invalidMessages);
const messages = extractMessages(content, path);
messages.forEach((message) => logger.log(` - key="${message.key}" originalDefaultMessage="${message.originalDefaultMessage}" transformedDefaultMessage="${message.transformedDefaultMessage}"`));
return messages;
}
Expand All @@ -81,10 +80,10 @@ function extractMessages(content, path) {
const _messages = [];
let match;
while ((match = DETECT_REGEXP.exec(content)) != null) {
const keyIndex = match[3] ? 3 : 6;
const keyIndex = match[3] ? 3 : 4;
const messageIndex = match[5] ? 5 : 8;
let key = match[keyIndex];
let defaultMessage = match[messageIndex];
let defaultMessage = match[messageIndex] || key;

// Trim "${variable}" to "{variable}"
const shouldTrim = /\.(d|defaultMessage)\([\s\S]*`[\s\S]*\)/.test(match[0]);
Expand Down Expand Up @@ -135,26 +134,6 @@ function verifyMessages(messages) {
return isOK;
}

/**
* Given file content, get intl object has no default message.
* @param {string} content file content
* @param {string} filePath file path
*/
function getNoDefaultMessages(content, filePath) {
const _messages = [];
let match;
while ((match = NO_DEFAULT_REGEXP.exec(content)) != null) {
let key = match[2];
_messages.push({
key,
path: filePath,
isValid: false,
invalidType: 'no_default',
});
}
return _messages;
}

/**
* Transform variables in source defaultMessage (such as ES6 template strings) to target defaultMessage (such as ICU format)
* Example: 'Hello, ${name}. Welcome to ${where}!' -> 'Hello, {name}. Welcome to {where}!'
Expand Down Expand Up @@ -202,6 +181,5 @@ module.exports = {
processParameters,
scanFiles,
verifyMessages,
getNoDefaultMessages,
logger,
};
2 changes: 1 addition & 1 deletion packages/react-intl-universal-extract/test-files/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class App extends Component {
<div>{intl.get('basic1').defaultMessage('default message')}</div>
<div>{intl.get('basic2').d('Default message for basic[2]')}</div>
<div>{intl.get('basic3').d('Default message for basic(3)')}</div>
<div>{intl.get('basic4').d('Default message for basic(4) with 中文')}</div>
<div>{intl.get('特征4').d('Default message for basic(4) with 中文')}</div>
<div>{intl.get('basic5').d('Default message for basic5 with 中文(5)')}</div>
<div>{intl.get('basic6').d('Default message for basic6 with "123(中文)"')}</div>

Expand Down