https://github.com/tobyzerner/fluent-php
https://github.com/tobyzerner/fluent-php
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/tobyzerner/fluent-php
- Owner: tobyzerner
- License: apache-2.0
- Created: 2019-11-14T04:12:28.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-25T02:17:26.000Z (about 6 years ago)
- Last Synced: 2025-01-28T16:27:44.529Z (over 1 year ago)
- Language: PHP
- Size: 31.3 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fluent PHP [![Build Status][travisimage]][travislink]
[travisimage]: https://travis-ci.org/tobyz/fluent-php.svg?branch=master
[travislink]: https://travis-ci.org/tobyz/fluent-php
This library is a PHP implementation of [Project Fluent][], a localization
framework designed to unleash the expressive power of the natural language.
It is a simple port of the official JavaScript [@fluent/bundle][] package with a very similar API and source code architecture. It also includes an overlay utility based on [@fluent/dom][] to allow HTML strings to be translated.
[Project Fluent]: https://projectfluent.org
[@fluent/bundle]: https://github.com/projectfluent/fluent.js/tree/master/fluent-bundle
[@fluent/dom]: https://github.com/projectfluent/fluent.js/tree/master/fluent-dom
## Installation
composer install tobyz/fluent-php
## Usage
```php
use Tobyz\Fluent\Bundle;
use Tobyz\Fluent\Resource;
use Tobyz\Fluent\Overlay;
$resource = new Resource('
-brand-name = Foo 3000
welcome = Welcome, {$name}, to {-brand-name}!
');
$bundle = new Bundle('en-US');
$errors = $bundle->addResource($resource);
if (count($errors)) {
// Syntax errors are per-message and don't break the whole resource
}
$welcome = $bundle->getMessage('welcome');
if ($welcome) {
$bundle->formatPattern($welcome['value'], ['name' => 'Anna']);
// → "Welcome, Anna, to Foo 3000!"
}
// Overlay translations and attributes onto HTML
// See https://github.com/projectfluent/fluent.js/wiki/DOM-Overlays
Overlay::translateHtml(
'

',
[
'value' => 'Hello,
!',
'attributes' => ['title' => 'Hello']
]
);
// → Hello,
!
```
## Caveats
* There is no PHP implementation of `Intl.PluralRules`. For now a basic "polyfill" has been included but it only contains the rule for English cardinals.
* PHP includes the [NumberFormatter][] and [IntlDateFormatter][] classes, but these lack some of the functionality of `Intl.NumberFormat` and `Intl.DateTimeFormat`, and thus the full API for the [built-in functions][] cannot be supported. Specifically:
* In `NUMBER`, the `currencyDisplay` option is not supported.
* In `DATETIME`, no options are supported except for `timeZone`. However, the [experimental] `dateStyle` and `timeStyle` options are supported.
* The library currently has no tests.
[NumberFormatter]: https://www.php.net/manual/en/class.numberformatter.php
[IntlDateFormatter]: https://www.php.net/manual/en/class.intldateformatter.php
[built-in functions]: https://projectfluent.org/fluent/guide/functions.html
[experimental]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat#Parameters
## Contributing
Fluent PHP is open-source, licensed under the Apache License, Version 2.0. Feel free to send pull requests or create issues if you come across problems or have great ideas.