Skip to content

Resolve "I18N - scan JH folder to generate po file"

What does this MR do and why?

Gem gettext_i18n_rails 提供了一个 files_to_translate 方法,可以用来扫描自定义文件夹。

文件 extract_gettext_all.js 是从 upstream 拷贝过来的,这个文件改动的频率比较低,有一个 follow-up issue #1903 用来处理后续的部分.

Proposal

  1. 新建一个 rake task jh:gettext:regenerate 用来生成极狐对应的 message entry。
  2. 使用 JH| 作为极狐的 msgid 前缀

The following rules are applied in this task.

Case Exists in JH Directories JH namespace JH PO Upstream PO Action
In JH Codes without JH namespace YES No Error
In JH Codes with JH namespace YES Yes Keep
Unused JH msg NO YES YES Remove
Overridden keys NO NO YES YES Keep
Overridden keys removed by upstream NO NO YES NO Error

使用 'JH|' 而不是 'JH_'

最终选择使用'JH|'.

方案1:我们每次新增 msgid 需要在 en/gitlab.po 里把所有的带有极狐前缀的 msgid,如 JH_username 都要手动加入对应的翻译值。

方案2:Gettext 本身已经支持 namespace。我们只需要使用新的翻译的方法名即可,如 s_()/ns_(),而不是_()

Usages of JH_

_('JH_PerformanceAnalytics')
=> "JH_PerformanceAnalytics"
s_('JH_PerformanceAnalytics|Last')
=> "Last"
ns_('JH_PerformanceAnalytics|Last %d day', 'JH_PerformanceAnalytics|Last %d days', 10)
=> "Last %d days"

Usages of JH|

_('JH|PerformanceAnalytics|Last')
=> "JH|PerformanceAnalytics|Last"
[4] pry(main)> s_('JH|PerformanceAnalytics|Last')
=> "Last"
[1] pry(main)> ns_('JH|PerformanceAnalytics|Last %d day', 'JH|PerformanceAnalytics|Last %d days', 10)
=> "Last %d days"

如何解决 Rake task 对应的错误

Msgid without "JH|" is found

这个错误表明:在jh目录下面发现了,不带有JH|前缀的msgid。需要在源码里加上JH|并且重新运行bundle exec rails jh:gettext:regenerate

错误示例 Msgid without "JH|" is found: A demo project showing how DongTai IAST integrates into DevOps for detecting vulnerabilities, Access token cannot be blank..

Unattached po entry is found

这个错误表明:在极狐的 po 文件里发现了孤立的不带有JH|前缀的 msgid, 这个 msgid 既没有出现在 jh 目录的源码里,也没有出现在 upstream 的源码里。出现这种情况的原因是:我们在极狐这边复写了 upstream 的翻译的值,比如把 GitLab 覆盖为 极狐GitLab,但是 upstream 在源码里改掉/删掉这个这个msgid,需要手动处理,如果只是改掉,我们需要在极狐这边也把这个 msgid 改掉。

错误示例 Unattached po entry "Captcha|Please click here to finish the verification" is found in locale :en.

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Closes #1116 (closed)

Martin Tan 编辑于

合并请求报告