gokhankurtulus/multilanguage

A simple PHP library for multi-language projects.

1.0.0 2023-12-31 08:15 UTC

This package is auto-updated.

Last update: 2024-05-30 01:00:26 UTC


README

License: MIT PHP Version Release

A simple PHP library for multi-language projects.

Installation

You can install the library using Composer. Run the following command:

composer require gokhankurtulus/multilanguage

Usage

You need to create a directory first and create the desired languages in this directory in JSON format. Example JSON files are provided below.

Example Hierarchy

project
   ├── Lang
   │    ├── de.json
   │    ├── en.json
   │    └── tr.json
   └── index.php

en.json

{
  "Hello": "Hello #name# $lastname$",
  "Homepage": "Homepage"
}

tr.json

{
  "Hello": "Selam #name# $lastname$",
  "Homepage": "Anasayfa"
}

de.json

{
  "Hello": "Hallo #name# $lastname$",
  "Homepage": "Startseite"
}

index.php

use MultiLanguage\MultiLanguage;

MultiLanguage::setDirectoryPath(__DIR__ . DIRECTORY_SEPARATOR . "Lang");
MultiLanguage::setAllowedLanguages(["en", "tr", "de"]);
MultiLanguage::setDefaultLanguage("en");
MultiLanguage::setCurrentLanguage("tr");

// Output will be "Homepage" because $lang parameter is set to 'en'.
// If $lang is null or empty tries to get current language
// if current is not defined then tries to get default language
// if both not defined and $lang is not given throws an LanguageException
echo MultiLanguage::translate('Homepage', 'en');

// Output will be "Anasayfa" because current language is 'tr'.
echo MultiLanguage::translate('Homepage');

// Output will be "Hallo John Doe".
// Specify unique keys in the language file then you can manipulate them.
// In this example, if your key is located many places in string it will change all of them.
echo MultiLanguage::translate('Hello', 'de', ['#name#' => 'John', '$lastname$' => 'Doe']);

Public Methods

MultiLanguage::translate(string  $text, ?string $lang = null, array $replacement = []);

MultiLanguage::getDirectoryPath();
MultiLanguage::setDirectoryPath(string $directoryPath, bool $force = false);

MultiLanguage::getAllowedLanguages();
MultiLanguage::setAllowedLanguages(array $languages);
MultiLanguage::isAllowedLanguage(string $lang);

MultiLanguage::getDefaultLanguage();
MultiLanguage::setDefaultLanguage(string $lang);

MultiLanguage::getCurrentLanguage();
MultiLanguage::setCurrentLanguage(string $lang);

License

MultiLanguage is open-source software released under the MIT License. Feel free to modify and use it in your projects.

Contributions

Contributions to MultiLanguage are welcome! If you find any issues or have suggestions for improvements, please create an issue or submit a pull request on the GitHub repository.