Skip to content

Commit

Permalink
Fix crash on Qihu-360X Browser
Browse files Browse the repository at this point in the history
  • Loading branch information
sheepzh committed Oct 17, 2023
1 parent d94a1dc commit daf7741
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/api/chrome/i18n.ts
Original file line number Diff line number Diff line change
@@ -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)
}
export const getMessage: (key: string) => string = chrome?.i18n?.getMessage
12 changes: 10 additions & 2 deletions src/i18n/chrome/t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@
*/

import { getMessage } from "@api/chrome/i18n"
import { router, ChromeMessage } from "./message"
import messages, { router, ChromeMessage } from "./message"
import { IS_CHROME } from "@util/constant/environment"
import { t } from ".."

export const keyPathOf = (key: (root: ChromeMessage) => string) => key(router)

export const t2Chrome = (key: (root: ChromeMessage) => string) => getMessage(keyPathOf(key))
export const t2Chrome = (key: (root: ChromeMessage) => string) => {
if (getMessage) {
return getMessage(keyPathOf(key))
}
console.error(IS_CHROME)
return t<ChromeMessage>(messages, { key }, 'en')
}

0 comments on commit daf7741

Please sign in to comment.