Skip to content

Latest commit

 

History

History
94 lines (80 loc) · 2.18 KB

README.md

File metadata and controls

94 lines (80 loc) · 2.18 KB

@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 🙏