Skip to content

Getting started

View .md

Three steps: create the setup, write texts next to their translations, read them where you need them.

Terminal window
npm add @falcondev-oss/lang-core @falcondev-oss/lang-vue

Call createLang() once per app and export what you need. base is the language your source texts are written in, languages lists every language you support.

lang/setup.ts
import { createLang } from '@falcondev-oss/lang-core'
export const lang = createLang({ base: 'en', languages: ['en', 'de'] })
export const { t, group } = lang
export type Language = (typeof lang.languages)[number] // 'en' | 'de'

A text is the English source text plus every other language. The English text is the key.

lang/translations.ts
import { group, t } from './setup'
export const translations = {
common: group(t('Save', { de: 'Speichern' }), t('Cancel', { de: 'Abbrechen' })),
login: {
title: t('Sign in', { de: 'Anmelden' }),
errors: group(t('Invalid email address', { de: 'Ungültige E-Mail-Adresse' })),
},
}

Leave out de and TypeScript reports the missing translation at that line. Hover a text in your editor and its type lists every translation.

lang/index.ts
import { createUseLang } from '@falcondev-oss/lang-vue'
import { ref } from 'vue'
import { lang, type Language } from './setup'
import { translations } from './translations'
export const locale = ref<Language>('en')
export const useLang = createUseLang(lang, locale, translations)
LoginForm.vue
<script setup lang="ts">
import { useLang } from '~/lang'
const text = useLang((all) => all.login)
</script>
<template>
<h1>{{ text.title }}</h1>
</template>

useLang() returns a ComputedRef that updates when locale changes. See Vue.

  • Texts: custom keys and what the types show
  • Parameters: numbers, names, plurals, Intl
  • Zod: translated validation messages