Skip to content

Commit 5ee2a4c

Browse files
authored
Merge pull request #283 from jonasraoni/feature/master/282-add-strict-po-loader
#282 Add strict po loader
2 parents 017e249 + 4af3d90 commit 5ee2a4c

15 files changed

+1229
-387
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
Previous releases are documented in [github releases](https://github.com/oscarotero/Gettext/releases)
99

10+
## [5.7.0] - 2021-07-27
11+
### Added
12+
- StrictPoLoader, a stricter PO loader more aligned with the syntax of the GNU gettext tooling [#282].
13+
- Previous attributes (msgctxt, msgid, msgid_plural) to the Translation class and the PO generator [#282].
14+
### Changed
15+
- Minor performance improvements to the Translations class [#282].
16+
1017
## [5.6.1] - 2021-12-04
1118
### Fixed
1219
- PHP 8.1 support [#278].
@@ -112,7 +119,9 @@ Previous releases are documented in [github releases](https://github.com/oscarot
112119
[#265]: https://github.com/php-gettext/Gettext/issues/265
113120
[#276]: https://github.com/php-gettext/Gettext/issues/276
114121
[#278]: https://github.com/php-gettext/Gettext/issues/278
122+
[#282]: https://github.com/php-gettext/Gettext/issues/282
115123

124+
[5.7.0]: https://github.com/php-gettext/Gettext/compare/v5.6.1...v5.7.0
116125
[5.6.1]: https://github.com/php-gettext/Gettext/compare/v5.6.0...v5.6.1
117126
[5.6.0]: https://github.com/php-gettext/Gettext/compare/v5.5.4...v5.6.0
118127
[5.5.4]: https://github.com/php-gettext/Gettext/compare/v5.5.3...v5.5.4

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ $translations->setDomain('my-blog');
9494

9595
## Loaders
9696

97-
The loaders allows to get gettext values from any format. For example, to load a .po file:
97+
The loaders allow to get gettext values from multiple formats. For example, to load a .po file:
9898

9999
```php
100100
use Gettext\Loader\PoLoader;
@@ -109,10 +109,39 @@ $string = file_get_contents('locales2/en.po');
109109
$translations = $loader->loadString($string);
110110
```
111111

112+
As of version 5.7.0, a `StrictPoLoader` has been included, with a parser more aligned to the GNU gettext tooling with the same expectations and failures (see the tests for more details).
113+
- It will fail with an exception when there's anything wrong with the syntax, and display the reason together with the line/byte where it happened.
114+
- It might also emit useful warnings, e.g. when there are more/less plural translations than needed, missing translation header, dangling comments not associated with any translation, etc.
115+
- Due to its strictness and speed (about 50% slower than the `PoLoader`), it might be interesting to be used as a kind of `.po` linter in a build system.
116+
- It also implements the previous translation comment (e.g. `#| msgid "previous"`) and extra escapes (16-bit unicode `\u`, 32-bit unicode `\U`, hexadecimal `\xFF` and octal `\77`).
117+
118+
The usage is basically the same as the `PoLoader`:
119+
120+
```php
121+
use Gettext\Loader\StrictPoLoader;
122+
123+
$loader = new StrictPoLoader();
124+
125+
//From a file
126+
$translations = $loader->loadFile('locales/en.po');
127+
128+
//From a string
129+
$string = file_get_contents('locales2/en.po');
130+
$translations = $loader->loadString($string);
131+
132+
//Display error messages using "at line X column Y" instead of "at byte X"
133+
$loader->displayErrorLine = true;
134+
//Throw an exception when a warning happens
135+
$loader->throwOnWarning = true;
136+
//Retrieve the warnings
137+
$loader->getWarnings();
138+
```
139+
112140
This package includes the following loaders:
113141

114142
- `MoLoader`
115143
- `PoLoader`
144+
- `StrictPoLoader`
116145

117146
And you can install other formats with loaders and generators:
118147

src/Generator/PoGenerator.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ public function generateString(Translations $translations): string
6565

6666
$prefix = $translation->isDisabled() ? '#~ ' : '';
6767

68+
if ($context = $translation->getPreviousContext()) {
69+
$lines[] = sprintf('%s#| msgctxt %s', $prefix, self::encode($context));
70+
}
71+
72+
if ($original = $translation->getPreviousOriginal()) {
73+
$lines[] = sprintf('%s#| msgid %s', $prefix, self::encode($original));
74+
}
75+
76+
if ($plural = $translation->getPreviousPlural()) {
77+
$lines[] = sprintf('%s#| msgid_plural %s', $prefix, self::encode($plural));
78+
}
79+
6880
if ($context = $translation->getContext()) {
6981
$lines[] = sprintf('%smsgctxt %s', $prefix, self::encode($context));
7082
}

src/Loader/MoLoader.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace Gettext\Loader;
55

66
use Exception;
7-
use Gettext\Translation;
87
use Gettext\Translations;
98

109
/**

0 commit comments

Comments
 (0)