【问题标题】:How to override react-admin existing translation如何覆盖 react-admin 现有的翻译
【发布时间】:2022-01-01 00:59:44
【问题描述】:

来自demo的App.tsx

import englishMessages from './i18n/en';
const i18nProvider = polyglotI18nProvider(locale => {
    if (locale === 'fr') {
        return import('./i18n/fr').then(messages => messages.default);
    }

    // Always fallback on english
    return englishMessages;
}, 'en');

const App = () => {
    return (
        <Admin
           ....
            i18nProvider={i18nProvider}
          ....

zh.ts

const customEnglishMessages: TranslationMessages = {
    ...englishMessages,
    pos: {
        search: 'Search',
        ....

packages>ra-language-english>src>index.ts

const englishMessages: TranslationMessages = {
    ra: {
    action:{...},
    boolean{...},
    page: {
          create: 'Create %{name}',
          dashboard: 'Dashboard',

如何在 en.ts 中重写现有的翻译“ra.page.dashboard”?

如果我在解构englishMessage后添加相同的结构

export const messages = {
    ...englishMessages,
    ra: {
        page: {
            dashboard: 'test edit dashboard'
        }
    }
};

我发现一个错误

ERROR in [at-loader] ./src/client/components/App/index.tsx:33:43
    TS2345: Argument of type '(locale: string) => { ra: { page: { dashboard: string; }; }; } | Promise<TranslationMessages | { resources: { posts: { name: string; fields: { average_note: string; body: string; comments: string; ... 12 more ...; authors: string; }; }; comments: { ...; }; users: { ...; }; }; ... 4 more ...; simple: { ...; }; }>' is not assignable to parameter of type 'GetMessages'.
  Type '{ ra: { page: { dashboard: string; }; }; } | Promise<TranslationMessages | { resources: { posts: { name: string; fields: { average_note: string; body: string; comments: string; commentable: string; ... 11 more ...; authors: string; }; }; comments: { ...; }; users: { ...; }; }; ... 4 more ...; simple: { ...; }; }>' is not assignable to type 'TranslationMessages | Promise<TranslationMessages>'.
    Type '{ ra: { page: { dashboard: string; }; }; }' is not assignable to type 'TranslationMessages | Promise<TranslationMessages>'.
      Type '{ ra: { page: { dashboard: string; }; }; }' is not assignable to type 'TranslationMessages'.
        Types of property 'ra' are incompatible.
          Type '{ page: { dashboard: string; }; }' is missing the following properties from type '{ action: { [key: string]: string | StringMap; add_filter: string; add: string; back: string; bulk_actions: string; cancel: string; clear_input_value: string; clone: string; confirm: string; ... 23 more ...; move_down: string; }; ... 8 more ...; validation: { ...; }; }': action, boolean, input, message, and 5 more.

【问题讨论】:

    标签: reactjs react-admin


    【解决方案1】:

    解决方案是使用deepmerge 来合并数组

    const newValues = {
        ra: {
            page: {
                dashboard: "test edit dashboard"
            }
        }
    }
    const messages = deepmerge.all([englishMessages, newValues]) 
                                                          as TranslationMessages;
    export default messages;
    

    第一个答案(不正确),这种方法会重写englishMessages中的所有值

    在any 中进行转换修复了构建错误

    export const messages = {
        ...englishMessages,
        ra: {
            page: {
                dashboard: 'test edit dashboard'
            }
        } as any
    };
    

    【讨论】:

      【解决方案2】:

      使用类型TranslationMessages

      所以,删除 Ra 对象并直接添加您的翻译

      export const messages : TranslationMessages  = {
          ...englishMessages,
          page: {
              dashboard: 'test edit dashboard'
          }
      };
      

      下面的旧的和不好的响应

      export const messages : TranslationMessages  = {
          ...englishMessages,
          ra: {
              page: {
                  dashboard: 'test edit dashboard'
              }
          }
      };
      

      【讨论】:

      • 通过向 TranslationMessages 添加强制转换它给出了一个错误:Type '{ dashboard: string; }' 缺少来自类型 '{ [key: string]: string | 的以下属性字符串映射;创建:字符串;仪表板:字符串;编辑:字符串;错误:字符串;列表:字符串;加载:字符串; not_found:字符串;显示:字符串;空:字符串;邀请:字符串; }':创建、编辑、错误、列表等 5 个。
      • 是的,我更正了我的回答。使用此解决方案不要使用 Ra 对象,而是直接放置您的翻译对象
      • 在这种情况下'page:.dashboard'有不同的路径,因为在englishMessages路径是'ra.page:dashboard',而翻译'test edit dashboard'不适用,因为在DashboardMenuItem这个翻译被称为
      猜你喜欢
      • 2015-09-03
      • 2015-02-08
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多