syntaxseed/translator

Translate strings in your application to multiple languages loaded from JSON files.

v1.0.0 2022-12-15 18:16 UTC

This package is auto-updated.

Last update: 2024-11-15 23:05:21 UTC


README

GitHub tag (latest SemVer)   PHP v8.0+   PHP v8.1+   License: MIT
follow on Twitter  Sponsor Project

Load JSON language files and translate them in your application.

Features

  • Easy to use, just set language and point to a JSON file.
  • Uses the primary language as the lookup key, so strings are understandable in your application.
  • Add any number of languages with a language key.
  • All the translations for one string are in the same file/place.
  • Fails silently - untranslated strings will just output the lookup string.
  • Load more than one language file for different parts of your application.
  • Short translation function name (get) for easy typing.
  • Switch target language when needed.

Install

Via Composer:

composer require syntaxseed/translator

Or add to composer.json:

"require": {
    "syntaxseed/translator": "^1.0"
},

Usage

For a more in-depth example, see examples/ directory.

use Syntaxseed\Translator\Translator;

require('../src/Translator.php');

// Initialize with base (lookup) language and target language to translate to.
$translations = new Translator('en', 'fr');
// Load a JSON file to the default lookup key.
$translations->loadLanguageFile(__DIR__.'/lang.json');

echo($translations->get('Hello World'));

Language File JSON Format

{
    "Email address" : {
        "fr" : "Addresse courriel",
        "es" : "Dirección de correo electrónico"
    },
    "Last name" : {
        "fr" : "Nom de famille",
        "es" : "Apellido"
    },
    "Your name" : {
        "fr" : "Votre nom",
        "es" : "Su nombre"
    }
}

Changelog

  • v1.0.0 - (2018-10-27) Created. Added to GitHub.