davidlienhard / i18n
🐘 php library to use for internationalization
Installs: 3 759
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:libary
Requires
- php: ^8.0
- league/flysystem: ^3
- nette/neon: ^3
- symfony/yaml: ^6
Requires (Dev)
README
🐘 php library to use for internationalization
Setup
You can install through composer
with:
composer require davidlienhard/i18n:^2
Note: davidlienhard/i18n requires PHP 8.0
How to use
1. Create language files
Create at least one language file. Supported filetypes are json
, yaml
, yml
or ini
.
./lang/en.yml
(English)
save: Save greeting: Hi %1
./lang/de.yml
(German)
save: Speichern greeting: Hallo %1
2. Load the class
Use composer autoloader if possible or include the files in the src
folders manually
3. Create the object
<?php declare(strict_types=1); use DavidLienhard\i18n\i18n; $i18n = new i18n;
4. Set the options
you can either set some options right through the constructor or via the set methods
$i18n = new i18n( filePath: "./lang/{LANGUAGE}.yml", cachePath: "./cache/", fallbackLang: "en", prefix: "L" ); $i18n->setNamespace("YourApp\Translations");
The following setter methods are available:
setFilePath(string $filePath)
: Sets the path to the language filessetCachePath(string $cachePath)
: Sets the path to the cache directorysetFallbackLang(string $fallbackLang)
: sets a fallback languagesetMergeFallback(bool $mergeFallback)
: whether or not to merge the fallback languagesetPrefix(string $prefix)
: Sets the prefix/name of the class to contain the translationssetForcedLang(string $forcedLang)
: a language that is forced to be usedsetSectionSeparator(string $sectionSeparator)
: the character to use to concatenate sections
5. Initialize the class / create cache-files
$i18n->init();
Thss will then create the cache file if required and load the new translation data with the given namespace & prefix/class-name.
6. Use the translation data
use YourApp\Translations\L; echo L::save; // Save / Speichern echo L::get("save"); // Save / Speichern echo L::greeting("David"); // Hi David // Hallo David echo L::get("greeting", "David"); // Hi David // Hallo David
License
The MIT License (MIT). Please see LICENSE for more information.