Skip to content

tobyzerner/fluent-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fluent PHP Build Status

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.

Installation

composer install tobyz/fluent-php

Usage

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(
    '<p><img data-l10n-name="world" src="world.png"></p>',
    [
        'value' => 'Hello, <img data-l10n-name="world" alt="world">!', 
        'attributes' => ['title' => 'Hello']
    ]
);
// → <p title="Hello">Hello, <img data-l10n-name="world" alt="world" src="world.png">!</p>

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.

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages