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

feat: i18n for Iceworks-component-builder #301

Merged
merged 11 commits into from
Jul 22, 2020

Conversation

sspku-yqLiu
Copy link
Collaborator

No description provided.

@sspku-yqLiu
Copy link
Collaborator Author

存在一个缺陷:当一开始不能连接到物料库时,会在国际化之前进行一轮报错,所以会出现两轮报错,一轮中文 一轮英文,其余良好。

@sspku-yqLiu sspku-yqLiu self-assigned this Jul 21, 2020
@luhc228 luhc228 changed the title feat: i18n for Iceworks-component-generater feat: i18n for Iceworks-component-builder Jul 21, 2020
@sspku-yqLiu
Copy link
Collaborator Author

sspku-yqLiu commented Jul 21, 2020

关于提到的缺陷:

示例:

demo

错误产生条件:

  1. 断掉网线(或用任何方式断开网络)
  2. 用调试模式打开 i18n/iceworks-component-builder 分支中的 component-builder 插件
  3. 在调试窗口中输入: generate-componet ,选择进入生成组件页面
  4. 这时由于1中「断开网络」的环境,会导致出现无法连接到组建服务器的错误。

错误输出与期待输出

  • 错误输出
    在 demo 中可以看到,输出了两个提示,一个中文(默认页面)一个英文(刷新页面)。
  • 期望输出
    仅有「刷新页面」后的提示,即只有一次中文或英文提示。

错误推断

  • 出现错误的可能原因
    出现两次输出的原因是因为本页面存在一次刷新,即先使用中文版生成基础页面,待插件发送 VSCode 使用语言后根据语言刷新为用户使用的语言。

  • 代码定位

      async function getData(source: string) {
        let data = {};
        try {
          data = await callService('material', 'getData', source);
        } catch (e) {
          Notification.error({ content: intl.formatMessage({id:'web.ComponentBuiilder.Home.getDataError'})});
        }
        console.log('getData', data);
        return data;
      }
      

    这个函数检测是否能够从云上下载物料,并且对用户提供反馈,由于出现错误,就会使用 Notification.error进行提示。这里面触发了两次提示。

    由于父组件 <LocalProvider> 收到 callService 的返回值,改变了 intl 从而进行了一轮刷新。第二次提示可能是由于 intl 刷新触发的。

  • 为了解决这个错误的努力

    我尝试在跟组件添加 loading 判别器,期待通过是否加载的判断防止为更新语言的界面加载。

        <RawIntlProvider value={i18n}>
          <ConfigProvider>
            {loading?<Loading visible={loading} style={{width: '100%', height:'80vh'}} /> :React.Children.only(props.children)}
          </ConfigProvider>
        </RawIntlProvider>

    失败原因推测:需要了解 react 加载原理, 似乎是先加载组建而后根据判别式来判定是否进行渲染.

    尝试使用 await 阻塞异步函数

      await callService('common', 'getLanguage');
      sources = await callService('material', 'getSourcesByProjectType')

    并没有阻塞成功。

  • 暂时的思路

    通过某种方式,比如说使用一个 flag ,让第一轮刷新的时候不出现提示,仅在第二轮刷新的时候进行提示。

    例如:

    let flag = false;
    ...
      async function getData(source: string) {
      if(!flag){
        flag = true;
        return;
      }
        let data = {};
        try {
          data = await callService('material', 'getData', source);
        } catch (e) {
          Notification.error({ content: intl.formatMessage({id:'web.ComponentBuiilder.Home.getDataError'})});
        }
        console.log('getData', data);
        return data;
      }

    当然 还是希望有一个比较优雅的办法的...

@sspku-yqLiu
Copy link
Collaborator Author

补充,为什么说上面的解决方案不优雅

因为实际上存在着多个类似的检验函数,所以需要为每一个类似的组件分配一个 flag ,看起来就像:

let defaultFlag1 = true;
let defaultFlag2 = true ;  
  async function getSources() {
    if(defaultFlag1){
      defaultFlag1 = false;
      return;
    }  
    ...
  async function getData(source: string) {
    if(defaultFlag2){
      defaultFlag2 = false;
      return;
    }
    ...

@sspku-yqLiu
Copy link
Collaborator Author

进一步的思考

能不能利用一种东西来进行状态判别呢?一个想法是 intl.message 中添加一个 status 属性,但是这显然有些混淆了,我希望每个功能是分开的。

那么 我尝试利用 locale 进行传值:

i8n.tsx:

// 初始化时 locale 为 default, 这会使得它自动转换为 en 模式, 并且保留 local 属性为 default
export const defaultIntl = createIntl({locale:'default',messages:localeMessages['zh-cn'].messages})

//判别函数:判断是否国际化进程结束
export function checkI18nReady(intl: IntlShape){
  return intl.locale !== 'default'
}

使用:

async function getSources() {
    if(!checkI18nReady(intl)){
      return;
    }
  ...

这在运行的时候是比较良好的,但是麻烦在于,你必须找到所有这种类型的函数,并给他加一个判断。也许存在一种方法可以直接影响所有这类的函数而不是冲进去给他加个判断,如果找不到理想状态下的方法,我倾向于使用这种方法。

@sspku-yqLiu sspku-yqLiu force-pushed the i18n/iceworks-component-builder branch from 9b921ae to 0fd22be Compare July 21, 2020 14:56
@alvinhui alvinhui self-requested a review July 22, 2020 03:17
@alvinhui
Copy link
Contributor

reviewed

@alvinhui alvinhui merged commit ba92e99 into feat/i18n Jul 22, 2020
@delete-merged-branch delete-merged-branch bot deleted the i18n/iceworks-component-builder branch July 22, 2020 07:37
alvinhui added a commit that referenced this pull request Jul 27, 2020
* feat: apply repo

* feat: convert ts2js

* fix: fix type name

* chore: remove console

* chore: version

* chore: remove code

* refactor: remove iceworks-cli

* feat: support transform ts to js

* chore: version

* refactor: rename Iceworks Material Helper to React Component Helper

* docs: update logo

* chore: lint

* feat: i18n 功能包

* feat: extension 示例 iceworks-app 国际化

* docs: typo

* chore: remove material-import package.json

* feat: add package transform-ts-to-js to tsconfig.json

* feat: 改变了编译顺序,将 i18n package 导出的实体变为导出类。

* feat: web 示例 iceworks-app 设置页面

* refactor: 根据 i8n 的新导出方式进行更新,删除无用行,更改模板及引用方式

* feat: 使用 useEffect 函数进行异步加载

* feat: 改变了编译顺序,将 i18n package 导出的实体变为导出类。

* refactor: move export to bottom

* feat: i18n for packages/block-service

* refactor: format codes and delete useless file `util/i18n.ts`

* feat: replace injectIntl with `useIntl` hook and format codes

* feat: addLocalProvider To Pages/*/index for i18n

* refactor: add `[]` param to useEffect, update en-US JSON and app.tsx

* feat: add ui

* feat: support ejs options

* fix: error styles

* fix: lint

* feat: i18n for packages/project-service

* refactor: uv/pv logic

* refactor: pv|uv

* style: camel case

* chore: add comments

* chore: version & change log

* Hotfix: rax scaffolds title app type icon disappear (#296)

* fix: rax scaffolds title app type icon disappear

* chore: version

* refactor: do not export recordPV and recordUV, using record

* fix: add i18n dep to package.json

* feat: types

* feat: i18n for packages/page-service

* feat: i18n for packages/material-service

* fix: ejs options

* fix: useState name

* feat: i18n for packages/component-service

* chore: generate-project version

* feat: i18n for project-creator

* feat: i18n for page-builder

* feat: i18n for material-helper

* feat: i18n for Iceworks-component-builder (#301)

* feat: i18n for Iceworks-component-generater

* fix: fix double alerts bug and add iceworks/i18n to dep

* fix: web i18n

* style: remove empty line

* fix: ignore i18n error

* chore: remove unnecessary code

* chore: remove unnecessary note

* feat: support next locale

* style: indent

* fix: delete redundant codes

Co-authored-by: alvinhui <alvin.hui@qq.com>

* refactor: update i18n in extension and i18n.tsx in web

* fix: default locale

* chore: version

* chore: types

* refactor: 0.1.0 should be first version

* chore: url extraction as constant (#318)

* chore: url extraction as constant

* style: indent

* fix: en-US errors

* feat: support webapp and assets def project (#299)

* docs: update gifs for en-US (#325)

* fix: update i18n translations

* docs: update i18n doc gifs (#324)

* docs: update i18n doc gifs

* docs: update component builder gifs

* docs: update material-helper and page-bulder doc gifs

* fix: fail to debug extension in development env (#326)

* fix: fail to debug extension in development

* fix: fail-to-debug-extension-in-development

* docs: change slogan for en-US (#327)

* docs: add markdownlint to ci and update docs (#328)

* fix: update i18n translations

* docs: update i18n doc gifs (#324)

* docs: update i18n doc gifs

* docs: update component builder gifs

* docs: update material-helper and page-bulder doc gifs

* feat: add docslInt in ci

* docs: update docs due to markdownlint

* fix: update material helper demo

* fix: fix URl to URL

* fix: fail to install deps when import materials (#329)

* fix: create a new terminal to install deps

* chore: remove repeated bulkDownload code

* chore: version

* refactor: update code due to code style (#331)

* chore: record locale (#332)

* docs: change slogan for en-US

* chore: record locale

* fix: fail get material detail in unknown project (#333)

* fix: fail to get material detail in unknown project

* fix: change specifiedType in getSources method

* chore: version

* feat: get user info through def client login (#323)

* feat: def login client

* feat: get user info in web

* fix: eslint

* feat: get def-login-client zip

* feat: add install def-login-client-deps

* style: change IIFE

* fix: style

* chore: accesskey

* fix: access key not found in ci setup

* fix: ci

* fix: fix by the comment

* fix: lint

* feat: not run the script when key unexist

* fix: lint

* refactor: encode alibaba-inc (#335)

* refactor: encode constants with 'alibaba-Inc'

* fix: add js-base dep to package.json

* fix: eslint err

* fix: use namespace to make test work

* feat: add docs check to CI (#338)

* chore: change version add CHANGLOG

* fix: package publishConfig

* fix: extension build size is  too bulky (#340)

* fix: en-US translation (#339)

* fix: change material-import to material-helper (#341)

* fix: beta and prod publish env (#343)

* chore: title typo (#342)

* chore: title typo

* chore: settings

* fix: gitlab url (#345)

* chore: typo

Co-authored-by: yangfan <18767120422@163.com>
Co-authored-by: alvinhui <alvin.hui@qq.com>
Co-authored-by: sspku-yqLiu <liuyongqi@pku.edu.cn>
Co-authored-by: sspku-yqLiu <56879942+sspku-yqLiu@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants