Skip to content

Commit

Permalink
fix: use preferred language for wiki
Browse files Browse the repository at this point in the history
  • Loading branch information
linonetwo committed Feb 10, 2024
1 parent ada6f68 commit c053af7
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion assets/plugins/syncadaptor.html

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ function getInfoTiddlerFields(updateInfoTiddlersCallback: (infos: Array<{ text:
// Basics
if (!$tw.browser || typeof window === 'undefined') return infoTiddlerFields;
const isInTidGi = typeof document !== 'undefined';
const workspaceID = window.meta?.()?.workspaceID;
const { workspaceID, language } = window.meta?.() ?? {};
infoTiddlerFields.push({ title: '$:/info/tidgi', text: mapBoolean(isInTidGi) }, { title: '$:/info/tidgi-mobile', text: mapBoolean(isInTidGi) });
if (language !== undefined) {
infoTiddlerFields.push({ title: '$:/info/browser/language', text: language });
}
if (isInTidGi && workspaceID) {
infoTiddlerFields.push({ title: '$:/info/tidgi/workspaceID', text: workspaceID });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@ const setup = () => {
});
};

// eslint-disable-next-line no-var
declare var exports: {
startup: typeof setup;
};
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
exports.startup = setup;
24 changes: 24 additions & 0 deletions plugins/src/expo-file-system-syncadaptor/setLanguage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const setLanguage = () => {
const { language } = window.meta?.() ?? {};
let twLanguage = 'en-GB';
switch (language) {
case 'en': {
twLanguage = 'en-GB';
break;
}
case 'zh': {
twLanguage = 'zh-CN';
break;
}
default: {
break;
}
}
$tw.wiki.addTiddler({
title: '$:/language',
text: twLanguage,
});
};

/* eslint-disable @typescript-eslint/no-unsafe-member-access */
exports.startup = setLanguage;
3 changes: 3 additions & 0 deletions plugins/src/expo-file-system-syncadaptor/setLanguage.ts.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module-type: startup
title: $:/plugins/linonetwo/expo-file-system-syncadaptor/setLanguage.js
type: application/javascript
7 changes: 6 additions & 1 deletion src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const supportedLanguages = [
];
export const detectedLanguage = getLocales()[0].languageCode;
void i18n.use(initReactI18next).init({
lng: useConfigStore.getState().preferredLanguage ?? detectedLanguage ?? defaultLanguage,
fallbackLng: 'en',
resources: {
en: {
Expand All @@ -30,6 +29,12 @@ void i18n.use(initReactI18next).init({
},
},
compatibilityJSON: 'v3',
// getState() only have value when it is in dev mode hot reload, will return default value on production, use `subscribe` as below to fix this.
lng: useConfigStore.getState().preferredLanguage ?? detectedLanguage ?? defaultLanguage,
});
// incase store is not loaded from asyncStorage at this time, we need to subscribe to the store to get the latest value
useConfigStore.subscribe((state) => {
void i18n.changeLanguage(state.preferredLanguage ?? detectedLanguage ?? defaultLanguage);
});

export { default } from 'i18next';
1 change: 0 additions & 1 deletion src/pages/Config/Language.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export function Language(): JSX.Element {
// when tap again, set to undefined
const preferredLanguage = currentLanguage === newValue ? undefined : newValue;
setConfig({ preferredLanguage });
await i18n.changeLanguage(preferredLanguage ?? detectedLanguage ?? defaultLanguage);
}}
buttons={supportedLanguages}
/>
Expand Down
6 changes: 4 additions & 2 deletions src/pages/WikiWebView/WikiViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const WikiViewer = ({ wikiWorkspace, webviewSideReceiver, quickLoad }: Wi
useRequestNativePermissions();

const [loaded, setLoaded] = useState(false);
const [rememberLastVisitState] = useConfigStore(state => [state.rememberLastVisitState]);
const [rememberLastVisitState, preferredLanguage] = useConfigStore(state => [state.rememberLastVisitState, state.preferredLanguage]);
/**
* Register service JSB to be `window.service.xxxService`, for plugin in webView to call.
*/
Expand Down Expand Up @@ -146,7 +146,9 @@ export const WikiViewer = ({ wikiWorkspace, webviewSideReceiver, quickLoad }: Wi
userAgent={FAKE_USER_AGENT}
// add DOCTYPE at load time to prevent Quirks Mode
source={{
html: `<!doctype html><html lang="en"><head><meta charset="UTF-8" /></head><body><div id="tidgi-mobile-webview-before-loaded-place-holder"/></body></html>`,
html: `<!doctype html><html lang="${
preferredLanguage ?? 'en'
}"><head><meta charset="UTF-8" /></head><body><div id="tidgi-mobile-webview-before-loaded-place-holder"/></body></html>`,
/**
* Add baseUrl to fix `SecurityError: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.`
* @url https://github.com/react-native-webview/react-native-webview/issues/1635#issuecomment-1021425071
Expand Down
3 changes: 3 additions & 0 deletions src/pages/WikiWebView/getWindowMeta.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import i18n from 'i18next';
import { IWikiWorkspace } from '../../store/workspace';

export interface WindowMeta {
language?: string;
workspaceID: string;
}

Expand All @@ -11,6 +13,7 @@ export function getWindowMeta(workspace: IWikiWorkspace) {
window.meta = () => (${
JSON.stringify({
workspaceID: workspace.id,
language: i18n.language,
})
});
`;
Expand Down

0 comments on commit c053af7

Please sign in to comment.