alibaba / react-intl-universal

Internationalize React apps. Not only for Component but also for Vanilla JS.
1.33k stars 154 forks source link

react-intl-universal-extract work wrong for some situation!!! #174

Closed pumpkinduan closed 2 years ago

pumpkinduan commented 2 years ago

操作流程与业务代码

  1. 如下为编写的业务代码,在一个.tsx文件中:
    
    const radioConfig = [
    {
        title: intl.get('meet_room.room_booking.simple_repeat_type_never'),
        value: Type.never,
    },
    {
        title: intl.get('meet_room.room_booking.simple_repeat_type_daily'),
        value: Type.daily,
    },
    {
        title: intl.get('meet_room.room_booking.simple_repeat_type_workday').d('TEST'),
        value: Type.workday,
    },
    {
        title: intl.get('meet_room.room_booking.simple_repeat_type_weekly'),
        value: Type.weekly,
    },
    {
        title: intl.get('meet_room.room_booking.simple_repeat_type_monthly'),
        value: Type.monthly,
    },
    {
        title: intl.get('meet_room.room_booking.simple_repeat_type_custom').d('Custom'),
        value: Type.custom,
    },
    ];

const Component = () => { const header = useMemo(() => { return { title: !isSelectedCustomMode() ? intl.get('meet_room.room_booking.circle_tip') : intl.get('meet_room.room_booking.simple_repeat_type_custom').d('Custom'), left: , right: (

{intl.get('common.button.ok')}
        ),
    };
}, [handleConfirm, handleGoBack, isSelectedCustomMode])

}

2. 我需要提取key-value至json文件中,理想情况下是生成这样的json:
```json
{
    "meet_room.room_booking.simple_repeat_type_custom": "Custom",
    "meet_room.room_booking.simple_repeat_type_workday": "TEST"
}
  1. 但由于不兼容intl.get('key')的写法,必须写intl.get('key').d('value'),脚本才会正常提取文案,原因是如下代码: image

  2. 所以为了兼容原来的写法:intl.get('key'),校验message没有默认值时的错误处理,我暂时关闭了, 且过滤掉了没有默认值的message,如下图: image

  3. 然后 执行 react-intl-universal-extract --cmd extract --source-path ./src --output-path ./locales/cn.json

  4. 最后生成出来的json文件,部分key值与value对应不上,如下:

    {
     "meet_room.room_booking.circle_tip": "Custom",
     "meet_room.room_booking.simple_repeat_type_never": "TEST",
     "meet_room.room_booking.simple_repeat_type_weekly": "Custom"
    }

疑问???我打印了extractMessage执行时,正则匹配的结果,如下:

image

image 我想,match[3]应该为'meet_room.room_booking.simple_repeat_type_workday'这个key才对,但实际却是‘meet_room.room_booking.simple_repeat_type_never’ 是这个匹配的正则出了问题,还是这个工具的机制是这样呢?

cwtuan commented 2 years ago

All of the .d or .defaultMessage is required for extracting message.

serfgy commented 2 years ago

intl.get('common.special').d('special') are you saying the .d is compulsory for all .get()?

cwtuan commented 2 years ago

intl.get('common.special').d('special') are you saying the .d is compulsory for all .get()?

Yes, it is.