1. Go to this page and download the library: Download bermudaphp/polyglot library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bermudaphp / polyglot example snippets
// Create an I18n instance using the factory
$i18n = I18nFactory::create('/path/to/translations', 'en', 'en');
// Basic translation
echo $i18n->translate('welcome'); // "Welcome!"
// Translation with parameters
echo $i18n->translate('greeting', ['name' => 'John']); // "Hello, John!"
// Translation with pluralization
echo $i18n->translatePlural('items', 1); // "You have 1 item"
echo $i18n->translatePlural('items', 5); // "You have 5 items"
// Shorthand methods
echo $i18n->t('welcome');
echo $i18n->tp('items', 3);
// Change locale
$i18n->setLocale('fr');
echo $i18n->t('welcome'); // "Bienvenue !"
use Bermuda\Polyglot\Translator;
use Bermuda\Polyglot\Loader\PhpArrayMessageLoader;
use Bermuda\Polyglot\Formatter\IcuMessageFormatter;
use Bermuda\Polyglot\PluralRule\CldrPluralRuleProvider;
use Bermuda\Polyglot\Cache\InMemoryCache;
// Create loaders
$loader = new PhpArrayMessageLoader('/path/to/translations');
// Create formatters
$formatter = new IcuMessageFormatter(new CldrPluralRuleProvider());
// Create plural rule provider
$pluralRuleProvider = new CldrPluralRuleProvider();
// Create cache (optional)
$cache = new InMemoryCache();
// Create translator
$translator = new Translator(
locale: 'en',
fallbackLocale: 'en',
loader: $loader,
formatter: $formatter,
pluralRuleProvider: $pluralRuleProvider,
cache: $cache
);
// Create I18n instance
$i18n = new I18n($translator);
use Bermuda\Polyglot\LocaleEnum;
$locale = LocaleEnum::ENGLISH_US;
$i18n->setLocale($locale);
// Get language code
$language = $locale->getLanguageCode(); // 'en'
// Get region code
$region = $locale->getRegionCode(); // 'US'
// Create from string
$locale = LocaleEnum::fromString('en-US'); // Handles both 'en-US' and 'en_US' formats
use Bermuda\Polyglot\Locale;
$locale = new Locale('en_US');
// Access components
echo $locale->language; // 'en'
echo $locale->region; // 'US'
echo $locale->variant; // null
// Convert to string
echo $locale; // 'en_US'
// Get fallbacks
$fallbacks = $locale->getFallbacks(); // ['en']
use Bermuda\Polyglot\Generator\IcuMessage;
// Create a message builder for a specific locale
$builder = IcuMessage::for('en');
// Create a simple message with placeholders
$message = $builder->message('Hello, {name}!');
// Create a gender-specific message
$gender = IcuMessage::gender(
'gender',
'He will attend', // male
'She will attend', // female
'They will attend' // other
);
use Bermuda\Polyglot\Generator\IcuMessage;
// Create a plural builder for English
$pluralBuilder = IcuMessage::plural('count', 'en');
// Add cases manually
$pluralBuilder
->when('one', 'You have # item')
->when('other', 'You have # items');
// Get the ICU message
$icuMessage = $pluralBuilder->build();
// Result: "{count, plural, one{You have # item} other{You have # items}}"
// Using withInflections for common plurals
$builder = IcuMessage::plural('count', 'en')
->withInflections('You have # item', [
'one' => '',
'other' => 's'
]);
// For languages with more complex pluralization (e.g., Russian)
$builder = IcuMessage::plural('count', 'ru')
->withInflections('У вас # товар', [
'one' => '',
'few' => 'а',
'many' => 'ов'
]);
use Bermuda\Polyglot\Generator\IcuMessage;
// Create a select builder
$selectBuilder = IcuMessage::select('role');
// Add cases
$selectBuilder
->when('admin', 'You have full access')
->when('manager', 'You have limited access')
->otherwise('You have basic access');
// Get the ICU message
$icuMessage = $selectBuilder->build();
// Result: "{role, select, admin{You have full access} manager{You have limited access} other{You have basic access}}"
// Using with nested plural
$builder = IcuMessage::select('gender')
->when('male', function($b) {
return $b->plural('count', 'en')
->when('one', 'He has # apple')
->when('other', 'He has # apples');
})
->when('female', function($b) {
return $b->plural('count', 'en')
->when('one', 'She has # apple')
->when('other', 'She has # apples');
});
use Bermuda\Polyglot\Cache\PsrCacheAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
// Create a PSR-16 compatible cache
$psr16Cache = new \Symfony\Component\Cache\Psr16Cache(
new FilesystemAdapter()
);
// Create a cache adapter
$cache = new PsrCacheAdapter($psr16Cache);
// Use with I18n factory
$i18n = I18nFactory::create(
resourcesPath: '/path/to/translations',
defaultLocale: 'en',
fallbackLocale: 'en',
cache: $cache
);
use Bermuda\Polyglot\Cache\InMemoryCache;
$cache = new InMemoryCache();
use Bermuda\Polyglot\Detector\HttpAcceptLanguageDetector;
use Bermuda\Polyglot\Detector\QueryLocaleDetector;
use Bermuda\Polyglot\Detector\PathLocaleDetector;
use Bermuda\Polyglot\Detector\LocaleDetectorChain;
// Available locales
$availableLocales = ['en', 'fr', 'de', 'es'];
// Create detectors
$httpDetector = new HttpAcceptLanguageDetector($availableLocales);
$queryDetector = new QueryLocaleDetector($availableLocales, 'locale');
$pathDetector = new PathLocaleDetector($availableLocales);
// Create a chain of detectors (checked in order)
$detector = new LocaleDetectorChain([
$pathDetector, // First check URL path (/en/page)
$queryDetector, // Then check query param (?locale=en)
$httpDetector // Finally check Accept-Language header
]);
// Use with I18n
$i18n = new I18n($translator, $detector);
// Detect and set locale
$i18n->detectAndSetLocale('en'); // 'en' is the default if no locale is detected
use Bermuda\Polyglot\I18nMiddleware;
// Create middleware
$middleware = I18nFactory::createMiddleware($i18n, 'en');
// Add to your middleware stack
$app->pipe($middleware);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.