Download the PHP package seablast/i18n without Composer
On this page you can find all versions of the php package seablast/i18n. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download seablast/i18n
More information about seablast/i18n
Files in seablast/i18n
Package i18n
Short Description A lightweight internationalization (i18n) module designed for apps using the Seablast for PHP framework
License MIT
Informations about the package i18n
Seablast I18n
A lightweight internationalization (i18n) module for apps using the Seablast for PHP framework. It provides a Latte translation filter, a language-selection API, optional Universal Language Selector templates, and Phinx migrations for translation data. Installable via Composer, it integrates seamlessly and activates only when needed, allowing you to effortlessly provide multilingual support and manage user language preferences.
Usage
UI
The Latte filter translate uses the dictionary loaded by Seablast\I18n\SeablastTranslate. Seablast registers it in Seablast\SeablastView::renderLatte() from the SeablastConstant::TRANSLATE_CLASS setting defined in app.conf.php.
Use as: const back = {="Zpět"|translate};
Note: In Latte, SB:LANGUAGE is defined lazily by the translator. For that reason, {=''|translate} in views/uls.menu.latte runs before SB:LANGUAGE is read.
To display the language selector, include the three uls.*.latte files as follows:
Note: The I18n:SHOW_LANGUAGE_SELECTOR flag controls whether the contents of all uls.*.latte templates are rendered. As a result, you do not need to wrap uls.*.latte includes in custom Latte conditions; include them, and the application decides whether they take effect.
Instead of using the language selector, you can switch the language programmatically by calling:
The caller is responsible for reloading or re-rendering the page so translated strings update:
The window.languageSelector function is declared in uls.js.latte.
That function returns jQuery.Promise (a promise-like object with .then(), .done(), .fail(), .always())
and in the fulfillment value, there's JSON, e.g. {message: 'en'}.
The language switching endpoint exists independently, but the UI selector is gated by I18n:SHOW_LANGUAGE_SELECTOR to prevent exposing unfinished or tenant-specific i18n:
Note: only languages from the configuration (e.g. ->setArrayString(I18nConstant::LANGUAGE_LIST, ['en', 'cs'])) are accepted. The first configured language is the default.
Database structure
To create the expected database table structure (for dictionary and localised items), just add the seablast/i18n migration path to your phinx.php configuration, e.g.
Dictionary table: translations
| Column | Type | Attributes | Description |
|---|---|---|---|
id |
integer | Primary key, auto-increment (identity) |
Unique identifier for each translation entry. |
language |
string(5) | Indexed, part of unique constraint | Configured language code (for example en or cs). |
translation_key |
string(255) | Indexed, part of unique constraint | The lookup key used in the application (e.g., "Save PDF", "Back"). |
translation_value |
text | Localized string corresponding to the key in the given language. |
Integration
- Seablast/Seablast::v0.2.11 contains
APP_DIR . '/vendor/seablast/i18n/conf/app.conf.php', // Seablast/i18n extension configurationso use at least this Seablast version. "seablast/seablast": "^0.2.7"is in therequire-devsection ofcomposer.jsonbecause the app that uses Seablast I18n may use whatever dev version of Seablast.
Language API
- API
'/api/language'using'model' => '\Seablast\I18n\Models\ApiLanguageModel'returns the selected language or accepts a language to store in the cookie 'sbLanguage'. - The cookie 'sbLanguage' is created after a successful language-selection request.
Language selector
- Because typically
.htaccessusesRedirectMatch 404 vendor\/(?!seablast\/)to make vendor folder off limits for web access except the seablast library, the jquery.uls is in Seablast for PHP since v0.2.11 and not in this module. - However, it's useful to know that to make the SVG icon in
.uls-triggeradopt thefont-colorof the surrounding element, the following style was added intouls/images/language.svg:fill="currentColor". Alsouls/css/jquery.uls.csswas changed (changed:.uls-trigger, added:.uls-trigger iconand.uls-trigger .icon svg). - The selector uses
I18nConstant::LANGUAGE_LISTas both the allowed ULS language set and itsquickList; language labels come from ULS data instead of hardcoded labels in this library. - Language is lazy-initialised in SeablastView:
$translator = new $translatorClass($this->model->getConfiguration());instantiates SeablastTranslate, which calls$lang = new ApiLanguageModel($this->configuration, new \Seablast\Seablast\Superglobals());. There$this->configuration->setString('SB:LANGUAGE', $result);is set. '/api/language'using'model' => '\Seablast\I18n\Models\ApiLanguageModel'is called from window.languageSelector when uls.onSelect with parameter.
Localised data access
Extend the class FetchLocalisedItemsModel with preset of these three properties
in order to access the localised items filtered by their type.
The full class looks like this:
TODO: Find a way to initialise mysqli() automatically, so that it is not dependent on the Seablast\Auth presence.
(See Seablast\Dist BlogModel.php.)
This MODEL yields items one by one from the database in a lazy, memory efficient, way. The VIEW can display it as follows:
This code can be seen live in Seablast\Dist blog-readonly.latte. The texts can also be directly editable by admins as seen in Seablast\Dist blog-editable.latte. (This of course requires users to be logged in, hence Seablast\Auth is required.)
All versions of i18n with dependencies
nette/utils Version ^3.2.10 || ^4.0.5 || ^4.1.0
robmorgan/phinx Version ^0.12.13 || ^0.13.4 || ^0.14.0 || ^0.15.5 || ^0.16.5
tracy/tracy Version ^2.9.8 || ^2.10.9 || ^2.11.0
webmozart/assert Version ^1.10.0