🐘 php library to use for internationalization

2.0.4 2022-11-07 20:56 UTC

This package is auto-updated.

Last update: 2024-10-22 09:35:41 UTC


README

🐘 php library to use for internationalization

Latest Stable Version Source Code Software License Minimum PHP Version CI Status

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 files
  • setCachePath(string $cachePath): Sets the path to the cache directory
  • setFallbackLang(string $fallbackLang): sets a fallback language
  • setMergeFallback(bool $mergeFallback): whether or not to merge the fallback language
  • setPrefix(string $prefix): Sets the prefix/name of the class to contain the translations
  • setForcedLang(string $forcedLang): a language that is forced to be used
  • setSectionSeparator(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.