Skip to content

Commit

Permalink
chore: write new tests and add a new example
Browse files Browse the repository at this point in the history
  • Loading branch information
williamdes committed Mar 31, 2021
1 parent b988af5 commit 3ee642a
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 12 deletions.
59 changes: 50 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,65 @@ composer require wdes/php-i18n-l10n

Have a look at example file [example/simple.php](example/simple.php)

### Example
### Example without a MO file

```php
<?php

declare(strict_types = 1);

// Can be removed :)

require_once __DIR__ . '/../vendor/autoload.php';

use \Wdes\phpI18nL10n\plugins\MoReader;
use \Wdes\phpI18nL10n\Launcher;
use \Wdes\phpI18nL10n\Twig\Extension\I18n as ExtensionI18n;
use \Twig\Environment as TwigEnvironment;
use \Twig\Loader\FilesystemLoader as TwigLoaderFilesystem;
use Wdes\phpI18nL10n\plugins\MoReader;
use Wdes\phpI18nL10n\Launcher;
use Wdes\phpI18nL10n\Twig\Extension\I18n as ExtensionI18n;
use Twig\Environment as TwigEnvironment;
use Twig\Loader\ArrayLoader as TwigLoader;

$dataDir = __DIR__ . '/locale/';
$moReader = new MoReader(
['localeDir' => $dataDir]
$moReader = new MoReader();
$moReader->setTranslations(
[
'Homepage' => 'Page d\'accueil',
]
);
// Load the translation plugin
Launcher::setPlugin($moReader);

$twig = new TwigEnvironment(new TwigLoader());
$twig->addExtension(new ExtensionI18n());
// You can use a file instead, see the example using a mo file
$templateContents = <<<HTML
<html>
<title>{% trans %}Homepage{% endtrans %}</title>
<body>
{% trans %}Homepage{% endtrans %}
</body>
</html>
HTML;
echo $twig->createTemplate($templateContents)->render([]);
```

### Example with a MO file

```php
<?php

declare(strict_types = 1);

// Can be removed :)

require_once __DIR__ . '/../vendor/autoload.php';

use Wdes\phpI18nL10n\plugins\MoReader;
use Wdes\phpI18nL10n\Launcher;
use Wdes\phpI18nL10n\Twig\Extension\I18n as ExtensionI18n;
use Twig\Environment as TwigEnvironment;
use Twig\Loader\FilesystemLoader as TwigLoaderFilesystem;

$dataDir = __DIR__ . '/locale/';
$moReader = new MoReader();
$moReader->readFile($dataDir . 'fr.mo'); // Load the file you want (a specific language for example)
// Load the translation plugin
Launcher::setPlugin($moReader);
Expand All @@ -61,6 +101,7 @@ echo $twig->render(
'say' => 'Hello world'
]
);

```

### Scripts
Expand Down
35 changes: 35 additions & 0 deletions example/no-data-file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types = 1);

// Can be removed :)

require_once __DIR__ . '/../vendor/autoload.php';

use Wdes\phpI18nL10n\plugins\MoReader;
use Wdes\phpI18nL10n\Launcher;
use Wdes\phpI18nL10n\Twig\Extension\I18n as ExtensionI18n;
use Twig\Environment as TwigEnvironment;
use Twig\Loader\ArrayLoader as TwigLoader;

$moReader = new MoReader();
$moReader->setTranslations(
[
'Homepage' => 'Page d\'accueil',
]
);
// Load the translation plugin
Launcher::setPlugin($moReader);

$twig = new TwigEnvironment(new TwigLoader());
$twig->addExtension(new ExtensionI18n());
// You can use a file instead, see the example using a mo file
$templateContents = <<<HTML
<html>
<title>{% trans %}Homepage{% endtrans %}</title>
<body>
{% trans %}Homepage{% endtrans %}
</body>
</html>
HTML;
echo $twig->createTemplate($templateContents)->render([]);
4 changes: 1 addition & 3 deletions example/simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
use Twig\Loader\FilesystemLoader as TwigLoaderFilesystem;

$dataDir = __DIR__ . '/locale/';
$moReader = new MoReader(
['localeDir' => $dataDir]
);
$moReader = new MoReader();
$moReader->readFile($dataDir . 'fr.mo'); // Load the file you want (a specific language for example)
// Load the translation plugin
Launcher::setPlugin($moReader);
Expand Down
252 changes: 252 additions & 0 deletions tests/I18nTestNoData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
<?php

declare(strict_types = 1);

/* The contents of this file is free and unencumbered software released into the
* public domain.
* For more information, please refer to <http://unlicense.org/>
*/
namespace Wdes\phpI18nL10n\Tests;

use PHPUnit\Framework\TestCase;
use Wdes\phpI18nL10n\Twig\Extension\I18n as ExtensionI18n;
use Twig\Environment as TwigEnv;
use Twig\Loader\FilesystemLoader as TwigLoaderFilesystem;
use Wdes\phpI18nL10n\plugins\MoReader;
use Wdes\phpI18nL10n\Launcher;

/**
* @author William Desportes <[email protected]>
* @license Unlicense
*/
class I18nTestNoData extends TestCase
{

/**
* The TwigEnv object
*
* @var TwigEnv
*/
private $twig = null;

/**
* The MoReader object
*
* @var MoReader
*/
private $moReader = null;

/**
* Set up the instance
*
* @return void
*/
public function setUp(): void
{
$this->moReader = new MoReader();
Launcher::setPlugin($this->moReader);

$loader = new TwigLoaderFilesystem();
$this->twig = new TwigEnv(
$loader
);

$this->twig->addExtension(new ExtensionI18n());
}

/**
* Test simple translation using get and set
* @return void
*/
public function testSimpleTranslationGetSetTranslations(): void
{
$template = $this->twig->createTemplate(
'{% trans "Translate this" %}',
'testSimpleTranslationGetSetTranslations'
);
$html = $template->render([]);
$this->assertNotEmpty($html);
$this->assertEquals('Translate this', $html);
$this->moReader->setTranslations([]);
$this->assertSame($this->moReader->getTranslations(), []);
$html = $template->render([]);
$this->assertNotEmpty($html);
$this->assertEquals('Translate this', $html);
$this->moReader->setTranslations(
[
'Translate this' => 'blob blob blob',
]
);
$this->assertSame(
$this->moReader->getTranslations(),
[
'Translate this' => 'blob blob blob',
]
);
$html = $template->render([]);
$this->assertNotEmpty($html);
$this->assertEquals('blob blob blob', $html);
}

/**
* Test simple translation
* @return void
*/
public function testSimpleTranslation(): void
{
$template = $this->twig->createTemplate(
'{% trans "Translate this" %}'
);
$html = $template->render([]);
$this->assertEquals('Translate this', $html);
$this->assertNotEmpty($html);
}

/**
* Test simple translation with a comment
* @return void
*/
public function testSimpleTranslationWithComment(): void
{
$template = $this->twig->createTemplate(
'{% trans %}Translate this{% notes %}And note{% endtrans %}'
);
$html = $template->render([]);
$this->assertEquals('Translate this', $html);
$this->assertNotEmpty($html);
}

/**
* Test simple translation with context
* @return void
*/
public function testSimpleTranslationWithContext(): void
{
$template = $this->twig->createTemplate(
'{% trans %}Translate this{% context %}NayanCat{% endtrans %}'
);
$html = $template->render([]);
$this->assertEquals('Translate this', $html);
$this->assertNotEmpty($html);
}

/**
* Test simple translation with context and a variable
* @return void
*/
public function testSimpleTranslationWithContextAndVariable(): void
{
$template = $this->twig->createTemplate(
'{% trans %}Translate this {{name}} {% context %}The user name{% endtrans %}'
);
$html = $template->render(
[
'name' => 'williamdes',
]
);
$this->assertEquals('Translate this williamdes', $html);
$this->assertNotEmpty($html);
}

/**
* Test simple translation with context and some variables
* @return void
*/
public function testSimpleTranslationWithContextAndVariables(): void
{
$template = $this->twig->createTemplate(
'{% trans %}Translate this {{key}}: {{value}} {% context %}The user name{% endtrans %}'
);
$html = $template->render(
[
'key' => 'user',
'value' => 'williamdes',
]
);
$this->assertEquals('Translate this user: williamdes', $html);
$this->assertNotEmpty($html);
}

/**
* Test plural translation
* @return void
*/
public function testPluralTranslation(): void
{
$template = $this->twig->createTemplate(
'{% trans %}One person{% plural nbr_persons %}{{ nbr }} persons{% endtrans %}'
);
$html = $template->render(['nbr' => 5]);
$this->assertEquals('One person', $html);
$this->assertNotEmpty($html);
}

/**
* Test plural translation with comment
* @return void
*/
public function testPluralTranslationWithComment(): void
{
$template = $this->twig->createTemplate(
'{% trans %}one user likes this.{% plural nbr_persons %}{{ nbr }} users likes this.{% notes %}Number of users{% endtrans %}'
);
$html = $template->render(['nbr' => 5]);
$this->assertEquals('one user likes this.', $html);
$this->assertNotEmpty($html);
}

/**
* Test simple plural translation
* @return void
*/
public function testSimplePluralTranslation(): void
{
$template = $this->twig->createTemplate(
'{% trans %}One person{% plural a %}persons{% endtrans %}'
);
$html = $template->render(['nbr' => 5]);
$this->assertEquals('One person', $html);
$this->assertNotEmpty($html);
}

/**
* Test simple plural translation using count
* @return void
*/
public function testSimplePluralTranslationCount(): void
{
$template = $this->twig->createTemplate(
'{% trans %}One person{% plural a.count %}persons{% endtrans %}'
);
$html = $template->render(['a' => ['1', '2']]);
$this->assertEquals('One person', $html);
$this->assertNotEmpty($html);
}

/**
* Test simple plural translation using count and vars
* @return void
*/
public function testSimplePluralTranslationCountAndVars(): void
{
$template = $this->twig->createTemplate(
'{% trans %}One person{% plural a.count %}persons and {{ count }} dogs{% endtrans %}'
);
$html = $template->render(['a' => ['1', '2'], 'nbrdogs' => 3]);
$this->assertEquals('One person', $html);
$this->assertNotEmpty($html);
}

/**
* Test simple plural translation using count and vars
* @return void
*/
public function testExtensionI18nGetName(): void
{
$extension = new ExtensionI18n();
$this->assertSame('i18n', $extension->getName());
}

}

0 comments on commit 3ee642a

Please sign in to comment.