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

如何在React Native项目中适配Angular格式的国际化翻译文件?

Got it, let's work through this—since you need to reuse those existing Angular translation files in React Native without modifying them, i18next is a great fit here because it’s super flexible with custom translation formats. Here’s a step-by-step solution:

Step 1: Set up i18next and react-i18next in your React Native project

First, install the core libraries:

npm install i18next react-i18next --save
# or with yarn
yarn add i18next react-i18next
Step 2: Transform your array-based translation files to i18next's key-value format

i18next expects translations to be structured as { key: "translation text" }, but your files are arrays of objects. Create a utility function to convert them:

// utils/i18nTransformer.js
export const transformTranslationData = (translationArray) => {
  return translationArray.reduce((formattedTranslations, item) => {
    // Clean up Angular's interpolation tags to match i18next syntax
    const cleanedDefinition = item.definition.replace(
      /<ph name="INTERPOLATION"><ex>{{.*?}}<\/ex>({{.*?}})<\/ph>/g,
      '$1'
    );
    formattedTranslations[item.term] = cleanedDefinition;
    return formattedTranslations;
  }, {});
};

This function does two things:

  • Converts the array into a key-value object using term as the key
  • Strips out Angular's <ph> interpolation wrapper, leaving just {{variable}} which i18next understands natively
Step 3: Initialize i18next with your transformed translations

Create an i18n configuration file (e.g., src/i18n.js):

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import enRaw from '../translations/en.json';
import deRaw from '../translations/de.json';
import { transformTranslationData } from './utils/i18nTransformer';

// Transform the raw array data to i18next-compatible format
const resources = {
  en: {
    translation: transformTranslationData(enRaw)
  },
  de: {
    translation: transformTranslationData(deRaw)
  }
};

i18n
  .use(initReactI18next) // Connect to react-i18next
  .init({
    resources,
    lng: 'en', // Set your default language
    interpolation: {
      escapeValue: false // React handles XSS protection natively
    }
  });

export default i18n;

Don't forget to import this config in your root App.js or index.js:

import './src/i18n';
Step 4: Use translations in your React Native components

Use the useTranslation hook to access translated text and interpolate variables:

import { View, Text, Button } from 'react-native';
import { useTranslation } from 'react-i18next';

const ExampleComponent = ({ alert }) => {
  const { t, i18n } = useTranslation();

  // Function to switch languages
  const switchLanguage = (lng) => {
    i18n.changeLanguage(lng);
  };

  return (
    <View>
      <Text>{t('modal.alert.close')}</Text>
      {/* Pass interpolation variables as an object */}
      <Text>{t('has-expired', { alert: { uniqueId: alert.uniqueId } })}</Text>
      
      <Button title="Switch to German" onPress={() => switchLanguage('de')} />
      <Button title="Switch to English" onPress={() => switchLanguage('en')} />
    </View>
  );
};

export default ExampleComponent;
Key Notes
  • If your translation files have more complex Angular tags (like pluralization), you can extend the transformTranslationData function to handle those cases (e.g., map Angular plural rules to i18next's pluralization syntax)
  • This approach keeps your original translation files intact—no edits needed, perfect for reusing across Angular and React Native

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 16:17:48