Download the PHP package sroehrl/php-i18n-translate without Composer
On this page you can find all versions of the php package sroehrl/php-i18n-translate. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sroehrl/php-i18n-translate
More information about sroehrl/php-i18n-translate
Files in sroehrl/php-i18n-translate
Package php-i18n-translate
Short Description Simple yet powerful i18n support for PHP projects
License MIT
Informations about the package php-i18n-translate
PHP i18n translate
Straight forward. Convenient. Fast.
Installation
composer require sroehrl/php-i18n-translate
Quick start:
1. In Code
Outputs:
2. In HTML
main.html
Outputs:
Table of Contents
- Installation
- Quick Start
- Table of Contents
- Initialization
- Setting up translations
- Time, Date, Currencies & Numbers
- Time & Date Formats
- Usage with "Template" recommended
- Placeholders & Dynamic values
Initialization
$t = new I18nTranslate(string $locale = null, string $clientTimezone = null)
You can initialize either with or without a ISO-locale (e.g. 'en-US'). If no value is provided, the class first tries to set the locale by the ACCEPT_LANGUAGE header and if that fails defaults to "en-US". If you don't pass a $clientTimeZone (e.g. 'Europe/Paris'), then a guess is made based on the locale. This can potentially lead to time offsets in countries with multiple timezones.
TIPS:
Timezones: There are several "timezone-guessing" mechanisms around. Using JavaScript is usually the most reliable way.
Settings: When dealing with internationalization, setting your server & database to UTC is a battle-tested approach.
Setting up translations
Whether you read your translations from a database or a file: gettext is not required, and you are expected to
run the method setTranslations for every language you support.
$t->setDebug(true)
Will output a missing key message when a translation isn't set, rather than the following default behavior. This can be useful while developing/translating.
- If the language is not found, the class will default to the first defined language
- If a key is not found, the class will return the key
- If the key has a suffix (.plural), it will be removed
About locale-translations: The decision to ignore the country-specification on the locale on translations is intended. While formatting reacts to the differences of country localisation, translations do not. Example en-US vs. en-EN: the date formatting will react to these differences, but translations like 'color' <=> 'colour' are not supported.
Time, Date, Currencies & Numbers
This package includes a formatter for currencies, numbers and dates. If you want to use its functionality outside of an HTML template, you can initialize it yourself.
The following converters are at your disposal:
- date (accepts optional format)
- date-local (accepts optional format)
- time (accepts optional format)
- time-local (accepts optional format)
- currency (accepts optional, but recommended, ISO currency code like "USD")
- number
In most scenarios the templating attributes will be sufficient to handle your needs:
For a better understanding of how to pass values to your HTML, read here
Time & Date formats
This package uses the Intl-extension for PHP but has a fallback mechanisms. If you do not have Intl installed, localized transformation does not work.
Date & Time inputs are interpreted either as UNIX timestamps or strings supported by PHP's strtotime function.
Date & Time formats accepts strings in the format of ISO8601 date format So not PHP's date notation
I kindly ask contributors to find an appropriate list. Until then, this dated Zend list is the best I could find: formats
Default fallback formats:
| Metric | Imperial | |
|---|---|---|
| date | dd.MM.Y | MM/dd/Y |
| time | HH:mm | hh:mm A z |
Numbers and currencies work with or without the Intl-extension, but might not conform to the ISO 8601 standard without the Intl-extension.
The examples at Quick Start should help.
Using i18nTranslate with version 2+ of the neoan3-apps/template engine.
Under the hood this package interprets html-files with the help of neoan3-apps/template. It is therefore already available to you once you installed this package. The following setup allows the template engine to run PRIOR to translations, making dynamic formats and values possible:
test.html
Example-output:
Placeholders & Dynamic values
Placeholders are written embraced by [% ans %] (e.g. [% my-var %]). They enable dynamic values within a translation.
... or when using template values:
Attributes as functions
When using the attributes within t-tags as functions, they can be referenced as follows:
- i18n-currency -> [%currency-value%]
- i18n-time, i18n-time-local -> [%time-value%]
- i18n-date, i18n-date-local -> [%date-value%]
- i18n-number -> [%number-value%]
CONTRIBUTION
rules