You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用babel-plugin-content-transformer时YAML修改需重启Metro服务器

问题:React Native中使用babel-plugin-content-transformer加载YAML做i18n,修改后需重启Metro才能生效

我使用babel-plugin-content-transformer库加载YAML文件替代JSON,在react-i18next本地化库中使用。功能正常,但存在问题:修改.yml文件的翻译内容后,仅重启Metro服务器才能看到变更,按'r'刷新无效,必须停止并重启服务器。使用JSON文件时一切运行正常。

我的babel.config.js配置

module.exports = function (api) {
  const presets = [['module:metro-react-native-babel-preset']]
  const plugins = [
    ['module-resolver', {
      root: ['./'],
      extensions: ['.ios.js', '.android.js', '.js', '.ts', '.tsx', '.json'],
      alias: {
        '@bluecentury': './src',
      }
    }],
    ['module:react-native-dotenv', {
      'moduleName': '@vemasys/env',
    }],
    ['react-native-reanimated/plugin'],
    ['content-transformer', {
      transformers: [{
        file: /\.ya?ml$/,
        format: 'yaml'
      }]
    }]
  ]
  api.cache(false)
  return {
    presets,
    plugins
  }
}

我的i18n.ts配置

import i18n from 'i18next'
import {initReactI18next} from 'react-i18next'
import translationEN from './enyaml.yml'
// import translationEN from './en.json'

const resources = {
  en: {
    translation: translationEN,
  },
}

i18n.use(initReactI18next).init({
  resources,
  lng: 'en',
  fallbackLng: 'en',
  interpolation: {
    escapeValue: false,
  },
})

内容的提问来源于stack exchange,提问作者Stas Motorny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 02:25:53