1. Go to this page and download the library: Download wufr/php-language-localizer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wufr / php-language-localizer example snippets
// Import the Translator class
use wUFr\Translator;
// Create a new translator instance
$translator = new Translator(
dir: "./locales/", // Directory containing translation files
lang: "en_US" // Language code
);
// You can also set/change these values after initialization
$translator->setDirectory("./custom/locales/");
$translator->setLanguage("fr_FR");
// Translate a simple string
echo $translator->locale("common/general", "welcome");
// Output: "Welcome to our application"
// Translation file:
// "items" => [
// "male" => [
// 1 => "He has one item",
// 2 => "He has two items",
// 5 => "He has many items"
// ],
// "female" => [
// 1 => "She has one item",
// 2 => "She has two items",
// 5 => "She has many items"
// ],
// "neutral" => [
// 1 => "They have one item",
// 2 => "They have two items",
// 5 => "They have many items"
// ],
// "entity" => [
// 1 => "It contains one component",
// 2 => "It contains two components",
// 5 => "It contains many components"
// ]
// ]
// Code:
echo $translator->locale("users/inventory", "items", [
"_gender" => "female",
"_counter" => 1
]);
// Output: "She has one item"
echo $translator->locale("users/inventory", "items", [
"_gender" => "entity",
"_counter" => 3
]);
// Output: "It contains two components"
// Translation file:
// "purchase" => [
// 1 => "Thank you {name} for buying a piece of {product}",
// 2 => "Thank you {name} for buying two pieces of {product}",
// 5 => "Thank you {name} for buying {count} pieces of {product}"
// ]
// Code:
echo $translator->locale("shop/checkout", "purchase", [
"_counter" => 50,
"count" => 50, // This is used as a parameter in the string
"name" => "John",
"product" => "Premium Widget"
]);
// Output: "Thank you John for buying 50 pieces of Premium Widget"
use wUFr\Gender;
// Using enum for gender parameter
echo $translator->locale("users/profile", "biography", [
"_gender" => Gender::Male->value
]);
// Output: "He is a developer"
// For objects, animals, and non-gendered entities
echo $translator->locale("products/description", "details", [
"_gender" => Gender::Entity->value
]);
// Output: "It is a high-quality product"
$gender = Gender::Female;
echo $gender->getPronoun(); // "she"
echo $gender->getPossessivePronoun(); // "her"
echo $gender->getObjectPronoun(); // "her"
echo $gender->getDescription(); // "Female"
// Available gender options:
// - Gender::Male - For male subjects (he/his/him)
// - Gender::Female - For female subjects (she/her/her)
// - Gender::Neutral - For gender-neutral people (they/their/them)
// - Gender::Entity - For objects, animals, babies, etc. (it/its/it)
// If the file doesn't exist
echo $translator->locale("nonexistent", "key");
// Output: '<b style="color:red">lang file NOT found: nonexistent</b>'
// If the key doesn't exist in the file
echo $translator->locale("common/general", "nonexistentKey");
// Output: '<b style="color:red">lang key NOT found: common/general-nonexistentKey</b>'
// If using array translation without counter or gender
echo $translator->locale("common/general", "arrayKey");
// Output: '<b style="color:red">lang counter or gender NOT set</b>'
// If the specified gender doesn't exist in the translation
echo $translator->locale("users/profile", "welcome", ["_gender" => "nonexistent"]);
// Output: '<b style="color:red">lang gender NOT found: nonexistent</b>'
// Constructor
public function __construct(string $dir = "/locales/", string $lang = "en_US")
// Directory and language setters/getters
public function setDirectory(string $dir): self
public function setLanguage(string $lang): self
public function getDirectory(): string
public function getLanguage(): string
// Main translation method
public function locale(string $file, string $key, array $params = []): string
enum Gender: string
{
case Male = 'male';
case Female = 'female';
case Neutral = 'neutral';
case Entity = 'entity';
public function getDescription(): string
public function getPronoun(): string
public function getPossessivePronoun(): string
public function getObjectPronoun(): string
}
bash
composer
/locales/ # Base directory for all translations
/en_US/ # English (US) translations
/common/ # Common translations
general.php # General terms
errors.php # Error messages
/admin/ # Admin panel translations
dashboard.php
users.php
/fr_FR/ # French translations
/common/
general.php
errors.php
...
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.