Skip to content

Commit

Permalink
Make module configs translateable
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Oct 28, 2023
1 parent 83a1826 commit 0d0dd16
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
9 changes: 8 additions & 1 deletion Website/src/components/ConfigureView/libs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Markdown, PromoBanner } from "./components";
import { useActivity } from "@Hooks/useActivity";
import { Toolbar } from "@Components/onsenui/Toolbar";
import { SuFile } from "@Native/SuFile";
import { os } from "@Native/Os";
import { StringsProvider, useStrings } from "@Hooks/useStrings";

export const libraries = [
{
Expand Down Expand Up @@ -52,6 +52,13 @@ export const libraries = [
useNativeProperties: useNativeProperties,
useNativeStorage: useNativeStorage,
useTheme: useTheme,
useStrings: useStrings,
},
},
{
name: "@mmrl/providers",
__esModule: {
StringsProvider: StringsProvider,
},
},

Expand Down
14 changes: 9 additions & 5 deletions Website/src/hooks/useStrings.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from "react";
import { useLocalStorage } from "usehooks-ts";
import { SetValue, useNativeStorage } from "./useNativeStorage";
import { StringDeclaration, strs, useLanguageMap } from "./../locales/declaration";
import { StringDeclaration, Strs, useLanguageMap } from "./../locales/declaration";
import { useSettings } from "./useSettings";

interface StrContext {
Expand All @@ -25,14 +25,18 @@ export interface AvailableLangs {
value: str;
}

interface StringsProviderProps extends React.PropsWithChildren {
data: Strs;
}

/**
*
* @param strings The first element is the default language
*/
export const StringsProvider = (props: React.PropsWithChildren) => {
export const StringsProvider = (props: StringsProviderProps) => {
const { settings } = useSettings();

const defaultLanguage = Object.keys(strs)[0];
const defaultLanguage = Object.keys(props.data)[0];
const currentLanguage = React.useMemo(() => settings.language.value, [settings]);

const format = React.useCallback((template: string, object?: object) => {
Expand All @@ -52,8 +56,8 @@ export const StringsProvider = (props: React.PropsWithChildren) => {

const strings = React.useCallback(
(key: string, fmt?: object) => {
const currentLang = strs[currentLanguage];
const defaultLang = strs[defaultLanguage];
const currentLang = props.data[currentLanguage];
const defaultLang = props.data[defaultLanguage];

if (currentLang[key] !== undefined) {
return format(currentLang[key], fmt);
Expand Down
3 changes: 2 additions & 1 deletion Website/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { MMRLAnchor } from "./custom-elements/anchor";

import "onsenui/css/onsenui.css";
import "@Styles/default.scss";
import { strs } from "./locales/declaration";

ons.platform.select("android");

Expand All @@ -32,7 +33,7 @@ ons.ready(() => {
<React.StrictMode>
<ModConfProvider>
<SettingsProvider>
<StringsProvider>
<StringsProvider data={strs}>
<ThemeProvider>
<Preventer prevent="contextmenu">
<RepoProvider>
Expand Down
2 changes: 1 addition & 1 deletion Website/src/locales/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React from "react";
*/
export type StringDeclaration = keyof typeof en;

type Strs = {
export type Strs = {
[code: str]: Partial<Record<StringDeclaration, str>>;
};

Expand Down

0 comments on commit 0d0dd16

Please sign in to comment.