PHP Classes

How to Adapt PHP Applications to Support Multiple Idioms using the Package PHP Multilanguage Strings: Get texts to use in multilanguage applications

Recommend this page to a friend!
  Info   View files Documentation   View files View files (24)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-04-05 (22 hours ago) RSS 2.0 feedNot yet rated by the usersTotal: 1 This week: 1All time: 11,297 This week: 140Up
Version License PHP version Categories
multi-lang 1.0MIT/X Consortium ...8Localization, Cache, PHP 8
Description 

Author

This package can get texts to use in multilanguage applications.

It can load text strings from JSON files that define application text strings in a given language.

The package can return text strings assigned to a given text and optionally format the strings with text parameters that replace certain marks in those strings.

It can also store the loaded text strings in cache storage, which is supported by cache driver classes provided by the package.

Currently, it provides cache driver classes to store loaded text strings in arrays or does not store those strings in any caching storage.

Picture of Mateo
  Performance   Level  
Name: Mateo <contact>
Classes: 23 packages by
Country: Peru Peru
Innovation award
Innovation award
Nominee: 10x

Winner: 3x

Documentation

Multi-lang

A simple package to manage multi-language in your project.

Installation

composer require mateodioev/multi-lang

Usage

  • Create a directory to save your language files, for example `resources/lang`.
  • Create a file for each language you want to support, for example `en.json` and `es.json`.

File en.json:

{
    "english_name": "English",
    "name": "Spanish",
    "data": {
        "welcome": "Welcome {full_name} to our website",
        "goodbye": "Goodbye dear {full_name}"
    }
}

File es.json:

{
    "english_name": "Spanish",
    "name": "Español",
    "data": {
        "welcome": "Bienvenido {full_name} a nuestro sitio web",
        "goodbye": "Adiós querido {full_name}"
    }
}

  • Configure the dir
use Mateodioev\MultiLang\Cache\InMemoryCache;
use Mateodioev\MultiLang\Lang;

Lang::setup(
    dir: __DIR__ . '/resources/lang',
    cache: new InMemoryCache() // Optional
);

  • Format the strings
// If the key does not exist it will return null
Lang::get('es')->data('welcome')?->format(['full_name' => 'Mateo']);
// Output: Bienvenido Mateo a nuestro sitio web

File format

  • `english_name`: The name of the language in English.
  • `name`: The name of the language in its own language.
  • `data`: The key-value pairs of the language strings. - `key`: The key of the string. - `value`: The value of the string. You can use placeholders to replace them with values. > WARNING: If the json file does not contain any of these keys or different data it will give an error.

Validate the files

You can validate the files to check if they have the correct format. > WARNING: Not use in production. This load and parse all the files

Lang::compareData();

  Files folder image Files  
File Role Description
Files folder imagesrc (6 files, 3 directories)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .php-cs-fixer.dist.php Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file readme.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageCache (3 files)
Files folder imageExceptions (8 files)
Files folder imageParser (2 files)
  Plain text file Builder.php Class Class source
  Plain text file BulkBuilder.php Class Class source
  Plain text file DataAccessor.php Class Class source
  Plain text file Lang.php Class Class source
  Plain text file Language.php Class Class source
  Plain text file Parser.php Class Class source

  Files folder image Files  /  src  /  Cache  
File Role Description
  Plain text file Cache.php Class Class source
  Plain text file InMemoryCache.php Class Class source
  Plain text file NullCache.php Class Class source

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file EmptyDirectoryException.php Class Class source
  Plain text file FileException.php Class Class source
  Plain text file InvalidDirectoryException.php Class Class source
  Plain text file InvalidFormatParamsException.php Class Class source
  Plain text file LanguageDataMismatchException.php Class Class source
  Plain text file LanguageTokenMismatchException.php Class Class source
  Plain text file MultiLangExceptionInterface.php Class Class source
  Plain text file RequiredParamException.php Class Class source

  Files folder image Files  /  src  /  Parser  
File Role Description
  Plain text file FileParser.php Class Class source
  Plain text file StringParser.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:1
This week:1
All time:11,297
This week:140Up