Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language fallback #943

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ import { Dimensions, StyleSheet } from "react-native";
import { useTranslation } from "react-i18next";
import FastImage from "@gkasdorf/react-native-fast-image";
import { useMe } from "@src/stores/site/siteStore";
import { View } from "./src/components/common/Gluestack";
import {
useSettingsStore,
useThemeOptions,
} from "./src/stores/settings/settingsStore";
} from "@src/stores/settings/settingsStore";
import { selectSite } from "@src/slices/site/siteSlice";
import { truncateName } from "@src/helpers/TextHelper";
import { ICON_MAP } from "@src/constants/IconMap";
import { CustomTabBar } from "@src/components/common/Navigation/CustomTabBar";
import {
useAccountStore,
useAccounts,
useCurrentAccount,
} from "@src/stores/account/accountStore";
import { View } from "./src/components/common/Gluestack";

import { selectSite } from "./src/slices/site/siteSlice";
import { truncateName } from "./src/helpers/TextHelper";
import { ICON_MAP } from "./src/constants/IconMap";
import KeywordsFilterScreen from "./src/components/screens/Settings/Filters/KeywordsFilterScreen";
import InstanceFiltersScreen from "./src/components/screens/Settings/Filters/InstanceFiltersScreen";
import { CustomTabBar } from "./src/components/common/Navigation/CustomTabBar";
import LoadingView from "./src/components/common/Loading/LoadingView";
import SFIcon from "./src/components/common/icons/SFIcon";
import EditCommentScreen from "./src/components/screens/Comments/EditCommentScreen";
Expand Down Expand Up @@ -63,11 +68,6 @@ import UserCommentsScreen from "./src/components/screens/UserProfile/UserComment
import UserPostsScreen from "./src/components/screens/UserProfile/UserPostsScreen";
import UserProfileScreen from "./src/components/screens/UserProfile/UserProfileScreen";
import ViewerScreen from "./src/components/screens/ViewerScreen";
import {
useAccountStore,
useAccounts,
useCurrentAccount,
} from "./src/stores/account/accountStore";
import { useAppSelector } from "./store";
import FiltersScreen from "./src/components/screens/Settings/Filters/FiltersScreen";

Expand Down Expand Up @@ -293,7 +293,7 @@ function InboxStackScreen() {
);
}

function SettingsScreens(stack) {
function SettingsScreens(stack: typeof SettingsStack) {
const { t } = useTranslation();
return (
<>
Expand Down Expand Up @@ -676,8 +676,8 @@ function Tabs() {
<SFIcon icon={ICON_MAP.USER_AVATAR} color={color} />
),
tabBarLabel: hideUsernameInTab
? "Profile"
: truncateName(currentAccount?.username ?? "Profile", 8),
? t("Profile")
: truncateName(currentAccount?.username ?? t("Profile"), 8),
freezeOnBlur: false,
}}
/>
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/DayJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import relativeTime from "dayjs/plugin/relativeTime";
import updateLocale from "dayjs/plugin/updateLocale";
import utc from "dayjs/plugin/utc";
import localizedFormat from "dayjs/plugin/localizedFormat";
import de_DE from "./i18n/locales/de.json";
import en_US from "./i18n/locales/en.json";
import cs_CZ from "./i18n/locales/cz.json";
import ro_RO from "./i18n/locales/ro.json";
import pt_BR from "./i18n/locales/pt_br.json";
import de_DE from "./i18n/locales/de_DE.json";
import en_US from "./i18n/locales/en_US.json";
import cs_CZ from "./i18n/locales/cs_CZ.json";
import ro_RO from "./i18n/locales/ro_RO.json";
import pt_BR from "./i18n/locales/pt_BR.json";

// dayjs locales
import "dayjs/locale/de";
Expand Down
54 changes: 43 additions & 11 deletions src/plugins/i18n/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,66 @@
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import en_US from "./locales/en.json";
import de_DE from "./locales/de.json";
import pt_BR from "./locales/pt_br.json";
import cs_CZ from "./locales/cz.json";
import ro_RO from "./locales/ro.json";
import { writeToLog } from "@src/helpers/LogHelper";
import languageDetector from "./languageDetector";
import en_US from "./locales/en_US.json";
import de_DE from "./locales/de_DE.json";
import pt_BR from "./locales/pt_BR.json";
import cs_CZ from "./locales/cs_CZ.json";
import ro_RO from "./locales/ro_RO.json";

type SupportedLocale = "en" | "de" | "pt" | "ro" | "cs";

i18n
.use(languageDetector)
.use(initReactI18next)
.init({
resources: {
en: {
"en-US": {
translation: en_US,
},
de: {
"de-DE": {
translation: de_DE,
},
pt: {
"pt-BR": {
translation: pt_BR,
},
ro: {
"ro-RO": {
translation: ro_RO,
},
cz: {
"cs-CZ": {
translation: cs_CZ,
},
},
fallbackLng: "en",
fallbackLng: (code) => {
const fallbacks = ["en-US"];

if (!code) return fallbacks;

const [locale] = code.split("-") as [SupportedLocale, string];

switch (locale) {
case "en":
// already covered
break;
case "de":
fallbacks.push("de-DE");
break;
case "pt":
fallbacks.push("pt-BR");
break;
case "ro":
fallbacks.push("ro-RO");
break;
case "cs":
fallbacks.push("cs-CZ");
break;
default:
writeToLog(`Language "${code}" is not yet supported!`);
break;
}

return fallbacks.reverse();
},
interpolation: {
escapeValue: false, // react already safes from xss => https://www.i18next.com/translation-function/interpolation#unescape
},
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/i18n/languageDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LanguageDetectorModule } from "i18next";
// noinspection JSUnusedGlobalSymbols
const languageDetector: LanguageDetectorModule = {
type: "languageDetector",
detect: () => Localization.locale.split("-")[0],
detect: () => Localization.locale,
init: () => {},
cacheUserLanguage: () => {},
};
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"useReaderMode": "Lesemodus verwenden"
},
"reportBugBtn": "Bug auf GitHub melden",
"checkInstanceStatus": "Lemmy Status prüfen",
"swipeToVote": "Mit Wisch hoch-/runterwählen",
"swipe": {
"left": {
Expand Down
File renamed without changes.
File renamed without changes.
Loading