PHP code example of m1sh0u / polyglot-php

1. Go to this page and download the library: Download m1sh0u/polyglot-php 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/ */

    

m1sh0u / polyglot-php example snippets


$polyglot = new Polyglot();

$polyglot->extend([
  "hello" => "Hello"
]);

$polyglot->t("hello");
=> "Hello"

$polyglot = new Polyglot(['phrases' => ["hello" => "Hello"]]);

$polyglot->extend([
  "hello_name" => "Hola, %{name}."
]);

$polyglot->t("hello_name", ["name" => "DeNiro"]);
=> "Hola, DeNiro."

$polyglot->extend([
  "nav" => [
    "hello" => "Hello",
    "hello_name" => "Hello, %{name}",
    "sidebar" => [
      "welcome" => "Welcome"
    ]
  ]
]);

$polyglot->t("nav.sidebar.welcome");
=> "Welcome"

$polyglot = new Polyglot({
  "phrases" => [
    "hello_name" => "Hola {{name}}"
  ],
  "interpolation" => ["prefix" => "{{", "suffix" => "}}"]
});

$polyglot->t("hello_name", ["name" => "DeNiro"]);
=> "Hola, DeNiro."

$polyglot->locale()
=> "fr"

$polyglot = new Polyglot(["locale" => "fr"]);

$polyglot->extend([
  "num_cars" => "%{smart_count} car |||| %{smart_count} cars",
]);

$polyglot = new Polyglot(["locale" => "cs"]); // Czech
$polyglot->extend([
  "num_foxes" => "Mám %{smart_count} lišku |||| Mám %{smart_count} lišky |||| Mám %{smart_count} lišek"
])

$polyglot->t("num_cars", ["smart_count" => 0]);
=> "0 cars"

$polyglot->t("num_cars", ["smart_count" => 1]);
=> "1 car"

$polyglot->t("num_cars", ["smart_count" => 2]);
=> "2 cars"

$polyglot->t("num_cars", 2);
=> "2 cars"

use Polyglot\Pluralization\Rules\RuleInterface;

class RomanianRule implements RuleInterface
{
    public function decide(int $n): int
    {
        return $n !== 1 ? 1 : 0;
    }
}

$polyglot = new Polyglot([
    'phrases' => ['num_cars' => '%{smart_count} mașină |||| %{smart_count} mașini'],
    'locale' => 'ro',
    'pluralRules' => ['ro' => new RomanianRule()]
]);

$polyglot->t('num_cars', 1)
=> 1 mașină
$polyglot->t('num_cars', 6)
=> 6 mașini

$polyglot->t("hello");
=> "Hello"

$polyglot->t("hello_name", ["name" => "Spike"]);
=> "Hello, Spike"

// same as: $polyglot->t("car", ["smart_count" => 2]);
$polyglot->t("car", 2);
=> "2 cars"

$polyglot->t("i_like_to_write_in_language", [
  "_" => "I like to write in %{language}.",
  "language" => "JavaScript"
]);
=> "I like to write in JavaScript."

$polyglot->extend([
  "hello" => "Hello",
  "hello_name" => "Hello, %{name}"
]);

$polyglot->unset('some_key');
$polyglot->unset([
  'hello' => 'Hello',
  'hello_name' => 'Hello, %{name}',
  'foo' => [
    'bar' => 'This phrase’s key is "foo.bar"'
  ]
]);