A React Native library that brings the native iOS 18+ Translation Tasks to your React Native applications.
Note: If you want to display a native iOS sheet, you can use react-native-ios-translate-sheet which provides a native UI for translation settings.
- ๐ Seamless integration with iOS native translation capabilities
- ๐ Access to all languages supported by iOS translation
- ๐ฑ Native iOS UI and interactions
- ๐๏ธ Supports old & new arch on RN 0.76+
- โ๏ธ Powered by SwiftUI's translationTask API under the hood
This library is designed specifically for iOS 18.0 and above. It can be safely installed in your React Native project regardless of your target iOS version or if you're developing for Android. However, please note:
- On iOS versions below 18.0, the translation taks will throw an error
- On Android devices, the translation functionality is not available and will throw an error
yarn add react-native-ios-translate-tasksor
npm install react-native-ios-translate-tasksThen, install the native dependencies:
cd ios && pod install- First, wrap your app (or the part where you want to use the translation tasks) with the
IOSTranslateTasksProvider:
import { IOSTranslateTasksProvider } from 'react-native-ios-translate-tasks';
function App() {
return (
<IOSTranslateTasksProvider>
{/* Your app content */}
</IOSTranslateTasksProvider>
);
}- Then, use the
useIOSTranslateTaskshook in any component where you want to trigger translation tasks:
import { useIOSTranslateTasks } from 'react-native-ios-translate-tasks';
function MyComponent() {
const { startIOSTranslateTasks } = useIOSTranslateTasks();
const enText = "Hello world! This is a sample text to translate.";
const enText2 = "How are you?";
const handleTranslate = async () => {
try {
const { translatedTexts } = await startIOSTranslateTasks(
[enText, enText2],
{
sourceLanguage: "en_EN",
targetLanguage: "fr_FR",
},
);
console.log(translatedTexts); // ["Salut le monde ! Ceci est un exemple de texte ร traduire.", "Comment allez-vous ?"]
} catch (e) {
console.log(e);
}
};
return (
<Button
title="Translate"
onPress={handleTranslate}
/>
);
}The translatedTexts array in the response will maintain the same order as the input texts array. This means that if you pass ["Hello", "World"], the response will contain translations in the corresponding order (e.g., ["Bonjour", "Monde"] for French). This ordering guarantee makes it easy to map translations back to their original texts.
The useIOSTranslateTasks hook provides an isSupported boolean that you can use to check if the translation functionality is available on the current device:
const { isSupported } = useIOSTranslateTasks();Note: The translation tasks will only run on iOS devices running version 18.0 or later. On other platforms or iOS versions below 18.0, the startIOSTranslateTasks function will throw an error.
When using the translation feature for the first time with a specific language, iOS may display a sheet asking the user to download the required language pack. The user must complete this download for translations to work.
Several types of errors may occur during translation:
- If the user cancels the language download sheet, an error will be thrown
- Invalid language codes will result in an error. Make sure to use valid Unicode language identifiers (e.g., "en-US", "fr_FR", "es-419", "zh-Hant-TW")
- If either the source or target language is not supported, an error will be thrown
Please note that this feature does not work on iOS simulators. It requires a physical iOS device running version 18.0 or later to function properly.
We welcome contributions! Please see our Contributing Guide for more details.
This project is licensed under the MIT License - see the LICENSE file for details.
If you like this project, please consider supporting it by giving it a โญ๏ธ on GitHub!
