Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#282 Add strict po loader #283

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
fbd97d9
#282 Added partial support for the previous translation comment
jonasraoni Jul 10, 2022
7320117
#282 Added strict PO loader
jonasraoni Jul 10, 2022
8439ddb
#282 Added comments
jonasraoni Jul 20, 2022
faeef90
#282 Renamed identifiers
jonasraoni Jul 20, 2022
ef12806
#282 Added support for different line breaks
jonasraoni Jul 22, 2022
d656353
#282 Added support for extra escaping sequences
jonasraoni Jul 22, 2022
647cdfc
#282 Formatting
jonasraoni Jul 22, 2022
05bf1b4
#282 Removed non-used snapshot
jonasraoni Jul 22, 2022
0a4c6e4
#282 Fixed tests
jonasraoni Jul 22, 2022
5c4dfd3
#282 Removed non used imports
jonasraoni Jul 22, 2022
2c244a9
#282 Added checks for invalid newlines and duplicated entries
jonasraoni Jul 23, 2022
c3289bd
#282 Fixes and extra checks/warnings (missing header, incomplete plur…
jonasraoni Jul 23, 2022
7981363
#282 Updated and added tests
jonasraoni Jul 23, 2022
3347517
#282 Added test for the previous translation
jonasraoni Jul 23, 2022
d70bb1b
#282 Added warning for dangling comments in the end of the file, adde…
jonasraoni Jul 24, 2022
10ca616
#282 Updated/added tests
jonasraoni Jul 24, 2022
1830ea4
#282 Formatting
jonasraoni Jul 24, 2022
cea017b
#282 Minor updates to the Translations class
jonasraoni Jul 26, 2022
8700676
#282 Updated StrictPoLoader to use more native functions
jonasraoni Jul 26, 2022
8d1b18f
#282 Added public properties throwOnWarning and displayErrorLine (swi…
jonasraoni Jul 27, 2022
4af3d90
#282 Updated README and CHANGELOG
jonasraoni Jul 27, 2022
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

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

## [5.7.0] - 2021-07-27
### Added
- StrictPoLoader, a stricter PO loader more aligned with the syntax of the GNU gettext tooling [#282].
- Previous attributes (msgctxt, msgid, msgid_plural) to the Translation class and the PO generator [#282].
### Changed
- Minor performance improvements to the Translations class [#282].

## [5.6.1] - 2021-12-04
### Fixed
- PHP 8.1 support [#278].
Expand Down Expand Up @@ -112,7 +119,9 @@ Previous releases are documented in [github releases](https://github.com/oscarot
[#265]: https://github.com/php-gettext/Gettext/issues/265
[#276]: https://github.com/php-gettext/Gettext/issues/276
[#278]: https://github.com/php-gettext/Gettext/issues/278
[#282]: https://github.com/php-gettext/Gettext/issues/282

[5.7.0]: https://github.com/php-gettext/Gettext/compare/v5.6.1...v5.7.0
[5.6.1]: https://github.com/php-gettext/Gettext/compare/v5.6.0...v5.6.1
[5.6.0]: https://github.com/php-gettext/Gettext/compare/v5.5.4...v5.6.0
[5.5.4]: https://github.com/php-gettext/Gettext/compare/v5.5.3...v5.5.4
Expand Down
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ $translations->setDomain('my-blog');

## Loaders

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

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

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).
- 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.
- 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.
- 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.
- 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`).

The usage is basically the same as the `PoLoader`:

```php
use Gettext\Loader\StrictPoLoader;

$loader = new StrictPoLoader();

//From a file
$translations = $loader->loadFile('locales/en.po');

//From a string
$string = file_get_contents('locales2/en.po');
$translations = $loader->loadString($string);

//Display error messages using "at line X column Y" instead of "at byte X"
$loader->displayErrorLine = true;
//Throw an exception when a warning happens
$loader->throwOnWarning = true;
//Retrieve the warnings
$loader->getWarnings();
```

This package includes the following loaders:

- `MoLoader`
- `PoLoader`
- `StrictPoLoader`

And you can install other formats with loaders and generators:

Expand Down
12 changes: 12 additions & 0 deletions src/Generator/PoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ public function generateString(Translations $translations): string

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

if ($context = $translation->getPreviousContext()) {
$lines[] = sprintf('%s#| msgctxt %s', $prefix, self::encode($context));
}

if ($original = $translation->getPreviousOriginal()) {
$lines[] = sprintf('%s#| msgid %s', $prefix, self::encode($original));
}

if ($plural = $translation->getPreviousPlural()) {
$lines[] = sprintf('%s#| msgid_plural %s', $prefix, self::encode($plural));
}

if ($context = $translation->getContext()) {
$lines[] = sprintf('%smsgctxt %s', $prefix, self::encode($context));
}
Expand Down
1 change: 0 additions & 1 deletion src/Loader/MoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Gettext\Loader;

use Exception;
use Gettext\Translation;
use Gettext\Translations;

/**
Expand Down
Loading