0.1.11 • Published 8 months ago

@barfittc/translations v0.1.11

Weekly downloads
-
License
Apache 2.0
Repository
-
Last release
8 months ago

strongly typed translation api for vue

setup: create a new directory in your src called translations

inside the directory, create an index.ts

inside the index.ts file

import { useTranslations as useTranslationsBase } from "@barfittc/translations"

// your translations, to be mapped
import en from "./en"
import fr from "./fr"

// your translations defininions type
export type Translations = {
    "hello":string,
    "hello2":string,
    "messages": {
      "hello": string
    }
}

// the translation May, this gets passed to the addTranslations
export const TranslationMap = {
    "en": en,
    "fr": fr
}
// Type of list of languages
export type Language = keyof typeof TranslationMap;

// shortcut helper
export const useTranslations = useTranslationsBase<Translations, typeof TranslationMap>;

create a new ts for every language you wish to support, {0} are chunks that will get replaged by args provided by translate, 0 index from first arg

import type { Translations } from ".";
const translations: Translations = {
    "hello":"hello world",
    "hello2":"Hello {0}",
    "messages": {
      "hello": "Hello Solar System"
    }
};
export default translations;

Add to your main.ts vue definition

createApp(App)
    .use(addTranslations("en"/* default lang, key of the map defined */, TranslationMap /* the map defined with all keys being each language */))
	.mount('#app')

in your <script setup lang="ts"

const t = useTranlations();
console.log(t.translate(t.map.messages.hello))
t.changeLanguage("fr");

use in your

<div>$t($translations.hello)</div>
<div>$t($translations.hello2, "galaxy")</div>
0.1.11

8 months ago

0.1.10

8 months ago

0.1.9

8 months ago

0.1.8

9 months ago

0.1.7

9 months ago

0.1.6

9 months ago

0.1.5

9 months ago

0.1.4

9 months ago

0.1.3

9 months ago

0.1.2

9 months ago

0.1.1

9 months ago

0.1.0

9 months ago