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

[Research] osd translation issue #1582

Closed
ananzh opened this issue May 12, 2022 · 5 comments
Closed

[Research] osd translation issue #1582

ananzh opened this issue May 12, 2022 · 5 comments
Assignees

Comments

@ananzh
Copy link
Member

ananzh commented May 12, 2022

[Issue - 1]

Seems a lot of react components which use to translate are not working. (example)

Currently, we have two ways to translate the react components (did not research much on angular, since we will remove them sooner or later): 1) a <FormattedMessage> component 2) a global imported method i18n.translate.

So far the issues are:

  1. <FormattedMessage> component not working
  2. i18n.translate does not work on formatTime\Date\Plural etc.

In this research, I would like to investigate on two questions:

  1. why we need two duplicate ways to translate? can we remove one?
  2. why <FormattedMessage> component is not working? if we need it, how to fix it?
@ananzh ananzh added enhancement New feature or request needs research and removed enhancement New feature or request labels May 12, 2022
@ananzh ananzh self-assigned this May 12, 2022
@ananzh
Copy link
Member Author

ananzh commented May 19, 2022

There are two ways to translate strings:

1. i18n.translate method
I18n is first initialized by setting up locale if there is a customized one. Otherwise, it will set the locale to default en.
After i18n is initialized, when i18n.translate method is called, it will call getMessageById and pass the message id.
This getMessageById will first load all the translations like below by getTranslation method. Then it use translationsForLocale[currentLocale].messages[id] to translate.

translationsForLocale[currentLocale]: { formats:
{ number: { currency: [Object], percent: [Object] },
date:
{ short: [Object],
medium: [Object],
long: [Object],
full: [Object] },
time:
{ short: [Object],
medium: [Object],
long: [Object],
full: [Object] },
relative:
{ years: [Object],
months: [Object],
days: [Object],
hours: [Object],
minutes: [Object],
seconds: [Object] } },
locale: 'demo',
messages:
{ 'console.devToolsDescription': '跳过 cURL 并使用 JSON 接口在控制台中处理您的数据。',
'console.devToolsTitle': '与 OpenSearch API 进行交互',
'core.ui.welcomeMessage': '正在加载 OpenSearch',
'dashboard.featureCatalogue.dashboardSubtitle': '在控制板中分析数据。',
'discover.discoverSubtitle': '搜索和查找数据分析结果。',
'home.addData.sectionTitle': '获取你的数据',
'home.breadcrumbs.homeTitle': '主页',
'home.header.title': '欢迎归来',
'home.manageData.sectionTitle': '管理你的数据',
'home.tutorialDirectory.featureCatalogueDescription': '从热门应用和服务中采集数据。',
'home.tutorialDirectory.featureCatalogueTitle': '添加数据',
'opensearchDashboardsOverview.opensearchDashboards.solution.subtitle': '可视化和分析',
'opensearch-dashboards-react.osdOverviewPageHeader.addDataButtonLabel': '添加数据',
'opensearch-dashboards-react.osdOverviewPageHeader.devToolsButtonLabel': '开发工具',
'opensearch-dashboards-react.osdOverviewPageHeader.stackManagementButtonLabel': '管理',
'opensearch-dashboards-react.pageFooter.appDirectoryButtonLabel': '查看应用目录',
'opensearch-dashboards-react.pageFooter.changeHomeRouteLink': '登录时显示不同页面',
'discover.noResults.searchExamples.noResultsMatchSearchCriteriaTitle': '没有结果' } }

2.ForfamttedMessage
The <FormattedMessage /> is a react components in react-intl to translate string for other react components. For it to work, you first need to prepare a locale provided by react-intl and specifies the date, time, number and etc. The locale data is needed to be registered by addLocaleData() ( packages/osd-i18n/src/core/locales.js ).

@ananzh
Copy link
Member Author

ananzh commented May 19, 2022

Therefore for the previous two questions:
why we need two duplicate ways to translate? can we remove one?
--> ForfamttedMessage is used to translate react component and require some setup. i18n.translate is easier to use and only requires the translation json file. But it could not translate date, time, number and some formats.

why component is not working? if we need it, how to fix it?
--> <FormattedMessage> needs to setup in packages/osd-i18n/src/core/locales.js . For example, if your translation file is called demo.json and is chinese, then you need to add below at the bottom.

addLocaleData({ locale: "demo", pluralRuleFunction: function (n,ord){if(ord)return"other";return"other"},"fields":{"year":{"displayName":"年","relative":{"0":"今年","1":"明年","-1":"去年"},"relativeTime":{"future":{"other":"{0}年后"},"past":{"other":"{0}年前"}}},"year-short":{"displayName":"年","relative":{"0":"今年","1":"明年","-1":"去年"},"relativeTime":{"future":{"other":"{0}年后"},"past":{"other":"{0}年前"}}},"month":{"displayName":"月","relative":{"0":"本月","1":"下个月","-1":"上个月"},"relativeTime":{"future":{"other":"{0}个月后"},"past":{"other":"{0}个月前"}}},"month-short":{"displayName":"月","relative":{"0":"本月","1":"下个月","-1":"上个月"},"relativeTime":{"future":{"other":"{0}个月后"},"past":{"other":"{0}个月前"}}},"day":{"displayName":"日","relative":{"0":"今天","1":"明天","2":"后天","-2":"前天","-1":"昨天"},"relativeTime":{"future":{"other":"{0}天后"},"past":{"other":"{0}天前"}}},"day-short":{"displayName":"日","relative":{"0":"今天","1":"明天","2":"后天","-2":"前天","-1":"昨天"},"relativeTime":{"future":{"other":"{0}天后"},"past":{"other":"{0}天前"}}},"hour":{"displayName":"小时","relative":{"0":"这一时间 \u002F 此时"},"relativeTime":{"future":{"other":"{0}小时后"},"past":{"other":"{0}小时前"}}},"hour-short":{"displayName":"小时","relative":{"0":"这一时间 \u002F 此时"},"relativeTime":{"future":{"other":"{0}小时后"},"past":{"other":"{0}小时前"}}},"minute":{"displayName":"分钟","relative":{"0":"此刻"},"relativeTime":{"future":{"other":"{0}分钟后"},"past":{"other":"{0}分钟前"}}},"minute-short":{"displayName":"分","relative":{"0":"此刻"},"relativeTime":{"future":{"other":"{0}分钟后"},"past":{"other":"{0}分钟前"}}},"second":{"displayName":"秒","relative":{"0":"现在"},"relativeTime":{"future":{"other":"{0}秒钟后"},"past":{"other":"{0}秒钟前"}}},"second-short":{"displayName":"秒","relative":{"0":"现在"},"relativeTime":{"future":{"other":"{0}秒后"},"past":{"other":"{0}秒前"}}}} });

@ananzh
Copy link
Member Author

ananzh commented May 19, 2022

Action item:

  1. For i18n-plugin, setup demo locale to packages/osd-i18n/src/core/locales.js.
  2. Add some words in i18n package README.

@ananzh
Copy link
Member Author

ananzh commented May 19, 2022

Others:

If you see this below error or similar, it means you haven't register your locale. You need to add it in packages/osd-i18n/src/core/locales.js.
Screen Shot 2022-05-18 at 19 52 08

If you see this diff error or similar, it means something is missing from your translation file. You can get what is missing from the message. Just need to add it to the translation file.
Screen Shot 2022-05-18 at 20 23 31

ananzh added a commit to ananzh/OpenSearch-Dashboards that referenced this issue May 20, 2022
*register demo locale for i18n-plugin which provides a
demo translation file.
*modify README to add register requirement. without register
the locale, the translations on all react components using
FormattedMessage, FormattedNumber and etc will fail.

Issue Resolved:
opensearch-project#1582

Signed-off-by: Anan Zhuang <[email protected]>
ananzh added a commit to ananzh/OpenSearch-Dashboards that referenced this issue May 23, 2022
*register demo locale for i18n-plugin which provides a
demo translation file.
*modify README to add register requirement. without register
the locale, the translations on all react components using
FormattedMessage, FormattedNumber and etc will fail.

Issue Resolved:
opensearch-project#1582

Signed-off-by: Anan Zhuang <[email protected]>
ananzh added a commit to ananzh/OpenSearch-Dashboards that referenced this issue Jun 3, 2022
*register demo locale for i18n-plugin which provides a
demo translation file.
*modify README to add register requirement. without register
the locale, the translations on all react components using
FormattedMessage, FormattedNumber and etc will fail.

Issue Resolved:
opensearch-project#1582

Signed-off-by: Anan Zhuang <[email protected]>
@joshuarrrr
Copy link
Member

@ananzh Can we close this now? I think you made the necessary doc updates, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants