Skip to content

pixltd-dev/ts-mini-translate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

ts-mini-translate

A very simple TypeScript translation utility.

Overview

ts-mini-translate is a lightweight translation function set for TypeScript projects. It allows you to easily add multi-language support by managing translation strings in JSON files.

Installation

  1. Copy the translate folder
    Simply copy the entire translate folder into your project.

  2. Enable JSON importing
    Ensure your TypeScript configuration allows importing JSON files.
    In your tsconfig.json, add:

    {
      "compilerOptions": {
        "resolveJsonModule": true
      }
    }

    Alternatively, you can modify the code to fetch JSON files from your webroot or a server endpoint if you prefer not to import them directly.

Usage

  • Place your translation files (e.g., en.json, es.json) inside the translate/locales directory.
  • Import and use the functions from translate/translations.ts in your application.

API Reference

async setLanguage(lang: string): Promise<void>

Loads the translation file for the specified language and sets it as the current language.
Also saves the language preference in localStorage.

  • lang: Language code (e.g., "en", "es").
  • Throws: Error if the language file is not found.

t(key: string): string

Translates a given key using the currently loaded translations.

  • key: The translation key.
  • Returns: The translated string, or the key itself if not found.

getCurrentLanguage(): string

Returns the currently active language code.


getSavedLanguage(): string | null

Retrieves the language preference previously saved in localStorage, or null if none is set.


About

Very simple typscript translation utility

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors