This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for translations using VIntl
Adds support for translations using VIntl module as a peer dependency. Peer dependencies are dependencies that have to be provided by the consumer, which means that `@vintl/vintl` has to be installed and set up in Theseus and any other consumers. Translations for Omorphia are done in Omorphia to prevent duplicate work, and the consumer has to import the locale files like they do for with the styles already. Those locale files have to be registered in VIntl plugin and processed using `@vintl/unplugin`. `@vintl/unplugin` is a Unplugin (which means it works with Rollup, Vite, and even Webpack) that takes in locale files generated by `@formatjs/cli` utility and transforms them into the proper ES modules. This allows consumers to get rid of the `intl-messageformat-parser`, which has a huge toll on the bundle size otherwise, at the cost of inability to parse custom messages. To give an example of how this can be approached, docs have been updated to support changing the language used in examples. Knossos is already doing all this automatically by using `@vintl/nuxt`. `@formatjs/cli` allows to define messages directly and the code and then automatically extract them into the locale file. Collocation makes it easier to maintain the messages and see them right in the code. Translations are done using Crowdin, so Crowdin needs to be connected to this repository, just like it was connected to Knossos [^1]. This commit also introduces a config that allows to do just that. [^1]: modrinth/knossos#738 (comment)
- Loading branch information
Showing
11 changed files
with
688 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
project_id: 518556 | ||
preserve_hierarchy: true | ||
commit_message: '[ci skip]' | ||
|
||
files: | ||
- source: /locales/en-US/* | ||
dest: /%original_file_name% | ||
translation: /locales/%locale%/%original_file_name% |
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,15 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
declare module '@modrinth/omorphia-dev/locales/index.js' { | ||
interface LocaleExport { | ||
messages: Record<string, any[]> | ||
} | ||
|
||
interface LocaleDefinition { | ||
importFunction(): Promise<LocaleExport> | ||
} | ||
|
||
const localeDefinitions: Partial<Record<string, LocaleDefinition>> | ||
|
||
export { localeDefinitions } | ||
} |
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,73 @@ | ||
<script setup lang="ts"> | ||
import { useVIntl } from '@vintl/vintl' | ||
import { DropdownSelect } from 'omorphia' | ||
import { computed, ref } from 'vue' | ||
const { $locales, $config, changeLocale } = useVIntl() | ||
const getLocaleDisplayName = (() => { | ||
const cache = new Map<string, Intl.DisplayNames>() | ||
return function getLocaleDisplayName(locale: string) { | ||
let displayNames = cache.get(locale) | ||
if (displayNames == null) { | ||
displayNames = new Intl.DisplayNames(locale, { | ||
type: 'language', | ||
languageDisplay: 'standard', | ||
}) | ||
cache.set(locale, displayNames) | ||
} | ||
return displayNames.of(locale) | ||
} | ||
})() | ||
const isChanging = ref(false) | ||
const currentLocale = computed({ | ||
get() { | ||
return $config.locale | ||
}, | ||
async set(value) { | ||
if (isChanging.value) return | ||
try { | ||
isChanging.value = true | ||
await changeLocale(value) | ||
} finally { | ||
isChanging.value = false | ||
} | ||
}, | ||
}) | ||
</script> | ||
<template> | ||
<div class="LanguageSwitcher"> | ||
<h2 class="title">Playground language</h2> | ||
|
||
<DropdownSelect | ||
class="locale-dropdown" | ||
name="locale" | ||
v-model="currentLocale" | ||
placeholder="Change language" | ||
:disabled="isChanging" | ||
:options="Array.from($locales).map(([{ tag }]) => tag)" | ||
:display-name="(locale: string) => getLocaleDisplayName(locale)" | ||
/> | ||
</div> | ||
</template> | ||
<style scoped> | ||
.LanguageSwitcher { | ||
padding-block: 18px; | ||
border-bottom: 1px solid var(--vp-c-divider); | ||
} | ||
.LanguageSwitcher .title { | ||
font-weight: 700; | ||
font-size: 14px; | ||
color: var(--vp-c-text-1); | ||
} | ||
.LanguageSwitcher .locale-dropdown { | ||
width: 200px; | ||
font-size: 14px; | ||
} | ||
</style> |
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,13 +1,49 @@ | ||
import DefaultTheme from 'vitepress/theme' | ||
import { localeDefinitions } from '@modrinth/omorphia-dev/locales/index.js' | ||
import { createPlugin } from '@vintl/vintl/plugin' | ||
import Omorphia from 'omorphia' | ||
import DefaultTheme from 'vitepress/theme' | ||
import { createVNode } from 'vue' | ||
import DemoContainer from './DemoContainer.vue' | ||
import LanguageSwitcher from './LanguageSwitcher.vue' | ||
|
||
import './compat.scss' | ||
|
||
/** @type {import('vitepress').Theme} */ | ||
export default { | ||
...DefaultTheme, | ||
enhanceApp(ctx) { | ||
ctx.app.use(Omorphia) | ||
ctx.app.component('DemoContainer', DemoContainer) | ||
ctx.app.use( | ||
createPlugin({ | ||
controllerOpts: { | ||
locales: Object.keys(localeDefinitions).map((tag) => ({ tag })), | ||
listen: { | ||
async localeload(event) { | ||
const locale = event.locale.tag | ||
if (!Object.hasOwn(localeDefinitions, locale)) { | ||
throw new Error(`Unknown locale: ${locale}`) | ||
} | ||
|
||
try { | ||
const { messages } = await localeDefinitions[locale].importFunction() | ||
event.addMessages(messages) | ||
} catch (err) { | ||
console.error(`Failed to load locale: ${locale}`, err) | ||
} | ||
}, | ||
}, | ||
defaultMessageOrder: ['locale', 'descriptor'], | ||
}, | ||
globalMixin: false, | ||
}) | ||
) | ||
}, | ||
Layout() { | ||
return createVNode(DefaultTheme.Layout, null, { | ||
'sidebar-nav-before'() { | ||
return createVNode(LanguageSwitcher) | ||
}, | ||
}) | ||
}, | ||
} |
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,5 @@ | ||
{ | ||
"omorphia.component.copy.action.copy": { | ||
"defaultMessage": "Copy code to clipboard" | ||
} | ||
} |
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.