A class for quickly providing multiple translations in PHP.
This script was developed as a personal project by Michel Descoteaux and is free to use and distribute.
- PHP >= 8.0
- Composer - Install
-
Run
composer require mouseeatscat/phpquicktranslate
-
Add this code to the top of your php document:
<?php
use MouseEatsCat\QuickTranslate;
require_once __DIR__ . '/vendor/autoload.php';
// Get the current language
$language = !empty($_GET['language']) ? $_GET['language']: 'en';
// Instantiate PHP Quick Translate
$qt = new QuickTranslate($language);
You can optionally add a mulilingal JSON translation file or multiple single-language files.
// Single-Language JSON files
$qt->addTranslationSource('/translations/en.json', 'en');
$qt->addTranslationSource('/translations/fr.json', 'fr');
// OR Multilingual JSON file
$qt->addTranslationSource('/translations/multilingual.json');
// OR Path to translations directory
$qt->addTranslationSource('/translations/');
You can then load a translation by it's key:
echo $qt->t('translation_key');
// OR echo using a method
$qt->et('translation_key');
Assuming you have one json file for each language. Each file would contain something like this:
{
"translation_example": "Translated Text"
}
Assuming you have one json file containing all translations for all languages. The file would contain something like this:
{
"translation_example": {
"en": "English Translated Text",
"fr": "French Translated Text"
}
}
You can also create a one-time translation as you go. (both alternatives below are equivalent)
$qt->et([
'en' => 'English Test',
'fr' => 'French Text'
]);
// OR Alternative
$qt->et('[:en]English Text[:fr]French Text');
If the url is http://website.com/?language=en
OR http://website.com/
, the result will be:
English Text
If the url is http://website.com/?language=fr
, the result will be:
French Text
// Instantiate PHP Quick Translate
$qt = new QuickTranslate('language');
// Translate
$qt->t('translation_key');
// Same as t() except with echo
$qt->et('translation_key');
// Set the current language
$qt->setLanguage('language');
// Get the current language
$qt->getLanguage();
// Add a translation
$qt->addTranslation('language', 'translation_key', 'translation_value');
// Add a JSON translation source file directory
// translation source file directory
$qt->addTranslationSource('path/to/json/translations/');
// Individual translation source file
$qt->addTranslationSource('path/to/json/translations/language.json', 'language');
$qt->addTranslationSource('path/to/json/translations/multilingual.json');
// Add array or translations
// (single language + multilingual)
$qt->addTranslations(['translation_key' => 'translation_value'], 'language');
$qt->addTranslations([
'translation_key' => [
'en' => 'translation_value',
'fr' => 'translation_value'
]
]);
// Determine if a translation exists for given key
// If language isn't provided, the current language will be used
$qt->hasTranslation('translation_key', 'language');
// Gets the translation for given key
// If language isn't provided, the current language will be used
$qt->getTranslation('translation_key', 'language');