Skip to content

Commit

Permalink
L10n: Pass initial translation file location through constructor
Browse files Browse the repository at this point in the history
Eliminates the FAO requirement and the hardcoded default location.

Reviewed at https://reviews.lunr.nl/r/1081/
  • Loading branch information
pprkut committed Jan 22, 2024
1 parent 383d1bf commit 002b19b
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 93 deletions.
30 changes: 18 additions & 12 deletions src/Lunr/L10n/AbstractL10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Lunr\L10n;

use Psr\Log\LoggerInterface;
use DirectoryIterator;

/**
* Localization support trait.
Expand All @@ -31,7 +32,13 @@ abstract class AbstractL10n
* Locales location.
* @var string
*/
protected $locales_location;
protected string $locales_location;

/**
* Locales location iterator.
* @var DirectoryIterator
*/
protected DirectoryIterator $locales_iterator;

/**
* Shared instance of a Logger class.
Expand All @@ -42,14 +49,16 @@ abstract class AbstractL10n
/**
* Constructor.
*
* @param LoggerInterface $logger Shared instance of a Logger class.
* @param LoggerInterface $logger Shared instance of a Logger class.
* @param string $locales_location Location of translation files
*/
public function __construct($logger)
public function __construct($logger, $locales_location)
{
$this->logger = $logger;

$this->default_language = 'en_US';
$this->locales_location = dirname($_SERVER['PHP_SELF']) . '/l10n';

$this->set_locales_location($locales_location);
}

/**
Expand Down Expand Up @@ -93,14 +102,11 @@ public function set_default_language($language)
*/
public function set_locales_location($location)
{
if (file_exists($location) === TRUE)
{
$this->locales_location = $location;
}
else
{
$this->logger->warning('Invalid locales location: ' . $location);
}
// This will throw if $location does not exist, isn't a directory
// or we don't have permission to access it.
$this->locales_iterator = new DirectoryIterator($location);

$this->locales_location = $location;
}

}
Expand Down
11 changes: 6 additions & 5 deletions src/Lunr/L10n/GettextL10nProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ class GettextL10nProvider extends L10nProvider
/**
* Constructor.
*
* @param string $language POSIX locale definition
* @param string $domain Localization domain
* @param LoggerInterface $logger Shared instance of a logger class
* @param string $language POSIX locale definition
* @param string $domain Localization domain
* @param LoggerInterface $logger Shared instance of a logger class
* @param string $locales_location Location of translation files
*/
public function __construct($language, $domain, $logger)
public function __construct($language, $domain, $logger, $locales_location)
{
parent::__construct($language, $domain, $logger);
parent::__construct($language, $domain, $logger, $locales_location);
}

/**
Expand Down
30 changes: 12 additions & 18 deletions src/Lunr/L10n/L10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,13 @@
namespace Lunr\L10n;

use Psr\Log\LoggerInterface;
use Lunr\Ray\FilesystemAccessObjectInterface;

/**
* Localization support class
*/
class L10n extends AbstractL10n
{

/**
* Shared instance of a FilesystemAccessObject class.
* @var FilesystemAccessObjectInterface
*/
private $fao;

/**
* Static list of supported languages
* @var array|null
Expand All @@ -35,23 +28,19 @@ class L10n extends AbstractL10n
/**
* Constructor.
*
* @param LoggerInterface $logger Shared instance of a Logger class.
* @param FilesystemAccessObjectInterface $fao Shared instance of a FilesystemAccessObject class.
* @param LoggerInterface $logger Shared instance of a Logger class.
* @param string $locales_location Location of translation files
*/
public function __construct($logger, $fao)
public function __construct($logger, $locales_location)
{
parent::__construct($logger);

$this->fao = $fao;
parent::__construct($logger, $locales_location);
}

/**
* Destructor.
*/
public function __destruct()
{
unset($this->fao);

parent::__destruct();
}

Expand All @@ -70,10 +59,15 @@ public function get_supported_languages()
return self::$languages;
}

self::$languages = $this->fao->get_list_of_directories($this->locales_location);
self::$languages = [ $this->default_language ];

self::$languages[] = $this->default_language;
self::$languages = array_unique(self::$languages);
foreach ($this->locales_iterator as $file)
{
if (!$file->isDot() && $file->isDir() && $this->locales_iterator->getFilename() != $this->default_language)
{
self::$languages[] = $this->locales_iterator->getFilename();
}
}

return self::$languages;
}
Expand Down
11 changes: 6 additions & 5 deletions src/Lunr/L10n/L10nProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ abstract class L10nProvider extends AbstractL10n
/**
* Constructor.
*
* @param string $language POSIX locale definition
* @param string $domain Localization domain
* @param LoggerInterface $logger Shared instance of a logger class
* @param string $language POSIX locale definition
* @param string $domain Localization domain
* @param LoggerInterface $logger Shared instance of a logger class
* @param string $locales_location Location of translation files
*/
public function __construct($language, $domain, $logger)
public function __construct($language, $domain, $logger, $locales_location)
{
parent::__construct($logger);
parent::__construct($logger, $locales_location);

$this->language = $language;
$this->domain = $domain;
Expand Down
11 changes: 6 additions & 5 deletions src/Lunr/L10n/PHPL10nProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ class PHPL10nProvider extends L10nProvider
/**
* Constructor.
*
* @param string $language POSIX locale definition
* @param string $domain Localization domain
* @param LoggerInterface $logger Shared instance of a logger class
* @param string $language POSIX locale definition
* @param string $domain Localization domain
* @param LoggerInterface $logger Shared instance of a logger class
* @param string $locales_location Location of translation files
*/
public function __construct($language, $domain, $logger)
public function __construct($language, $domain, $logger, $locales_location)
{
parent::__construct($language, $domain, $logger);
parent::__construct($language, $domain, $logger, $locales_location);

$this->initialized = FALSE;
$this->lang_array = [];
Expand Down
25 changes: 13 additions & 12 deletions src/Lunr/L10n/Tests/AbstractL10nBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Lunr\L10n\Tests;

use Lunr\Halo\PropertyTraits\PsrLoggerTestTrait;
use Throwable;

/**
* This class contains test methods for the L10n class.
Expand All @@ -35,10 +36,7 @@ public function testDefaultLanguageSetCorrectly(): void
*/
public function testLocaleLocationSetCorrectly(): void
{
// /usr/bin/l10n by default
$default_location = dirname($_SERVER['PHP_SELF']) . '/l10n';

$this->assertPropertyEquals('locales_location', $default_location);
$this->assertPropertyEquals('locales_location', TEST_STATICS . '/l10n/');
}

/**
Expand Down Expand Up @@ -122,18 +120,21 @@ public function testSetValidLocalesLocation(): void
*/
public function testSetInvalidLocalesLocation(): void
{
// /usr/bin/l10n by default
$default_location = dirname($_SERVER['PHP_SELF']) . '/l10n';

$location = TEST_STATICS . '/../l10n';

$this->logger->expects($this->once())
->method('warning')
->with('Invalid locales location: ' . $location);
$this->expectException('UnexpectedValueException');
$this->expectExceptionMessage('Failed to open directory');

$this->class->set_locales_location($location);
try
{
$this->class->set_locales_location($location);
}
catch (Throwable $e)
{
$this->assertEquals(TEST_STATICS . '/l10n/', $this->get_reflection_property_value('locales_location'));

$this->assertEquals($default_location, $this->get_reflection_property_value('locales_location'));
throw $e;
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/Lunr/L10n/Tests/AbstractL10nTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function setUp(): void
$this->logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();

$this->class = $this->getMockBuilder('Lunr\L10n\AbstractL10n')
->setConstructorArgs([ $this->logger ])
->setConstructorArgs([ $this->logger, TEST_STATICS . '/l10n/' ])
->getMockForAbstractClass();

parent::baseSetUp($this->class);
Expand Down
2 changes: 1 addition & 1 deletion src/Lunr/L10n/Tests/GettextL10nProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function setUp(): void
$this->base_locale = setlocale(LC_MESSAGES, 0);
$this->base_domain = textdomain(NULL);

$this->class = new GettextL10nProvider(self::LANGUAGE, self::DOMAIN, $this->logger);
$this->class = new GettextL10nProvider(self::LANGUAGE, self::DOMAIN, $this->logger, TEST_STATICS . '/l10n/');
$this->class->set_default_language('nl_NL');
$this->class->set_locales_location(TEST_STATICS . '/l10n');

Expand Down
21 changes: 2 additions & 19 deletions src/Lunr/L10n/Tests/L10nBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ public function testLanguagesEmpty(): void
$this->assertEmpty($languages);
}

/**
* Test that the FilesystemAccessObject class is passed correctly.
*/
public function testFAOIsPassedCorrectly(): void
{
$this->assertPropertySame('fao', $this->fao);
}

/**
* Test that the language is correctly stored in the object.
*/
Expand All @@ -54,10 +46,7 @@ public function testDefaultLanguageSetCorrectly(): void
*/
public function testLocaleLocationSetCorrectly(): void
{
// /usr/bin/l10n by default
$default_location = dirname($_SERVER['PHP_SELF']) . '/l10n';

$this->assertPropertyEquals('locales_location', $default_location);
$this->assertPropertyEquals('locales_location', TEST_STATICS . '/l10n/');
}

/**
Expand All @@ -68,11 +57,6 @@ public function testLocaleLocationSetCorrectly(): void
*/
public function testInitialGetSupportedLanguages(): void
{
$this->fao->expects($this->once())
->method('get_list_of_directories')
->with(dirname($_SERVER['PHP_SELF']) . '/l10n')
->willReturn([ 'de_DE', 'nl_NL' ]);

$languages = $this->class->get_supported_languages();
sort($languages);
$this->assertEquals($this->languages, $languages);
Expand All @@ -99,8 +83,7 @@ public function testLanguagesPopulated(): void
*/
public function testCachedGetSupportedLanguages(): void
{
$this->fao->expects($this->never())
->method('get_list_of_directories');
$this->set_reflection_property_value('languages', [ 'de_DE', 'en_US', 'nl_NL' ]);

$languages = $this->class->get_supported_languages();
sort($languages);
Expand Down
5 changes: 1 addition & 4 deletions src/Lunr/L10n/Tests/L10nProviderBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ public function testDefaultLanguageSetCorrectly(): void
*/
public function testLocaleLocationSetCorrectly(): void
{
// /usr/bin/l10n by default
$default_location = dirname($_SERVER['PHP_SELF']) . '/l10n';

$this->assertPropertyEquals('locales_location', $default_location);
$this->assertPropertyEquals('locales_location', TEST_STATICS . '/l10n/');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Lunr/L10n/Tests/L10nProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function setUp(): void
$this->logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();

$this->class = $this->getMockBuilder('Lunr\L10n\L10nProvider')
->setConstructorArgs([ self::LANGUAGE, self::DOMAIN, $this->logger ])
->setConstructorArgs([ self::LANGUAGE, self::DOMAIN, $this->logger, TEST_STATICS . '/l10n/' ])
->getMockForAbstractClass();

parent::baseSetUp($this->class);
Expand Down
10 changes: 1 addition & 9 deletions src/Lunr/L10n/Tests/L10nTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ abstract class L10nTest extends LunrBaseTest
*/
protected $logger;

/**
* Mock instance of a FilesystemAccessObject class.
* @var FilesystemAccessObjectInterface
*/
protected $fao;

/**
* Array of supported languages.
* @var array
Expand All @@ -52,9 +46,8 @@ abstract class L10nTest extends LunrBaseTest
public function setUp(): void
{
$this->logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$this->fao = $this->getMockBuilder('Lunr\Ray\FilesystemAccessObjectInterface')->getMock();

$this->class = new L10n($this->logger, $this->fao);
$this->class = new L10n($this->logger, TEST_STATICS . '/l10n/');

$this->languages = [ 'de_DE', 'en_US', 'nl_NL' ];

Expand All @@ -67,7 +60,6 @@ public function setUp(): void
public function tearDown(): void
{
unset($this->logger);
unset($this->fao);
unset($this->class);
unset($this->languages);

Expand Down
2 changes: 1 addition & 1 deletion src/Lunr/L10n/Tests/PHPL10nProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function setUp(): void
{
$this->logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();

$this->class = new PHPL10nProvider(self::LANGUAGE, self::DOMAIN, $this->logger);
$this->class = new PHPL10nProvider(self::LANGUAGE, self::DOMAIN, $this->logger, TEST_STATICS . '/l10n/');
$this->class->set_locales_location(TEST_STATICS . '/l10n');

parent::baseSetUp($this->class);
Expand Down
3 changes: 3 additions & 0 deletions tests/statics/l10n/de_DE/LC_MESSAGES/Lunr.po
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
msgid ""
msgstr "Content-Type: text/plain; charset=UTF-8"

msgid "table"
msgstr "Tisch"

Expand Down

0 comments on commit 002b19b

Please sign in to comment.