diff --git a/src/api/chrome/i18n.ts b/src/api/chrome/i18n.ts index c7aeb135..4c9e480b 100644 --- a/src/api/chrome/i18n.ts +++ b/src/api/chrome/i18n.ts @@ -1,10 +1,10 @@ +// Bug of chrome: +// chrome.i18n.getUILanguage may not work in background export function getUILanguage(): string { - return chrome.i18n.getUILanguage() + return chrome?.i18n?.getUILanguage?.() } -// Bug of chrome: +// Bug of chrome: // chrome.i18n.getMessage may not work in background // @see https://stackoverflow.com/questions/6089707/calling-chrome-i18n-getmessage-from-a-content-script -export function getMessage(messageName: string): string { - return chrome.i18n.getMessage(messageName) -} \ No newline at end of file +export const getMessage: (key: string) => string = chrome?.i18n?.getMessage diff --git a/src/i18n/chrome/t.ts b/src/i18n/chrome/t.ts index f39063ad..fcb12362 100644 --- a/src/i18n/chrome/t.ts +++ b/src/i18n/chrome/t.ts @@ -1,13 +1,21 @@ /** * Copyright (c) 2021 Hengyang Zhang - * + * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ -import { getMessage } from "@api/chrome/i18n" -import { router, ChromeMessage } from "./message" +import { getMessage } from '@api/chrome/i18n' +import messages, { router, ChromeMessage } from './message' +import { t } from '..' +import { IS_CHROME } from '@util/constant/environment' export const keyPathOf = (key: (root: ChromeMessage) => string) => key(router) -export const t2Chrome = (key: (root: ChromeMessage) => string) => getMessage(keyPathOf(key)) \ No newline at end of file +export const t2Chrome = (key: (root: ChromeMessage) => string) => { + if (getMessage) { + return getMessage(keyPathOf(key)) + } + console.error(IS_CHROME) + return t(messages, { key }, 'en') +}