Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to **initphp/translator** will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- On POSIX, `useDirectory()->setDir()` with a directory whose path contains a
backslash (for example `…/b\y`) now reads that directory. The backslash was
treated as a glob escape character, so no language file was found.

## [2.0.1] - 2026-11-25

### Security
Expand Down
10 changes: 9 additions & 1 deletion src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,15 @@ private function assertValidLanguageCode(string $lang): void
*/
private function escapeGlob(string $path): string
{
return strtr($path, ['*' => '[*]', '?' => '[?]', '[' => '[[]']);
$map = ['*' => '[*]', '?' => '[?]', '[' => '[[]'];
// On POSIX a backslash escapes the next character in a glob pattern, so
// a literal one in the directory name has to be escaped too. On Windows
// it is the directory separator and must stay as it is.
if (DIRECTORY_SEPARATOR === '/') {
$map['\\'] = '\\\\';
}

return strtr($path, $map);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/LanguageCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,21 @@ public function testALiteralDirectoryNamedLikeAWildcardDoesNotReachTheParent(str
self::assertSame('outside.hello', $lang->translate('outside.hello'));
}

public function testADirectoryContainingABackslashIsReadOnPosix(): void
{
if (\DIRECTORY_SEPARATOR !== '/') {
self::markTestSkipped('A backslash is a path separator on Windows.');
}

$dir = $this->base . '/b\\y';
mkdir($dir . '/en', 0o777, true);
file_put_contents($dir . '/en/main.php', '<?php return [\'hello\' => \'Backslash\'];');

$lang = (new Translator())->useDirectory()->setDir($dir)->setDefault('en');

self::assertSame('Backslash', $lang->translate('main.hello'));
}

public function testAColonIsRejectedWhereItIsNotAValidFileNameCharacter(): void
{
if (\DIRECTORY_SEPARATOR !== '\\') {
Expand Down
Loading