-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(i18n): dynamic locale and OALocaleSelect component (#137)
- Loading branch information
1 parent
8e24335
commit 2eb3794
Showing
13 changed files
with
223 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
--- | ||
aside: false | ||
outline: false | ||
--- | ||
|
||
# Internationalization (i18n) | ||
|
||
You can use the `useTheme` composable to configure internationalization in your `.vitepress/theme/index.js` file, or in any `.md` page/file. All values are optional. | ||
|
||
```vue | ||
<script setup lang="ts"> | ||
import { useTheme, locales } from 'vitepress-openapi' | ||
useTheme({ | ||
i18n: { | ||
locale: 'en', // en | es | pt-BR | string | ||
fallbackLocale: 'en', // en | es | pt-BR | string | ||
messages: { | ||
en: { | ||
...locales.en, | ||
'custom i18n key': 'Custom i18n value', | ||
}, | ||
es: { | ||
...locales.es, | ||
'custom i18n key': 'Valor personalizado i18n', | ||
}, | ||
'pt-BR': { | ||
...locales['pt-BR'], | ||
'custom i18n key': 'Valor personalizado i18n', | ||
}, | ||
availableLocales: [ | ||
{ code: 'en', label: 'English' }, | ||
{ code: 'es', label: 'Español' }, | ||
{ code: 'pt-BR', label: 'Português (Brasil)' }, | ||
], | ||
}, | ||
}) | ||
</script> | ||
<!-- OASpec component or OAOperation component --> | ||
``` | ||
|
||
If you're using the built-in VitePress i18n features, you can configure the locale as follows: | ||
|
||
```vue | ||
<script setup lang="ts"> | ||
import { useTheme } from 'vitepress-openapi' | ||
import { useData } from 'vitepress' | ||
const { lang } = useData() | ||
useTheme({ | ||
i18n: { | ||
locale: lang.value, | ||
}, | ||
}) | ||
</script> | ||
<!-- OASpec component or OAOperation component --> | ||
``` | ||
|
||
You can see a [Live Example](https://vitepress-openapi-i18n.vercel.app/) and the [Source Code](https://github.com/enzonotario/vitepress-openapi-i18n) for more details. | ||
|
||
## `OALocaleSelect` component | ||
|
||
You can use the `OALocaleSelect` component to render a select with the available locales. Keep in mind that this will change only `vitepress-openapi` components, not the entire VitePress site. For example, you can add it to Navbar in your `.vitepress/config.js`: | ||
|
||
```js | ||
export default defineConfigWithTheme({ | ||
themeConfig: { | ||
nav: [ | ||
{ | ||
component: 'OALocaleSelect', | ||
}, | ||
], | ||
}, | ||
}) | ||
``` |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "vitepress-openapi", | ||
"type": "module", | ||
"version": "0.0.3-alpha.52", | ||
"version": "0.0.3-alpha.53", | ||
"packageManager": "[email protected]", | ||
"homepage": "https://vitepress-openapi.vercel.app/", | ||
"repository": { | ||
|
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 @@ | ||
<script setup> | ||
import { computed } from 'vue' | ||
import { useTheme } from '../../composables/useTheme' | ||
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger } from '../ui/select' | ||
const themeConfig = useTheme() | ||
const i18nConfig = themeConfig.getI18nConfig() | ||
const defaultLocale = computed(() => i18nConfig.locale?.value ?? 'en') | ||
function onLocaleChange(locale) { | ||
themeConfig.setI18nConfig({ | ||
locale, | ||
}) | ||
} | ||
</script> | ||
<template> | ||
<div class="self-center"> | ||
<Select | ||
:default-value="defaultLocale" | ||
:model-value="i18nConfig.locale" | ||
@update:model-value="onLocaleChange" | ||
> | ||
<SelectTrigger> | ||
<span class="vpi-languages option-icon" /> | ||
</SelectTrigger> | ||
<SelectContent> | ||
<SelectGroup> | ||
<SelectItem | ||
v-for="locale in i18nConfig.availableLocales" | ||
:key="locale.code" | ||
:value="locale.code" | ||
> | ||
{{ locale.label }} | ||
</SelectItem> | ||
</SelectGroup> | ||
</SelectContent> | ||
</Select> | ||
</div> | ||
</template> |
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
Oops, something went wrong.