-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add i18n support in plugin infra (#3661)
* feat: add i18n in plugin infra * feat: enable i18n keys in plugin infra
- Loading branch information
1 parent
4176711
commit a18a66b
Showing
14 changed files
with
128 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { I18NFieldOrReactNode, I18NStringField } from '@masknet/plugin-infra' | ||
import { useTranslation } from 'react-i18next' | ||
|
||
export interface PluginI18NFieldRenderProps { | ||
field: I18NFieldOrReactNode | ||
pluginID: string | ||
} | ||
export function PluginI18NFieldRender({ pluginID, field }: PluginI18NFieldRenderProps) { | ||
const [t] = useTranslation() | ||
if (!field) return null | ||
if (typeof field === 'object' && 'fallback' in field) { | ||
return ( | ||
<> | ||
{field.i18nKey | ||
? t(field.i18nKey, { ns: pluginID, nsSeparator: '%%%', defaultValue: field.fallback }) | ||
: field.fallback} | ||
</> | ||
) | ||
} | ||
return <>{field}</> | ||
} | ||
export function usePluginI18NField() { | ||
const [t] = useTranslation() | ||
return function (pluginID: string, field: I18NStringField) { | ||
if (!field.i18nKey) return field.fallback | ||
if (!field.i18nKey.startsWith('__')) { | ||
/** | ||
* This field is used in the definition of a plugin in form of | ||
* { fallback: "Text", i18nKey: "name" } | ||
* | ||
* Which is highly not likely to be analyzed by the type system. | ||
* Enforce those key to starts with __, we can exclude those keys | ||
* from the unused key result to keep the functionality of the analyzer. | ||
*/ | ||
console.warn( | ||
`[@masknet/plugin-infra] Plugin ${pluginID} uses i18n key ${field.i18nKey}. Please change it to __${field.i18nKey}.`, | ||
) | ||
return field.fallback | ||
} | ||
return t(field.i18nKey, { ns: pluginID, nsSeparator: '%%%', defaultValue: field.fallback }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "Example Plugin", | ||
"__entry__": "🤔 Example" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "測試插件", | ||
"__entry__": "🤔 測試" | ||
} |
Oops, something went wrong.