From 48877cbf0f99a0a51cdeff9a2ccb53b70340bf20 Mon Sep 17 00:00:00 2001 From: vinay kumar Date: Sat, 28 Jun 2025 09:57:23 +0530 Subject: [PATCH] Add i18next integration --- app/(tabs)/_layout.tsx | 20 +++++++--------- app/_layout.tsx | 34 ++++++++++++++++----------- services/i18n/index.ts | 40 ++++++++++++++++++++++++++++++++ services/i18n/languagesList.json | 8 +++++++ services/i18n/locals/en.json | 4 ++++ services/i18n/locals/hi.json | 4 ++++ 6 files changed, 86 insertions(+), 24 deletions(-) create mode 100644 services/i18n/index.ts create mode 100644 services/i18n/languagesList.json create mode 100644 services/i18n/locals/en.json create mode 100644 services/i18n/locals/hi.json diff --git a/app/(tabs)/_layout.tsx b/app/(tabs)/_layout.tsx index 30914fb..473d793 100644 --- a/app/(tabs)/_layout.tsx +++ b/app/(tabs)/_layout.tsx @@ -1,11 +1,11 @@ -import React from 'react'; -import FontAwesome from '@expo/vector-icons/FontAwesome'; -import { Link, Tabs } from 'expo-router'; -import { Pressable } from 'react-native'; - -import Colors from '@/constants/Colors'; -import { useColorScheme } from '@/components/useColorScheme'; -import { useClientOnlyValue } from '@/components/useClientOnlyValue'; +import React from "react"; +import FontAwesome from "@expo/vector-icons/FontAwesome"; +import { Link, Tabs } from "expo-router"; +import { Pressable } from "react-native"; +import Colors from "@/constants/Colors"; +import { useColorScheme } from "@/components/useColorScheme"; +import { useClientOnlyValue } from "@/components/useClientOnlyValue"; +import { TAB_CONFIG } from "@/constants/config"; // You can explore the built-in icon families and icons on the web at https://icons.expo.fyi/ function TabBarIcon(props: { @@ -21,9 +21,7 @@ export default function TabLayout() { return ( + + - + + ); } diff --git a/services/i18n/index.ts b/services/i18n/index.ts new file mode 100644 index 0000000..1422ac2 --- /dev/null +++ b/services/i18n/index.ts @@ -0,0 +1,40 @@ +import i18next from "i18next"; +import { initReactI18next } from "react-i18next"; +import en from "../i18n/locals/en.json"; +import hi from "../i18n/locals/hi.json"; +import AsyncStorage from "@react-native-async-storage/async-storage"; +import { STORAGE_KEYS } from "../../constants/config"; + +export const languageResources = { + en: { translation: en }, + hi: { translation: hi }, +}; + +i18next.use(initReactI18next).init({ + compatibilityJSON: "v4", + lng: "hi", + fallbackLng: "en", + resources: languageResources, + debug: true, +}); + +i18next.on("initialized", () => { + console.log("✅ i18next initialized with language:", i18next.language); + console.log("Translation test (welcome):", i18next.t("welcome")); +}); + +export const setLanguage = async (language: string) => { + await AsyncStorage.setItem(STORAGE_KEYS.LANGUAGE, language); + i18next.changeLanguage(language); +}; + +export const getLanguage = async () => { + const lang = await AsyncStorage.getItem(STORAGE_KEYS.LANGUAGE); + if (lang) { + i18next.changeLanguage(lang); + } + + return lang; +}; + +export default i18next; diff --git a/services/i18n/languagesList.json b/services/i18n/languagesList.json new file mode 100644 index 0000000..5642f19 --- /dev/null +++ b/services/i18n/languagesList.json @@ -0,0 +1,8 @@ +{ + "en": { + "name": "English" + }, + "hi": { + "name": "हिंदी" + } +} diff --git a/services/i18n/locals/en.json b/services/i18n/locals/en.json new file mode 100644 index 0000000..26659a1 --- /dev/null +++ b/services/i18n/locals/en.json @@ -0,0 +1,4 @@ +{ + "change-language": "Change language", + "welcome": "Welcome to Driver Saathi" +} diff --git a/services/i18n/locals/hi.json b/services/i18n/locals/hi.json new file mode 100644 index 0000000..bc2c8e0 --- /dev/null +++ b/services/i18n/locals/hi.json @@ -0,0 +1,4 @@ +{ + "change-language": "भाषा बदलें", + "welcome": "ड्राइवर साथी में आपका स्वागत है" +}