Skip to content

Typescript decorator which helps in bringing all translations in one place with its key, and more ...

License

Notifications You must be signed in to change notification settings

mustafah/translations

Repository files navigation

@Translations Typescript Decorator

Typescripts decorator which helps in:

  • All translations in one place with its key
  • Go To Definition support
  • Parameterized translations
  • Missing translation generation for quick prototyping

Install

Install dependencies with npm:

npm i mustafah/translations

Import

import Translations from 'translations';

Define your languages

enum Languages {
  Arabic,
  English,
  German,
  French
}
import { configTranslations } from 'translations';
configTranslations({ languagesEnum: Languages });

❤️ Add translations to your class

@Component({ ... })
export class Component {
  @Translations()
  t = {
    HelloWorld: [
      'مرحباً بالعالم',
      'Hello World',
      'Hallo Welt',
      'Bonjour le Monde'
    ]
  };
}

💬 Set current language

setTranslationLanguage(Languages.English);

😊 Parameterized translations

  @Translations()
  t: any = {
    WelcomeTo: (country) => [
      `أهلاً بك في ${country}`,
      `Welcome to ${country}`,
      `Willkommen in ${country}`,
      `Bienvenue en ${country}`
    ],
    // Countries
    Egypt: ['مصر', 'Egypt', 'Ägypten', 'Égypte'],
    Germany: ['ألمانيا', 'Germany', 'Deutschland', 'Allemagne']
  };

  setTranslationLanguage(Languages.English);
  console.log(this.t.WelcomeTo(this.t.Egypt));
  // Welcome to Egypt

  setTranslationLanguage(Languages.German);
  console.log(this.t.WelcomeTo(this.t.Germany));
  // Willkommen in Deutschland

🚀 Missing translations generation


Thank you 🙏

About

Typescript decorator which helps in bringing all translations in one place with its key, and more ...

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published