-
Notifications
You must be signed in to change notification settings - Fork 142
docs: add Language section #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
09a72cd
0e6ac33
36fa245
5a88595
58c656b
8eda954
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -376,3 +376,61 @@ async function AsyncTest(): Promise<string | null> { | |
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Language | ||
|
|
||
| ### English-first | ||
|
|
||
| Write the following in English by default: | ||
|
|
||
| - `manifest.json` | ||
| - `id` | ||
| - `name` | ||
| - `description` | ||
| - `README.md` | ||
|
|
||
| It is not recommended but if you really need it, you may include translations (except for `id`), but the English version must appear first. | ||
|
|
||
| ### UI localization | ||
|
|
||
| Set English as the default language for your plugin UI whenever possible. | ||
|
|
||
| Do not mix multiple languages within the same UI. | ||
|
|
||
| If your plugin offers localized UI strings, ensure it respects [`getLanguage()`](https://docs.obsidian.md/Reference/TypeScript+API/getLanguage) (requires `minAppVersion` to be at least `1.8.7`). | ||
|
|
||
| Don't add a custom language setting to your plugin; rely on Obsidian's built-in language setting instead. | ||
mnaoumov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| If your plugin ships without English support, call this out explicitly in both the `description` property in `manifest.json` and the `README.md`. | ||
|
|
||
| ### Internationalization (i18n) frameworks | ||
|
|
||
| If your plugin supports multiple UI languages, consider using frameworks like [i18next](https://www.i18next.com/), or hand-written one, e.g.: | ||
|
|
||
| ```ts | ||
| // locales/en.ts | ||
| export const en = { | ||
| 'some.key': 'Some value' | ||
| }; | ||
|
|
||
| // i18n.ts | ||
| import en from './locales/en.ts'; | ||
| import zhCN from './locales/zh_CN.ts' | ||
| import { getLanguage } from 'obsidian'; | ||
|
|
||
| const localeMap: { [locale: string]: Partial<typeof en> } = { | ||
| en: en | ||
| zh: zhCN | ||
| }; | ||
|
|
||
| const userLocale = localeMap[getLanguage()]; | ||
|
|
||
| export function t(key: keyof typeof en): string { | ||
| return userLocale?.[key] ?? en[key] ?? key; | ||
| } | ||
|
|
||
| // main.ts | ||
| import { t } from './i18n.ts'; | ||
|
|
||
| new Notice(t('some.key')); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one thing we need to check for this, who actually wrote this code first and what's the license
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is too ubiquitous to fall under any license, doing a quick code search reveals hundreds of other repositories, all using a similar implementation - it is reasonable to assume that some were independently written: https://sourcegraph.com/search?q=context:global+%22export+function+t%28%22&patternType=keyword&sm=0 |
||
| ``` | ||
Uh oh!
There was an error while loading. Please reload this page.