Composer package providing translation features for PHP apps

Overview

PHP translation

This is a composer package providing translation support for PHP applications. It is similar to gettext, in usage, with these differences:

  • Strings and translations are stored in a database
  • Translation cache in a keystore is supported (memcache or Redis usage is expected)
  • The __() function (similar to gettext's _()) supports \sergiosgc\sprintf style calls if sergiosgc-sprintf is present; e.g. __('Operator name set to %<name>', $operator)

Installation

Install via composer. In you composer.json:

{
    "repositories": [
        ...
        {
            "type": "vcs",
            "url": "https://github.com/sergiosgc/translation"
        }
    ],
    "require": {
        ...
        "sergiosgc/translation": "*"
    }
}

Database schema

Two tables, string and translation, in schema localization:

 Column  |  Type   | Collation | Nullable |                     Default                     
---------+---------+-----------+----------+-------------------------------------------------
 id      | integer |           | not null | nextval('localization.string_id_seq'::regclass)
 value   | text    |           | not null | 
 context | text    |           | not null | 
 in_use  | boolean |           | not null | true
Indexes:
    "string_pkey" PRIMARY KEY, btree (id)

and

              Table "localization.translation"
      Column      |  Type   | Collation | Nullable | Default 
------------------+---------+-----------+----------+---------
 string           | integer |           | not null | 
 locale_language  | text    |           | not null | 
 locale_territory | text    |           | not null | 
 value            | text    |           |          | 
Indexes:
    "translation_pkey" PRIMARY KEY, btree (string, locale_language, locale_territory)

Application setup

You need to setup the package when the application starts:

  1. Give it a callback to obtain the database connection (a PDO object)

  2. Provide a cache adapter object

    // Pass in a callable that returns a PDO connection
    \sergiosgc\translation\Translation::singleton()->setGetDatabase([ '\app\App', 'getDatabase' ]);
    // Pass in a \sergiosgc\translation\ICache
    \sergiosgc\translation\Translation::singleton()->setCache( new \sergiosgc\translation\RedisCache('localhost', 6379 ));
    

Then, at some point set the locale:

\sergiosgc\translation\Translation::singleton()->setLocale('pt_PT');

Usage

Usage is similar to gettext. Use __():

print(__('This will be translated'));

If \sergiosgc\sprintf is installed, you may use __() with the \sergiosgc\sprintf() signature:

print(__('This will be processed by %<package> after translation', [ 'package' => '\sergiosgc\sprintf' ]));

The print(__()) pattern can be condensed using __p():

__p('This will be processed by %<package> after translation', [ 'package' => '\sergiosgc\sprintf' ]);
You might also like...
The Translation component provides tools to internationalize your application.

Translation Component The Translation component provides tools to internationalize your application. Getting Started $ composer require symfony/transl

Filament Translations - Manage your translation with DB and cache
Filament Translations - Manage your translation with DB and cache

Filament Translations Manage your translation with DB and cache, you can scan your languages tags like trans(), __(), and get the string inside and tr

French-Traduction-Pterodactyl est la traduction française de pterodactyl French-Traduction-Pterodactyl is the French translation of pterodactyl

French-Traduction-Pterodactyl Star French-Traduction-Pterodactyl est la traduction française de pterodactyl French-Traduction-Pterodactyl is the Frenc

Translation (i18n) Manager as a virion

TL Translation (i18n) Manager as a virion Translation use hook-like $t = $tl-useTranslation($player-getLocale()); $player-sendMessage($t("message-k

A convenience package for php multilingual web applications
A convenience package for php multilingual web applications

PHP Translation Install Lifecycle Configuration Content of PHP File Content of Json File Content Of Database Table Use Of Array Or Json Database PHP T

[Deprecated] A Laravel package for multilingual models
[Deprecated] A Laravel package for multilingual models

This package has been deprecated. But worry not. You can use Astrotomic/laravel-translatable. Laravel-Translatable If you want to store translations o

Package to manage Laravel translations locally
Package to manage Laravel translations locally

Translation Manager For Laravel Easy to use package that helps you with the translation of your Laravel application locally. Features ✅ Check all loca

pH7CMS Internationalization package.

🎌 pH7CMS Internationalization (I18N) package 🙊 Get new languages for your pH7CMS website!

This package allows to translate text using Google Translator for free without api.

Text Translator for PHP This package allows text translation using Google Translate for free without an API. It extracts the translated text from the

Owner
Sérgio Carvalho
Sérgio Carvalho
Official PHP library for the DeepL language translation API.

deepl-php Official PHP client library for the DeepL API. The DeepL API is a language translation API that allows other computer programs to send texts

DeepL 78 Dec 23, 2022
Better translation management for Laravel

Better localization management for Laravel Introduction Keeping a project's translations properly updated is cumbersome. Usually translators do not ha

Waavi 354 Dec 18, 2022
[virion] Language management library for automatic translation

libtranslator :: library for automatic translation ✔️ Multilingual support for plugin messages ✔️ Translation language is set according to the player

PocketMine-MP projects of PresentKim 5 Jul 29, 2022
Laravel translation made __('simple').

Translation.io client for Laravel 5.5+/6/7/8 Add this package to localize your Laravel application. Use the official Laravel syntax (with PHP or JSON

Translation.io 109 Dec 29, 2022
🎌 Laravel Localization Helper :: Easily add translation variables from Blade templates.

LocalizationHelper Package for convenient work with Laravel's localization features and fast language files generation. Take a look at contributing.md

Awes.io 36 Jul 18, 2022
Manage Laravel translation files

Laravel 5 Translation Manager For Laravel 4, please use the 0.1 branch! This is a package to manage Laravel translation files. It does not replace the

Barry vd. Heuvel 1.5k Jan 4, 2023
A GUI for managing JSON translation files in your laravel projects.

Laravel Language Manager Langman is a GUI for managing your JSON language files in a Laravel project. Installation Begin by installing the package thr

Mohamed Said 515 Nov 30, 2022
A Gui To Manage Laravel Translation Files

Lingo A file based translation manager, which unlike other Lang managers don't need a database connection to handle the translation. Installation comp

Muah 97 Dec 5, 2022
Provides support for message translation and localization for dates and numbers.

The I18n library provides a I18n service locator that can be used for setting the current locale, building translation bundles and translating messages. Additionally, it provides the Time and Number classes which can be used to output dates, currencies and any numbers in the right format for the specified locale.

CakePHP 26 Oct 22, 2022
Internationalization tools, particularly message translation.

Aura.Intl The Aura.Intl package provides internationalization (I18N) tools, specifically package-oriented per-locale message translation. Installation

Aura for PHP 86 Dec 18, 2022