pax_global_header 0000666 0000000 0000000 00000000064 14765446412 0014527 g ustar 00root root 0000000 0000000 52 comment=6aca7c750ce27328c56b8d66f9883e9ec7357a0e
motranslator-5.4.0/ 0000775 0000000 0000000 00000000000 14765446412 0014262 5 ustar 00root root 0000000 0000000 motranslator-5.4.0/CHANGELOG.md 0000664 0000000 0000000 00000004756 14765446412 0016107 0 ustar 00root root 0000000 0000000 # Change Log
## [5.4.0] - 2025-03-16
* Bump PHP minimum version to 7.2
* Add support for Symfony 7
## [5.3.1] - 2023-08-23
* Add function guards to the global functions (#44)
## [5.3.0] - 2022-04-26
* Add support for Symfony 6
* Split out `.mo` parsing to separate `MoParser` class
* Added `CacheInterface` so alternate cache implementations are pluggable
* Added `ApcuCache` implementation to leverage shared in-memory translation cache
## [5.2.0] - 2021-02-05
* Fix "Translator::selectString() must be of the type integer, boolean returned" (#37)
* Fix "TypeError: StringReader::readintarray() ($count) must be of type int, float given" failing tests on ARM (#36)
* Add support for getting and setting all translations (#30)
## [5.1.0] - 2020-11-15
* Allow PHPUnit 9 (#35)
* Fix some typos
* Sync config files
* Allow PHP 8.0
## [5.0.0] - 2020-02-28
* Drop support for PHP 5.3, PHP 5.4, PHP 5.5, PHP 5.6, PHP 7.0 and HHVM
* Enabled strict mode on PHP files
* Add support for Symfony 5 (#34)
* Add support for phpunit 8
* Rename CHANGES.md to CHANGELOG.md and follow a standard format
## [4.0] - 2018-02-12
* The library no longer changes system locales.
## [3.4] - 2017-12-15
* Added Translator::setTranslation method.
## [3.3] - 2017-06-01
* Add support for switching locales for Loader instance.
## [3.2] - 2017-05-23
* Various fixes when handling corrupted mo files.
## [3.1] - 2017-05-15
* Documentation improvements.
## [3.0] - 2017-01-23
* All classes moved to the PhpMyAdmin namespace.
## [2.2] - 2017-01-07
* Coding style cleanup.
* Avoid installing tests using composer.
## [2.1] - 2016-12-21
* Various code cleanups.
* Added support for PHP 5.3.
## [2.0] - 2016-10-13
* Consistently use camelCase in API.
* No more relies on using eval().
* Depends on symfony/expression-language for calculations.
## [1.2] - 2016-09-22
* Stricter validation of plural expression.
## [1.1] - 2016-08-29
* Improved handling of corrupted mo files.
* Minor performance improvements.
* Stricter validation of plural expression.
## [1.0] - 2016-04-27
* Documentation improvements.
* Testsuite improvements.
## [0.4] - 2016-03-02
* Fixed test failures with hhvm due to broken putenv.
## [0.3] - 2016-03-01
* Added Loader::detectlocale method.
## [0.2] - 2016-02-24
* Marked PHP 5.4 and 5.5 as supported.
## [0.1] - 2016-02-23
* Initial release.
[5.4.0]: https://github.com/phpmyadmin/motranslator/compare/5.3.1...5.4.0
[5.3.1]: https://github.com/phpmyadmin/motranslator/compare/5.3.0...5.3.1
motranslator-5.4.0/CODE_OF_CONDUCT.md 0000664 0000000 0000000 00000006427 14765446412 0017072 0 ustar 00root root 0000000 0000000 # Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, sex characteristics, gender identity and expression,
level of experience, education, socio-economic status, nationality, personal
appearance, race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment
include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at info@phpmyadmin.net. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
[homepage]: https://www.contributor-covenant.org
For answers to common questions about this code of conduct, see
https://www.contributor-covenant.org/faq
motranslator-5.4.0/CONTRIBUTING.md 0000664 0000000 0000000 00000001536 14765446412 0016520 0 ustar 00root root 0000000 0000000 # Contributing to motranslator
## Reporting issues
Our issue tracker is hosted at GitHub:
https://github.com/phpmyadmin/motranslator/issues
Please search for existing issues before reporting new ones.
## Working with Git checkout
The dependencies are managed by Composer, to get them all installed (or update
on consequent runs) do:
```
composer update
```
## Submitting patches
Please submit your patches using GitHub pull requests, this allows us to review
them and to run automated tests on the code.
## Coding standards
We do follow PSR-1 and PSR-2 coding standards.
You can use phpcbf to fix the code to match our expectations:
```
composer run phpcbf
```
## Testsuite
Our code comes with quite comprehensive testsuite, it is automatically executed
on every commit and pull request, you can also run it locally:
```
composer run phpunit
```
motranslator-5.4.0/LICENSE 0000664 0000000 0000000 00000043176 14765446412 0015302 0 ustar 00root root 0000000 0000000 GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
{description}
Copyright (C) {year} {fullname}
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
{signature of Ty Coon}, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.
motranslator-5.4.0/PERFORMANCE.md 0000664 0000000 0000000 00000001755 14765446412 0016355 0 ustar 00root root 0000000 0000000 # Performance
This library was tweaked for best performance for single use - translating
application with many strings using mo file. Current benchmarks show it's about
four times faster than original php-gettext.
There are two benchmark scripts in the code:
* ``benchmark-context.php`` - benchmarks context usage
* ``benchmark-plural.php`` - benchmarks plural evaluation
* ``benchmark.php`` - benchmarks file parsing
* ``benchmark-apcu.php`` - benchmarks file parsing with APCu cache enabled
## Performance measurements
The performance improvements based on individual changes in the code:
| Stage | Seconds |
| -------------- | --------------- |
| Original code | 4.7929680347443 |
| Remove nocache | 4.6308250427246 |
| Direct endian | 4.5883052349091 |
| Remove attribs | 4.5297479629517 |
| String reader | 1.8148958683014 |
| No offset | 1.2436759471893 |
| Less attribs | 1.1722540855408 |
| Remove shift | 1.0970499515533 |
| Magic order | 1.0868430137634 |
motranslator-5.4.0/README.md 0000664 0000000 0000000 00000020353 14765446412 0015544 0 ustar 00root root 0000000 0000000 # motranslator
Translation API for PHP using Gettext MO files.

[](https://codecov.io/github/phpmyadmin/motranslator?branch=master)
[](https://scrutinizer-ci.com/g/phpmyadmin/motranslator/?branch=master)
[](https://packagist.org/packages/phpmyadmin/motranslator)
## Features
* All strings are stored in memory for fast lookup
* Fast loading of MO files
* Low level API for reading MO files
* Emulation of Gettext API
* No use of `eval()` for plural equation
## Limitations
* Default `InMemoryCache` not suitable for huge MO files which you don't want to store in memory
* Input and output encoding has to match (preferably UTF-8)
## Installation
Please use [Composer][1] to install:
```sh
composer require phpmyadmin/motranslator
```
## Documentation
The API documentation is available at .
## Object API usage
```php
// Create loader object
$loader = new PhpMyAdmin\MoTranslator\Loader();
// Set locale
$loader->setlocale('cs');
// Set default text domain
$loader->textdomain('domain');
// Set path where to look for a domain
$loader->bindtextdomain('domain', __DIR__ . '/data/locale/');
// Get translator
$translator = $loader->getTranslator();
// Now you can use Translator API (see below)
```
## Low level API usage
```php
// Directly load the mo file
// You can use null to not load a file and the use a setter to set the translations
$cache = new PhpMyAdmin\MoTranslator\Cache\InMemoryCache(new PhpMyAdmin\MoTranslator\MoParser('./path/to/file.mo'));
$translator = new PhpMyAdmin\MoTranslator\Translator($cache);
// Now you can use Translator API (see below)
```
## Translator API usage
```php
// Translate string
echo $translator->gettext('String');
// Translate plural string
echo $translator->ngettext('String', 'Plural string', $count);
// Translate string with context
echo $translator->pgettext('Context', 'String');
// Translate plural string with context
echo $translator->npgettext('Context', 'String', 'Plural string', $count);
// Get the translations
echo $translator->getTranslations();
// All getters and setters below are more to be used if you are using a manual loading mode
// Example: $translator = new PhpMyAdmin\MoTranslator\Translator(null);
// Set a translation
echo $translator->setTranslation('Test', 'Translation for "Test" key');
// Set translations
echo $translator->setTranslations([
'Test' => 'Translation for "Test" key',
'Test 2' => 'Translation for "Test 2" key',
]);
// Use the translation
echo $translator->gettext('Test 2'); // -> Translation for "Test 2" key
```
## Gettext compatibility usage
```php
// Load compatibility layer
PhpMyAdmin\MoTranslator\Loader::loadFunctions();
// Configure
_setlocale(LC_MESSAGES, 'cs');
_textdomain('phpmyadmin');
_bindtextdomain('phpmyadmin', __DIR__ . '/data/locale/');
_bind_textdomain_codeset('phpmyadmin', 'UTF-8');
// Use functions
echo _gettext('Type');
echo __('Type');
// It also support other Gettext functions
_dnpgettext($domain, $msgctxt, $msgid, $msgidPlural, $number);
_dngettext($domain, $msgid, $msgidPlural, $number);
_npgettext($msgctxt, $msgid, $msgidPlural, $number);
_ngettext($msgid, $msgidPlural, $number);
_dpgettext($domain, $msgctxt, $msgid);
_dgettext($domain, $msgid);
_pgettext($msgctxt, $msgid);
```
## Using APCu-backed cache
If you have the [APCu][5] extension installed you can use it for storing the translation cache. The `.mo` file
will then only be loaded once and all processes will share the same cache, reducing memory usage and resulting in
performance comparable to the native `gettext` extension.
If you are using `Loader`, pass it an `ApcuCacheFactory` _before_ getting the translator instance:
```php
PhpMyAdmin\MoTranslator\Loader::setCacheFactory(
new PhpMyAdmin\MoTranslator\Cache\AcpuCacheFactory()
);
$loader = new PhpMyAdmin\MoTranslator\Loader();
// Proceed as before
```
If you are using the low level API, instantiate the `ApcuCache` directly:
```php
$cache = new PhpMyAdmin\MoTranslator\Cache\ApcuCache(
new PhpMyAdmin\MoTranslator\MoParser('./path/to/file.mo'),
'de_DE', // the locale
'phpmyadmin' // the domain
);
$translator = new PhpMyAdmin\MoTranslator\Translator($cache);
// Proceed as before
```
By default, APCu will cache the translations until next server restart and prefix the cache entries with `mo_` to
avoid clashes with other cache entries. You can control this behaviour by passing `$ttl` and `$prefix` arguments, either
to the `ApcuCacheFactory` or when instantiating `ApcuCache`:
```php
PhpMyAdmin\MoTranslator\Loader::setCacheFactory(
new PhpMyAdmin\MoTranslator\Cache\AcpuCacheFactory(
3600, // cache for 1 hour
true, // reload on cache miss
'custom_' // custom prefix for cache entries
)
);
$loader = new PhpMyAdmin\MoTranslator\Loader();
// or...
$cache = new PhpMyAdmin\MoTranslator\Cache\ApcuCache(
new PhpMyAdmin\MoTranslator\MoParser('./path/to/file.mo'),
'de_DE',
'phpmyadmin',
3600, // cache for 1 hour
true, // reload on cache miss
'custom_' // custom prefix for cache entries
);
$translator = new PhpMyAdmin\MoTranslator\Translator($cache);
```
If you receive updated translation files you can load them without restarting the server using the low-level API:
```php
$parser = new PhpMyAdmin\MoTranslator\MoParser('./path/to/file.mo');
$cache = new PhpMyAdmin\MoTranslator\Cache\ApcuCache($parser, 'de_DE', 'phpmyadmin');
$parser->parseIntoCache($cache);
```
You should ensure APCu has enough memory to store all your translations, along with any other entries you use it
for. If an entry is evicted from cache, the `.mo` file will be re-parsed, impacting performance. See the
`apc.shm_size` and `apc.shm_segments` [documentation][6] and monitor cache usage when first rolling out.
If your `.mo` files are missing lots of translations, the first time a missing entry is requested the `.mo` file
will be re-parsed. Again, this will impact performance until all the missing entries are hit once. You can turn off this
behaviour by setting the `$reloadOnMiss` argument to `false`. If you do this it is _critical_ that APCu has enough
memory, or users will see untranslated text when entries are evicted.
## History
This library is based on [php-gettext][2]. It adds some performance
improvements and ability to install using [Composer][1].
## Motivation
Motivation for this library includes:
* The [php-gettext][2] library is not maintained anymore
* It doesn't work with recent PHP version (phpMyAdmin has patched version)
* It relies on `eval()` function for plural equations what can have severe security implications, see [CVE-2016-6175][4]
* It's not possible to install it using [Composer][1]
* There was place for performance improvements in the library
### Why not to use native gettext in PHP?
We've tried that, but it's not a viable solution:
* You can not use locales not known to system, what is something you can not
control from web application. This gets even more tricky with minimalist
virtualisation containers.
* Changing the MO file usually leads to PHP segmentation fault. It (or rather
Gettext library) caches headers of MO file and if it's content is changed
(for example new version is uploaded to server) it tries to access new data
with old references. This is bug known for ages:
https://bugs.php.net/bug.php?id=45943
### Why use Gettext and not JSON, YAML or whatever?
We want translators to be able to use their favorite tools and we want us to be
able to use wide range of tools available with Gettext as well such as
[web based translation using Weblate][3]. Using custom format usually adds
another barrier for translators and we want to make it easy for them to
contribute.
[1]:https://getcomposer.org/
[2]:https://launchpad.net/php-gettext
[3]:https://weblate.org/
[4]: https://www.cve.org/CVERecord?id=CVE-2016-6175
[5]:https://www.php.net/manual/en/book.apcu.php
[6]:https://www.php.net/manual/en/apcu.configuration.php
motranslator-5.4.0/benchmark-apcu.php 0000664 0000000 0000000 00000001200 14765446412 0017644 0 ustar 00root root 0000000 0000000 './tests/data/big.mo',
'little' => './tests/data/little.mo',
];
$start = microtime(true);
for ($i = 0; $i < 2000; ++$i) {
foreach ($files as $domain => $filename) {
$translator = new PhpMyAdmin\MoTranslator\Translator(
new PhpMyAdmin\MoTranslator\Cache\ApcuCache(
new PhpMyAdmin\MoTranslator\MoParser($filename), 'foo', $domain
)
);
$translator->gettext('Column');
}
}
$end = microtime(true);
$diff = $end - $start;
echo 'Execution took ' . $diff . ' seconds' . "\n";
motranslator-5.4.0/composer.json 0000664 0000000 0000000 00000003500 14765446412 0017002 0 ustar 00root root 0000000 0000000 {
"name": "phpmyadmin/motranslator",
"description": "Translation API for PHP using Gettext MO files",
"license": "GPL-2.0-or-later",
"keywords": ["gettext", "mo", "translator", "i18n"],
"homepage": "https://github.com/phpmyadmin/motranslator",
"authors": [
{
"name": "The phpMyAdmin Team",
"email": "developers@phpmyadmin.net",
"homepage": "https://www.phpmyadmin.net/team/"
}
],
"support": {
"issues": "https://github.com/phpmyadmin/motranslator/issues",
"source": "https://github.com/phpmyadmin/motranslator"
},
"scripts": {
"phpcbf": "@php phpcbf",
"phpcs": "@php phpcs",
"phpstan": "@php phpstan",
"phpunit": "@php phpunit",
"test": [
"@phpcs",
"@phpstan",
"@phpunit"
]
},
"require": {
"php": "^7.2 || ^8.0",
"symfony/expression-language": "^5.0 || ^6.0 || ^7.0"
},
"require-dev": {
"phpmyadmin/coding-standard": "^3.0.0",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan": "^1.12",
"phpstan/phpstan-deprecation-rules": "^1.2",
"phpstan/phpstan-phpunit": "^1.4",
"phpstan/phpstan-strict-rules": "^1.6",
"phpunit/phpunit": "^8.5 || ^9.6 || ^10.5"
},
"autoload": {
"psr-4": {
"PhpMyAdmin\\MoTranslator\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"PhpMyAdmin\\MoTranslator\\Tests\\": "tests"
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
}
},
"suggest": {
"ext-apcu": "Needed for ACPu-backed translation cache"
}
}
motranslator-5.4.0/phpunit.xml.dist 0000664 0000000 0000000 00000002017 14765446412 0017435 0 ustar 00root root 0000000 0000000
./tests
src/
motranslator-5.4.0/src/ 0000775 0000000 0000000 00000000000 14765446412 0015051 5 ustar 00root root 0000000 0000000 motranslator-5.4.0/src/Cache/ 0000775 0000000 0000000 00000000000 14765446412 0016054 5 ustar 00root root 0000000 0000000 motranslator-5.4.0/src/Cache/ApcuCache.php 0000664 0000000 0000000 00000007343 14765446412 0020410 0 ustar 00root root 0000000 0000000 parser = $parser;
$this->locale = $locale;
$this->domain = $domain;
$this->ttl = $ttl;
$this->reloadOnMiss = $reloadOnMiss;
$this->prefix = $prefix;
$this->ensureTranslationsLoaded();
}
public function get(string $msgid): string
{
$msgstr = apcu_fetch($this->getKey($msgid), $success);
if ($success && is_string($msgstr)) {
return $msgstr;
}
if (! $this->reloadOnMiss) {
return $msgid;
}
return $this->reloadOnMiss($msgid);
}
private function reloadOnMiss(string $msgid): string
{
// store original if translation is not present
$cached = apcu_entry($this->getKey($msgid), static function () use ($msgid) {
return $msgid;
}, $this->ttl);
// if another process has updated cache, return early
if ($cached !== $msgid && is_string($cached)) {
return $cached;
}
// reload .mo file, in case entry has been evicted
$this->parser->parseIntoCache($this);
$msgstr = apcu_fetch($this->getKey($msgid), $success);
return $success && is_string($msgstr) ? $msgstr : $msgid;
}
public function set(string $msgid, string $msgstr): void
{
apcu_store($this->getKey($msgid), $msgstr, $this->ttl);
}
public function has(string $msgid): bool
{
return apcu_exists($this->getKey($msgid));
}
public function setAll(array $translations): void
{
$keys = array_map(function (string $msgid): string {
return $this->getKey($msgid);
}, array_keys($translations));
$translations = array_combine($keys, $translations);
assert(is_array($translations));
apcu_store($translations, null, $this->ttl);
}
private function getKey(string $msgid): string
{
return $this->prefix . $this->locale . '.' . $this->domain . '.' . $msgid;
}
private function ensureTranslationsLoaded(): void
{
// Try to prevent cache slam if multiple processes are trying to load translations. There is still a race
// between the exists check and creating the entry, but at least it's small
$key = $this->getKey(self::LOADED_KEY);
$loaded = apcu_exists($key) || apcu_entry($key, static function (): int {
return 0;
}, $this->ttl);
if ($loaded) {
return;
}
$this->parser->parseIntoCache($this);
apcu_store($this->getKey(self::LOADED_KEY), 1, $this->ttl);
}
}
motranslator-5.4.0/src/Cache/ApcuCacheFactory.php 0000664 0000000 0000000 00000001345 14765446412 0021734 0 ustar 00root root 0000000 0000000 ttl = $ttl;
$this->reloadOnMiss = $reloadOnMiss;
$this->prefix = $prefix;
}
public function getInstance(MoParser $parser, string $locale, string $domain): CacheInterface
{
return new ApcuCache($parser, $locale, $domain, $this->ttl, $this->reloadOnMiss, $this->prefix);
}
}
motranslator-5.4.0/src/Cache/CacheFactoryInterface.php 0000664 0000000 0000000 00000000371 14765446412 0022742 0 ustar 00root root 0000000 0000000 $msgstr` entries
*
* This will overwrite existing values for `$msgid`, but is not guaranteed to clear cache of existing entries
* not present in `$translations`.
*
* @param array $translations
*/
public function setAll(array $translations): void;
}
motranslator-5.4.0/src/Cache/GetAllInterface.php 0000664 0000000 0000000 00000000303 14765446412 0021552 0 ustar 00root root 0000000 0000000
*/
public function getAll(): array;
}
motranslator-5.4.0/src/Cache/InMemoryCache.php 0000664 0000000 0000000 00000001747 14765446412 0021261 0 ustar 00root root 0000000 0000000 */
private $cache;
public function __construct(MoParser $parser)
{
$this->cache = [];
$parser->parseIntoCache($this);
}
public function get(string $msgid): string
{
return array_key_exists($msgid, $this->cache) ? $this->cache[$msgid] : $msgid;
}
public function set(string $msgid, string $msgstr): void
{
$this->cache[$msgid] = $msgstr;
}
public function has(string $msgid): bool
{
return array_key_exists($msgid, $this->cache);
}
public function setAll(array $translations): void
{
$this->cache = $translations;
}
/**
* @inheritDoc
*/
public function getAll(): array
{
return $this->cache;
}
}
motranslator-5.4.0/src/CacheException.php 0000664 0000000 0000000 00000000222 14765446412 0020440 0 ustar 00root root 0000000 0000000
Copyright (c) 2009 Danilo Segan
Copyright (c) 2016 Michal Čihař
This file is part of MoTranslator.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
namespace PhpMyAdmin\MoTranslator;
use PhpMyAdmin\MoTranslator\Cache\CacheFactoryInterface;
use PhpMyAdmin\MoTranslator\Cache\InMemoryCache;
use function array_push;
use function file_exists;
use function getenv;
use function in_array;
use function preg_match;
use function sprintf;
class Loader
{
/**
* Loader instance.
*
* @static
* @var Loader
*/
private static $instance = null;
/**
* Factory to return a factory responsible for returning a `CacheInterface`
*
* @static
* @var CacheFactoryInterface|null
*/
private static $cacheFactory = null;
/**
* Default gettext domain to use.
*
* @var string
*/
private $defaultDomain = '';
/**
* Configured locale.
*
* @var string
*/
private $locale = '';
/**
* Loaded domains.
*
* @var array>
*/
private $domains = [];
/**
* Bound paths for domains.
*
* @var array
*/
private $paths = ['' => './'];
/**
* Returns the singleton Loader object.
*
* @return Loader object
*/
public static function getInstance(): Loader
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Loads global localization functions.
*/
public static function loadFunctions(): void
{
require_once __DIR__ . '/functions.php';
}
/**
* Figure out all possible locale names and start with the most
* specific ones. I.e. for sr_CS.UTF-8@latin, look through all of
* sr_CS.UTF-8@latin, sr_CS@latin, sr@latin, sr_CS.UTF-8, sr_CS, sr.
*
* @param string $locale Locale code
*
* @return string[] list of locales to try for any POSIX-style locale specification
*/
public static function listLocales(string $locale): array
{
$localeNames = [];
if ($locale) {
if (
preg_match(
'/^(?P[a-z]{2,3})' // language code
. '(?:_(?P[A-Z]{2}))?' // country code
. '(?:\\.(?P[-A-Za-z0-9_]+))?' // charset
. '(?:@(?P[-A-Za-z0-9_]+))?$/', // @ modifier
$locale,
$matches
)
) {
$lang = $matches['lang'] ?? null;
$country = $matches['country'] ?? null;
$charset = $matches['charset'] ?? null;
$modifier = $matches['modifier'] ?? null;
if ($modifier) {
if ($country) {
if ($charset) {
array_push(
$localeNames,
sprintf('%s_%s.%s@%s', $lang, $country, $charset, $modifier)
);
}
array_push(
$localeNames,
sprintf('%s_%s@%s', $lang, $country, $modifier)
);
} elseif ($charset) {
array_push(
$localeNames,
sprintf('%s.%s@%s', $lang, $charset, $modifier)
);
}
array_push(
$localeNames,
sprintf('%s@%s', $lang, $modifier)
);
}
if ($country) {
if ($charset) {
array_push(
$localeNames,
sprintf('%s_%s.%s', $lang, $country, $charset)
);
}
array_push(
$localeNames,
sprintf('%s_%s', $lang, $country)
);
} elseif ($charset) {
array_push(
$localeNames,
sprintf('%s.%s', $lang, $charset)
);
}
array_push($localeNames, $lang);
}
// If the locale name doesn't match POSIX style, just include it as-is.
if (! in_array($locale, $localeNames)) {
array_push($localeNames, $locale);
}
}
return $localeNames;
}
/**
* Sets factory responsible for composing a `CacheInterface`
*/
public static function setCacheFactory(?CacheFactoryInterface $cacheFactory): void
{
self::$cacheFactory = $cacheFactory;
}
/**
* Returns Translator object for domain or for default domain.
*
* @param string $domain Translation domain
*/
public function getTranslator(string $domain = ''): Translator
{
if (empty($domain)) {
$domain = $this->defaultDomain;
}
if (! isset($this->domains[$this->locale])) {
$this->domains[$this->locale] = [];
}
if (! isset($this->domains[$this->locale][$domain])) {
if (isset($this->paths[$domain])) {
$base = $this->paths[$domain];
} else {
$base = './';
}
$localeNames = $this->listLocales($this->locale);
$filename = '';
foreach ($localeNames as $locale) {
$filename = $base . '/' . $locale . '/LC_MESSAGES/' . $domain . '.mo';
if (file_exists($filename)) {
break;
}
}
// We don't care about invalid path, we will get fallback
// translator here
$moParser = new MoParser($filename);
if (self::$cacheFactory instanceof CacheFactoryInterface) {
$cache = self::$cacheFactory->getInstance($moParser, $this->locale, $domain);
} else {
$cache = new InMemoryCache($moParser);
}
$this->domains[$this->locale][$domain] = new Translator($cache);
}
return $this->domains[$this->locale][$domain];
}
/**
* Sets the path for a domain.
*
* @param string $domain Domain name
* @param string $path Path where to find locales
*/
public function bindtextdomain(string $domain, string $path): void
{
$this->paths[$domain] = $path;
}
/**
* Sets the default domain.
*
* @param string $domain Domain name
*/
public function textdomain(string $domain): void
{
$this->defaultDomain = $domain;
}
/**
* Sets a requested locale.
*
* @param string $locale Locale name
*
* @return string Set or current locale
*/
public function setlocale(string $locale): string
{
if (! empty($locale)) {
$this->locale = $locale;
}
return $this->locale;
}
/**
* Detects currently configured locale.
*
* It checks:
*
* - global lang variable
* - environment for LC_ALL, LC_MESSAGES and LANG
*
* @return string with locale name
*/
public function detectlocale(): string
{
if (isset($GLOBALS['lang'])) {
return $GLOBALS['lang'];
}
$locale = getenv('LC_ALL');
if ($locale !== false) {
return $locale;
}
$locale = getenv('LC_MESSAGES');
if ($locale !== false) {
return $locale;
}
$locale = getenv('LANG');
if ($locale !== false) {
return $locale;
}
return 'en';
}
}
motranslator-5.4.0/src/MoParser.php 0000664 0000000 0000000 00000005533 14765446412 0017320 0 ustar 00root root 0000000 0000000 filename = $filename;
}
/**
* Parses .mo file and stores results to `$cache`
*/
public function parseIntoCache(CacheInterface $cache): void
{
if ($this->filename === null) {
return;
}
if (! is_readable($this->filename)) {
$this->error = self::ERROR_DOES_NOT_EXIST;
return;
}
$stream = new StringReader($this->filename);
try {
$magic = $stream->read(0, 4);
if (strcmp($magic, self::MAGIC_LE) === 0) {
$unpack = 'V';
} elseif (strcmp($magic, self::MAGIC_BE) === 0) {
$unpack = 'N';
} else {
$this->error = self::ERROR_BAD_MAGIC;
return;
}
/* Parse header */
$total = $stream->readint($unpack, 8);
$originals = $stream->readint($unpack, 12);
$translations = $stream->readint($unpack, 16);
/* get original and translations tables */
$totalTimesTwo = (int) ($total * 2);// Fix for issue #36 on ARM
$tableOriginals = $stream->readintarray($unpack, $originals, $totalTimesTwo);
$tableTranslations = $stream->readintarray($unpack, $translations, $totalTimesTwo);
/* read all strings to the cache */
for ($i = 0; $i < $total; ++$i) {
$iTimesTwo = $i * 2;
$iPlusOne = $iTimesTwo + 1;
$iPlusTwo = $iTimesTwo + 2;
$original = $stream->read($tableOriginals[$iPlusTwo], $tableOriginals[$iPlusOne]);
$translation = $stream->read($tableTranslations[$iPlusTwo], $tableTranslations[$iPlusOne]);
$cache->set($original, $translation);
}
} catch (ReaderException $e) {
$this->error = self::ERROR_READING;
return;
}
}
}
motranslator-5.4.0/src/ReaderException.php 0000664 0000000 0000000 00000002077 14765446412 0020651 0 ustar 00root root 0000000 0000000 .
Copyright (c) 2016 Michal Čihař
This file is part of MoTranslator.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
namespace PhpMyAdmin\MoTranslator;
use Exception;
/**
* Exception thrown when file can not be read.
*/
class ReaderException extends Exception
{
}
motranslator-5.4.0/src/StringReader.php 0000664 0000000 0000000 00000006234 14765446412 0020160 0 ustar 00root root 0000000 0000000 .
Copyright (c) 2016 Michal Čihař
This file is part of MoTranslator.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
namespace PhpMyAdmin\MoTranslator;
use function file_get_contents;
use function strlen;
use function substr;
use function unpack;
use const PHP_INT_MAX;
/**
* Simple wrapper around string buffer for
* random access and values parsing.
*/
class StringReader
{
/** @var string */
private $string;
/** @var int */
private $length;
/**
* @param string $filename Name of file to load
*/
public function __construct(string $filename)
{
$this->string = (string) file_get_contents($filename);
$this->length = strlen($this->string);
}
/**
* Read number of bytes from given offset.
*
* @param int $pos Offset
* @param int $bytes Number of bytes to read
*/
public function read(int $pos, int $bytes): string
{
if ($pos + $bytes > $this->length) {
throw new ReaderException('Not enough bytes!');
}
$data = substr($this->string, $pos, $bytes);
return $data === false ? '' : $data;
}
/**
* Reads a 32bit integer from the stream.
*
* @param string $unpack Unpack string
* @param int $pos Position
*
* @return int Integer from the stream
*/
public function readint(string $unpack, int $pos): int
{
$data = unpack($unpack, $this->read($pos, 4));
if ($data === false) {
return PHP_INT_MAX;
}
$result = $data[1];
/* We're reading unsigned int, but PHP will happily
* give us negative number on 32-bit platforms.
*
* See also documentation:
* https://secure.php.net/manual/en/function.unpack.php#refsect1-function.unpack-notes
*/
return $result < 0 ? PHP_INT_MAX : $result;
}
/**
* Reads an array of integers from the stream.
*
* @param string $unpack Unpack string
* @param int $pos Position
* @param int $count How many elements should be read
*
* @return int[] Array of Integers
*/
public function readintarray(string $unpack, int $pos, int $count): array
{
$data = unpack($unpack . $count, $this->read($pos, 4 * $count));
if ($data === false) {
return [];
}
return $data;
}
}
motranslator-5.4.0/src/Translator.php 0000664 0000000 0000000 00000024177 14765446412 0017726 0 ustar 00root root 0000000 0000000 .
Copyright (c) 2005 Nico Kaiser
Copyright (c) 2016 Michal Čihař
This file is part of MoTranslator.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
namespace PhpMyAdmin\MoTranslator;
use PhpMyAdmin\MoTranslator\Cache\CacheInterface;
use PhpMyAdmin\MoTranslator\Cache\GetAllInterface;
use PhpMyAdmin\MoTranslator\Cache\InMemoryCache;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Throwable;
use function chr;
use function count;
use function explode;
use function get_class;
use function implode;
use function intval;
use function ltrim;
use function preg_replace;
use function rtrim;
use function sprintf;
use function stripos;
use function strpos;
use function strtolower;
use function substr;
use function trim;
/**
* Provides a simple gettext replacement that works independently from
* the system's gettext abilities.
* It can read MO files and use them for translating strings.
*
* It caches ll strings and translations to speed up the string lookup.
*/
class Translator
{
/**
* None error.
*/
public const ERROR_NONE = 0;
/**
* File does not exist.
*/
public const ERROR_DOES_NOT_EXIST = 1;
/**
* File has bad magic number.
*/
public const ERROR_BAD_MAGIC = 2;
/**
* Error while reading file, probably too short.
*/
public const ERROR_READING = 3;
/**
* Big endian mo file magic bytes.
*/
public const MAGIC_BE = "\x95\x04\x12\xde";
/**
* Little endian mo file magic bytes.
*/
public const MAGIC_LE = "\xde\x12\x04\x95";
/**
* Parse error code (0 if no error).
*
* @var int
*/
public $error = self::ERROR_NONE;
/**
* Cache header field for plural forms.
*
* @var string|null
*/
private $pluralEquation = null;
/** @var ExpressionLanguage|null Evaluator for plurals */
private $pluralExpression = null;
/** @var int|null number of plurals */
private $pluralCount = null;
/** @var CacheInterface */
private $cache;
/**
* @param CacheInterface|string|null $cache Mo file to load (null for no file) or a CacheInterface implementation
*/
public function __construct($cache)
{
if (! $cache instanceof CacheInterface) {
$cache = new InMemoryCache(new MoParser($cache));
}
$this->cache = $cache;
}
/**
* Translates a string.
*
* @param string $msgid String to be translated
*
* @return string translated string (or original, if not found)
*/
public function gettext(string $msgid): string
{
return $this->cache->get($msgid);
}
/**
* Check if a string is translated.
*
* @param string $msgid String to be checked
*/
public function exists(string $msgid): bool
{
return $this->cache->has($msgid);
}
/**
* Sanitize plural form expression for use in ExpressionLanguage.
*
* @param string $expr Expression to sanitize
*
* @return string sanitized plural form expression
*/
public static function sanitizePluralExpression(string $expr): string
{
// Parse equation
$expr = explode(';', $expr);
if (count($expr) >= 2) {
$expr = $expr[1];
} else {
$expr = $expr[0];
}
$expr = trim(strtolower($expr));
// Strip plural prefix
if (substr($expr, 0, 6) === 'plural') {
$expr = ltrim(substr($expr, 6));
}
// Strip equals
if (substr($expr, 0, 1) === '=') {
$expr = ltrim(substr($expr, 1));
}
// Cleanup from unwanted chars
$expr = preg_replace('@[^n0-9:\(\)\?=!<>/%&| ]@', '', $expr);
return (string) $expr;
}
/**
* Extracts number of plurals from plurals form expression.
*
* @param string $expr Expression to process
*
* @return int Total number of plurals
*/
public static function extractPluralCount(string $expr): int
{
$parts = explode(';', $expr, 2);
$nplurals = explode('=', trim($parts[0]), 2);
if (strtolower(rtrim($nplurals[0])) !== 'nplurals') {
return 1;
}
if (count($nplurals) === 1) {
return 1;
}
return intval($nplurals[1]);
}
/**
* Parse full PO header and extract only plural forms line.
*
* @param string $header Gettext header
*
* @return string verbatim plural form header field
*/
public static function extractPluralsForms(string $header): string
{
$headers = explode("\n", $header);
$expr = 'nplurals=2; plural=n == 1 ? 0 : 1;';
foreach ($headers as $header) {
if (stripos($header, 'Plural-Forms:') !== 0) {
continue;
}
$expr = substr($header, 13);
}
return $expr;
}
/**
* Get possible plural forms from MO header.
*
* @return string plural form header
*/
private function getPluralForms(): string
{
// lets assume message number 0 is header
// this is true, right?
// cache header field for plural forms
if ($this->pluralEquation === null) {
$header = $this->cache->get('');
$expr = $this->extractPluralsForms($header);
$this->pluralEquation = $this->sanitizePluralExpression($expr);
$this->pluralCount = $this->extractPluralCount($expr);
}
return $this->pluralEquation;
}
/**
* Detects which plural form to take.
*
* @param int $n count of objects
*
* @return int array index of the right plural form
*/
private function selectString(int $n): int
{
if ($this->pluralExpression === null) {
$this->pluralExpression = new ExpressionLanguage();
}
try {
$plural = (int) $this->pluralExpression->evaluate(
$this->getPluralForms(),
['n' => $n]
);
} catch (Throwable $e) {
$plural = 0;
}
if ($plural >= $this->pluralCount) {
$plural = $this->pluralCount - 1;
}
return $plural;
}
/**
* Plural version of gettext.
*
* @param string $msgid Single form
* @param string $msgidPlural Plural form
* @param int $number Number of objects
*
* @return string translated plural form
*/
public function ngettext(string $msgid, string $msgidPlural, int $number): string
{
// this should contains all strings separated by NULLs
$key = implode(chr(0), [$msgid, $msgidPlural]);
if (! $this->cache->has($key)) {
return $number !== 1 ? $msgidPlural : $msgid;
}
$result = $this->cache->get($key);
// find out the appropriate form
$select = $this->selectString($number);
$list = explode(chr(0), $result);
// @codeCoverageIgnoreStart
if ($list === false) {
// This was added in 3ff2c63bcf85f81b3a205ce7222de11b33e2bf56 for phpstan
// But according to the php manual it should never happen
return '';
}
// @codeCoverageIgnoreEnd
if (! isset($list[$select])) {
return $list[0];
}
return $list[$select];
}
/**
* Translate with context.
*
* @param string $msgctxt Context
* @param string $msgid String to be translated
*
* @return string translated plural form
*/
public function pgettext(string $msgctxt, string $msgid): string
{
$key = implode(chr(4), [$msgctxt, $msgid]);
$ret = $this->gettext($key);
if (strpos($ret, chr(4)) !== false) {
return $msgid;
}
return $ret;
}
/**
* Plural version of pgettext.
*
* @param string $msgctxt Context
* @param string $msgid Single form
* @param string $msgidPlural Plural form
* @param int $number Number of objects
*
* @return string translated plural form
*/
public function npgettext(string $msgctxt, string $msgid, string $msgidPlural, int $number): string
{
$key = implode(chr(4), [$msgctxt, $msgid]);
$ret = $this->ngettext($key, $msgidPlural, $number);
if (strpos($ret, chr(4)) !== false) {
return $msgid;
}
return $ret;
}
/**
* Set translation in place
*
* @param string $msgid String to be set
* @param string $msgstr Translation
*/
public function setTranslation(string $msgid, string $msgstr): void
{
$this->cache->set($msgid, $msgstr);
}
/**
* Set the translations
*
* @param array $translations The translations "key => value" array
*/
public function setTranslations(array $translations): void
{
$this->cache->setAll($translations);
}
/**
* Get the translations
*
* @return array The translations "key => value" array
*/
public function getTranslations(): array
{
if ($this->cache instanceof GetAllInterface) {
return $this->cache->getAll();
}
throw new CacheException(sprintf(
"Cache '%s' does not support getting translations",
get_class($this->cache)
));
}
}
motranslator-5.4.0/src/functions.php 0000664 0000000 0000000 00000015026 14765446412 0017576 0 ustar 00root root 0000000 0000000
Copyright (c) 2009 Danilo Segan
Copyright (c) 2016 Michal Čihař
This file is part of MoTranslator.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
use PhpMyAdmin\MoTranslator\Loader;
if (! function_exists('_setlocale')) {
/**
* Sets a requested locale.
*
* @param int $category Locale category, ignored
* @param string $locale Locale name
*
* @return string Set or current locale
*/
function _setlocale(int $category, string $locale): string
{
return Loader::getInstance()->setlocale($locale);
}
}
if (! function_exists('_bindtextdomain')) {
/**
* Sets the path for a domain.
*
* @param string $domain Domain name
* @param string $path Path where to find locales
*/
function _bindtextdomain(string $domain, string $path): void
{
Loader::getInstance()->bindtextdomain($domain, $path);
}
}
if (! function_exists('_bind_textdomain_codeset')) {
/**
* Dummy compatibility function, MoTranslator assumes
* everything is using same character set on input and
* output.
*
* Generally it is wise to output in UTF-8 and have
* mo files in UTF-8.
*
* @param string $domain Domain where to set character set
* @param string $codeset Character set to set
*/
function _bind_textdomain_codeset($domain, $codeset): void
{
}
}
if (! function_exists('_textdomain')) {
/**
* Sets the default domain.
*
* @param string $domain Domain name
*/
function _textdomain(string $domain): void
{
Loader::getInstance()->textdomain($domain);
}
}
if (! function_exists('_gettext')) {
/**
* Translates a string.
*
* @param string $msgid String to be translated
*
* @return string translated string (or original, if not found)
*/
function _gettext(string $msgid): string
{
return Loader::getInstance()->getTranslator()->gettext($msgid);
}
}
if (! function_exists('__')) {
/**
* Translates a string, alias for _gettext.
*
* @param string $msgid String to be translated
*
* @return string translated string (or original, if not found)
*/
function __(string $msgid): string
{
return Loader::getInstance()->getTranslator()->gettext($msgid);
}
}
if (! function_exists('_ngettext')) {
/**
* Plural version of gettext.
*
* @param string $msgid Single form
* @param string $msgidPlural Plural form
* @param int $number Number of objects
*
* @return string translated plural form
*/
function _ngettext(string $msgid, string $msgidPlural, int $number): string
{
return Loader::getInstance()->getTranslator()->ngettext($msgid, $msgidPlural, $number);
}
}
if (! function_exists('_pgettext')) {
/**
* Translate with context.
*
* @param string $msgctxt Context
* @param string $msgid String to be translated
*
* @return string translated plural form
*/
function _pgettext(string $msgctxt, string $msgid): string
{
return Loader::getInstance()->getTranslator()->pgettext($msgctxt, $msgid);
}
}
if (! function_exists('_npgettext')) {
/**
* Plural version of pgettext.
*
* @param string $msgctxt Context
* @param string $msgid Single form
* @param string $msgidPlural Plural form
* @param int $number Number of objects
*
* @return string translated plural form
*/
function _npgettext(string $msgctxt, string $msgid, string $msgidPlural, int $number): string
{
return Loader::getInstance()->getTranslator()->npgettext($msgctxt, $msgid, $msgidPlural, $number);
}
}
if (! function_exists('_dgettext')) {
/**
* Translates a string.
*
* @param string $domain Domain to use
* @param string $msgid String to be translated
*
* @return string translated string (or original, if not found)
*/
function _dgettext(string $domain, string $msgid): string
{
return Loader::getInstance()->getTranslator($domain)->gettext($msgid);
}
}
if (! function_exists('_dngettext')) {
/**
* Plural version of gettext.
*
* @param string $domain Domain to use
* @param string $msgid Single form
* @param string $msgidPlural Plural form
* @param int $number Number of objects
*
* @return string translated plural form
*/
function _dngettext(string $domain, string $msgid, string $msgidPlural, int $number): string
{
return Loader::getInstance()->getTranslator($domain)->ngettext($msgid, $msgidPlural, $number);
}
}
if (! function_exists('_dpgettext')) {
/**
* Translate with context.
*
* @param string $domain Domain to use
* @param string $msgctxt Context
* @param string $msgid String to be translated
*
* @return string translated plural form
*/
function _dpgettext(string $domain, string $msgctxt, string $msgid): string
{
return Loader::getInstance()->getTranslator($domain)->pgettext($msgctxt, $msgid);
}
}
if (! function_exists('_dnpgettext')) {
/**
* Plural version of pgettext.
*
* @param string $domain Domain to use
* @param string $msgctxt Context
* @param string $msgid Single form
* @param string $msgidPlural Plural form
* @param int $number Number of objects
*
* @return string translated plural form
*/
function _dnpgettext(string $domain, string $msgctxt, string $msgid, string $msgidPlural, int $number): string
{
return Loader::getInstance()->getTranslator($domain)->npgettext($msgctxt, $msgid, $msgidPlural, $number);
}
}
motranslator-5.4.0/tests/ 0000775 0000000 0000000 00000000000 14765446412 0015424 5 ustar 00root root 0000000 0000000 motranslator-5.4.0/tests/Cache/ 0000775 0000000 0000000 00000000000 14765446412 0016427 5 ustar 00root root 0000000 0000000 motranslator-5.4.0/tests/Cache/ApcuCacheFactoryTest.php 0000664 0000000 0000000 00000005130 14765446412 0023143 0 ustar 00root root 0000000 0000000 markTestSkipped('ACPu extension is not installed and enabled for CLI');
}
protected function tearDown(): void
{
parent::tearDown();
apcu_clear_cache();
}
public function testGetInstanceReturnApcuCache(): void
{
$factory = new ApcuCacheFactory();
$instance = $factory->getInstance(new MoParser(null), 'foo', 'bar');
self::assertInstanceOf(ApcuCache::class, $instance);
}
public function testConstructorSetsTtl(): void
{
$locale = 'foo';
$domain = 'bar';
$msgid = 'Column';
$ttl = 1;
$factory = new ApcuCacheFactory($ttl);
$parser = new MoParser(__DIR__ . '/../data/little.mo');
$factory->getInstance($parser, $locale, $domain);
sleep($ttl * 2);
apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $msgid, $success);
self::assertFalse($success);
}
public function testConstructorSetsReloadOnMiss(): void
{
$expected = 'Column';
$locale = 'foo';
$domain = 'bar';
$msgid = 'Column';
$factory = new ApcuCacheFactory(0, false);
$parser = new MoParser(__DIR__ . '/../data/little.mo');
$instance = $factory->getInstance($parser, $locale, $domain);
apcu_delete('mo_' . $locale . '.' . $domain . '.' . $msgid);
$actual = $instance->get($msgid);
self::assertSame($expected, $actual);
}
public function testConstructorSetsPrefix(): void
{
$expected = 'Pole';
$locale = 'foo';
$domain = 'bar';
$msgid = 'Column';
$prefix = 'baz_';
$factory = new ApcuCacheFactory(0, true, $prefix);
$parser = new MoParser(__DIR__ . '/../data/little.mo');
$factory->getInstance($parser, $locale, $domain);
$actual = apcu_fetch($prefix . $locale . '.' . $domain . '.' . $msgid);
self::assertSame($expected, $actual);
}
}
motranslator-5.4.0/tests/Cache/ApcuCacheTest.php 0000664 0000000 0000000 00000016535 14765446412 0021626 0 ustar 00root root 0000000 0000000 markTestSkipped('ACPu extension is not installed and enabled for CLI');
}
protected function tearDown(): void
{
parent::tearDown();
apcu_clear_cache();
}
public function testConstructorLoadsCache(): void
{
$expected = 'Pole';
$locale = 'foo';
$domain = 'bar';
$msgid = 'Column';
new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain);
$actual = apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $msgid);
self::assertSame($expected, $actual);
}
public function testConstructorSetsTtl(): void
{
$locale = 'foo';
$domain = 'bar';
$msgid = 'Column';
$ttl = 1;
new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain, $ttl);
sleep($ttl * 2);
apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $msgid, $success);
self::assertFalse($success);
apcu_fetch('mo_' . $locale . '.' . $domain . '.' . ApcuCache::LOADED_KEY, $success);
self::assertFalse($success);
}
public function testConstructorSetsReloadOnMiss(): void
{
$expected = 'Column';
$locale = 'foo';
$domain = 'bar';
$msgid = 'Column';
$prefix = 'baz_';
$cache = new ApcuCache(
new MoParser(__DIR__ . '/../data/little.mo'),
$locale,
$domain,
0,
false,
$prefix
);
apcu_delete($prefix . $locale . '.' . $domain . '.' . $msgid);
$actual = $cache->get($msgid);
self::assertSame($expected, $actual);
}
public function testConstructorSetsPrefix(): void
{
$expected = 'Pole';
$locale = 'foo';
$domain = 'bar';
$msgid = 'Column';
$prefix = 'baz_';
new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain, 0, true, $prefix);
$actual = apcu_fetch($prefix . $locale . '.' . $domain . '.' . $msgid);
self::assertSame($expected, $actual);
}
public function testEnsureTranslationsLoadedSetsLoadedKey(): void
{
$expected = 1;
$locale = 'foo';
$domain = 'bar';
new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain);
$actual = apcu_fetch('mo_' . $locale . '.' . $domain . '.' . ApcuCache::LOADED_KEY);
self::assertSame($expected, $actual);
}
public function testEnsureTranslationsLoadedHonorsLock(): void
{
$locale = 'foo';
$domain = 'bar';
$msgid = 'Column';
$lock = 'mo_' . $locale . '.' . $domain . '.' . ApcuCache::LOADED_KEY;
apcu_entry($lock, static function () {
sleep(1);
return 1;
});
new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain);
$actual = apcu_fetch($lock);
self::assertSame(1, $actual);
apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $msgid, $success);
self::assertFalse($success);
}
public function testGetReturnsMsgstr(): void
{
$expected = 'Pole';
$msgid = 'Column';
$cache = new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), 'foo', 'bar');
$actual = $cache->get($msgid);
self::assertSame($expected, $actual);
}
public function testGetReturnsMsgidForCacheMiss(): void
{
$expected = 'Column';
$cache = new ApcuCache(new MoParser(null), 'foo', 'bar');
$actual = $cache->get($expected);
self::assertSame($expected, $actual);
}
public function testStoresMsgidOnCacheMiss(): void
{
$expected = 'Column';
$locale = 'foo';
$domain = 'bar';
$cache = new ApcuCache(new MoParser(null), $locale, $domain);
$cache->get($expected);
$actual = apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $expected);
self::assertSame($expected, $actual);
}
public function testGetReloadsOnCacheMiss(): void
{
$expected = 'Pole';
$locale = 'foo';
$domain = 'bar';
$msgid = 'Column';
$cache = new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), $locale, $domain);
apcu_delete('mo_' . $locale . '.' . $domain . '.' . ApcuCache::LOADED_KEY);
$actual = $cache->get($msgid);
self::assertSame($expected, $actual);
}
public function testReloadOnMissHonorsLock(): void
{
$expected = 'Pole';
$locale = 'foo';
$domain = 'bar';
$msgid = 'Column';
$cache = new ApcuCache(new MoParser(null), $locale, $domain);
$method = new ReflectionMethod($cache, 'reloadOnMiss');
$method->setAccessible(true);
$key = 'mo_' . $locale . '.' . $domain . '.' . $msgid;
apcu_entry($key, static function () use ($expected): string {
sleep(1);
return $expected;
});
$actual = $method->invoke($cache, $msgid);
self::assertSame($expected, $actual);
}
public function testSetSetsMsgstr(): void
{
$expected = 'Pole';
$msgid = 'Column';
$cache = new ApcuCache(new MoParser(null), 'foo', 'bar');
$cache->set($msgid, $expected);
$actual = $cache->get($msgid);
self::assertSame($expected, $actual);
}
public function testHasReturnsFalse(): void
{
$cache = new ApcuCache(new MoParser(null), 'foo', 'bar');
$actual = $cache->has('Column');
self::assertFalse($actual);
}
public function testHasReturnsTrue(): void
{
$cache = new ApcuCache(new MoParser(__DIR__ . '/../data/little.mo'), 'foo', 'bar');
$actual = $cache->has('Column');
self::assertTrue($actual);
}
public function testSetAllSetsTranslations(): void
{
$translations = [
'foo' => 'bar',
'and' => 'another',
];
$cache = new ApcuCache(new MoParser(null), 'foo', 'bar');
$cache->setAll($translations);
foreach ($translations as $msgid => $expected) {
$actual = $cache->get($msgid);
self::assertSame($expected, $actual);
}
}
public function testCacheStoresPluralForms(): void
{
$expected = ['first', 'second'];
$plural = ["%d pig went to the market\n", "%d pigs went to the market\n"];
$msgid = implode(chr(0), $plural);
$cache = new ApcuCache(new MoParser(null), 'foo', 'bar');
$cache->set($msgid, implode(chr(0), $expected));
$msgstr = $cache->get($msgid);
$actual = explode(chr(0), $msgstr);
self::assertSame($expected, $actual);
}
}
motranslator-5.4.0/tests/Cache/ApcuDisabledTest.php 0000664 0000000 0000000 00000001400 14765446412 0022313 0 ustar 00root root 0000000 0000000 markTestSkipped('ext-apcu is enabled');
}
$this->expectException(CacheException::class);
$this->expectExceptionMessage('ACPu extension must be installed and enabled');
new ApcuCache(new MoParser(null), 'foo', 'bar');
}
}
motranslator-5.4.0/tests/Cache/InMemoryCacheTest.php 0000664 0000000 0000000 00000004410 14765446412 0022462 0 ustar 00root root 0000000 0000000 get('Column');
self::assertSame($expected, $actual);
}
public function testGetReturnsMsgidForCacheMiss(): void
{
$expected = 'Column';
$cache = new InMemoryCache(new MoParser(null));
$actual = $cache->get($expected);
self::assertSame($expected, $actual);
}
public function testSetSetsMsgstr(): void
{
$expected = 'Pole';
$msgid = 'Column';
$cache = new InMemoryCache(new MoParser(null));
$cache->set($msgid, $expected);
$actual = $cache->get($msgid);
self::assertSame($expected, $actual);
}
public function testHasReturnsFalse(): void
{
$cache = new InMemoryCache(new MoParser(null));
$actual = $cache->has('Column');
self::assertFalse($actual);
}
public function testHasReturnsTrue(): void
{
$cache = new InMemoryCache(new MoParser(__DIR__ . '/../data/little.mo'));
$actual = $cache->has('Column');
self::assertTrue($actual);
}
public function testSetAllSetsTranslations(): void
{
$translations = [
'foo' => 'bar',
'and' => 'another',
];
$cache = new InMemoryCache(new MoParser(null));
$cache->setAll($translations);
foreach ($translations as $msgid => $expected) {
$actual = $cache->get($msgid);
self::assertSame($expected, $actual);
}
}
public function testGetAllReturnsTranslations(): void
{
$expected = [
'foo' => 'bar',
'and' => 'another',
];
$cache = new InMemoryCache(new MoParser(null));
$cache->setAll($expected);
$actual = $cache->getAll();
self::assertSame($expected, $actual);
}
}
motranslator-5.4.0/tests/FunctionsTest.php 0000664 0000000 0000000 00000002424 14765446412 0020747 0 ustar 00root root 0000000 0000000 setlocale($locale);
$loader->textdomain($domain);
$loader->bindtextdomain($domain, __DIR__ . '/data/locale/');
return $loader;
}
public function testLocaleChange(): void
{
$loader = new Loader();
$loader->setlocale('cs');
$loader->textdomain('phpmyadmin');
$loader->bindtextdomain('phpmyadmin', __DIR__ . '/data/locale/');
$translator = $loader->getTranslator('phpmyadmin');
self::assertSame('Typ', $translator->gettext('Type'));
$loader->setlocale('be_BY');
$translator = $loader->getTranslator('phpmyadmin');
self::assertSame('Тып', $translator->gettext('Type'));
}
/**
* @dataProvider translatorData
*/
public function testGetTranslator(string $domain, string $locale, string $otherdomain, string $expected): void
{
$loader = $this->getLoader($domain, $locale);
$translator = $loader->getTranslator($otherdomain);
self::assertSame($expected, $translator->gettext('Type'));
}
/**
* @return array[]
*/
public static function translatorData(): array
{
return [
[
'phpmyadmin',
'cs',
'',
'Typ',
],
[
'phpmyadmin',
'cs_CZ',
'',
'Typ',
],
[
'phpmyadmin',
'be_BY',
'',
'Тып',
],
[
'phpmyadmin',
'be@latin',
'',
'Typ',
],
[
'phpmyadmin',
'cs',
'other',
'Type',
],
[
'other',
'cs',
'phpmyadmin',
'Type',
],
];
}
public function testInstance(): void
{
$loader = Loader::getInstance();
$loader->setlocale('cs');
$loader->textdomain('phpmyadmin');
$loader->bindtextdomain('phpmyadmin', __DIR__ . '/data/locale/');
$translator = $loader->getTranslator();
self::assertSame('Typ', $translator->gettext('Type'));
/* Ensure the object survives */
$loader = Loader::getInstance();
$translator = $loader->getTranslator();
self::assertSame('Typ', $translator->gettext('Type'));
/* Ensure the object can support different locale files for the same domain */
$loader = Loader::getInstance();
$loader->setlocale('be_BY');
$loader->bindtextdomain('phpmyadmin', __DIR__ . '/data/locale/');
$translator = $loader->getTranslator();
self::assertSame('Тып', $translator->gettext('Type'));
}
public function testDetect(): void
{
$GLOBALS['lang'] = 'foo';
$loader = Loader::getInstance();
self::assertSame('foo', $loader->detectlocale());
unset($GLOBALS['lang']);
}
public function testDetectEnv(): void
{
$loader = Loader::getInstance();
foreach (['LC_MESSAGES', 'LC_ALL', 'LANG'] as $var) {
putenv($var);
if (getenv($var) === false) {
continue;
}
$this->markTestSkipped('Unsetting environment does not work');
}
unset($GLOBALS['lang']);
putenv('LC_ALL=baz');
self::assertSame('baz', $loader->detectlocale());
putenv('LC_ALL');
putenv('LC_MESSAGES=bar');
self::assertSame('bar', $loader->detectlocale());
putenv('LC_MESSAGES');
putenv('LANG=barr');
self::assertSame('barr', $loader->detectlocale());
putenv('LANG');
self::assertSame('en', $loader->detectlocale());
}
public function testSetCacheFactory(): void
{
$expected = 'Foo';
$locale = 'be_BY';
$domain = 'apcu';
$cache = $this->createMock(CacheInterface::class);
$cache->method('get')
->willReturn($expected);
/** @var CacheFactoryInterface&MockObject $factory */
$factory = $this->createMock(CacheFactoryInterface::class);
$factory->expects($this->once())
->method('getInstance')
->with($this->isInstanceOf(MoParser::class), $locale, $domain)
->willReturn($cache);
Loader::setCacheFactory($factory);
$loader = Loader::getInstance();
$loader->setlocale($locale);
$loader->bindtextdomain($domain, __DIR__ . '/data/locale/');
$translator = $loader->getTranslator($domain);
$actual = $translator->gettext('Type');
self::assertSame($expected, $actual);
}
}
motranslator-5.4.0/tests/MoFilesTest.php 0000664 0000000 0000000 00000010513 14765446412 0020333 0 ustar 00root root 0000000 0000000 getTranslator($filename);
self::assertSame('Pole', $parser->gettext('Column'));
// Non existing string
self::assertSame('Column parser', $parser->gettext('Column parser'));
}
/**
* @dataProvider provideMoFiles
*/
public function testMoFilePlurals(string $filename): void
{
$parser = $this->getTranslator($filename);
$expected2 = '%d sekundy';
if (strpos($filename, 'invalid-formula.mo') !== false || strpos($filename, 'lessplurals.mo') !== false) {
$expected0 = '%d sekunda';
$expected2 = '%d sekunda';
} elseif (strpos($filename, 'plurals.mo') !== false || strpos($filename, 'noheader.mo') !== false) {
$expected0 = '%d sekundy';
} else {
$expected0 = '%d sekund';
}
self::assertSame($expected0, $parser->ngettext('%d second', '%d seconds', 0));
self::assertSame('%d sekunda', $parser->ngettext('%d second', '%d seconds', 1));
self::assertSame($expected2, $parser->ngettext('%d second', '%d seconds', 2));
self::assertSame($expected0, $parser->ngettext('%d second', '%d seconds', 5));
self::assertSame($expected0, $parser->ngettext('%d second', '%d seconds', 10));
// Non existing string
self::assertSame('"%d" seconds', $parser->ngettext('"%d" second', '"%d" seconds', 10));
}
/**
* @dataProvider provideMoFiles
*/
public function testMoFileContext(string $filename): void
{
$parser = $this->getTranslator($filename);
self::assertSame('Tabulka', $parser->pgettext('Display format', 'Table'));
}
/**
* @dataProvider provideNotTranslatedFiles
*/
public function testMoFileNotTranslated(string $filename): void
{
$parser = $this->getTranslator($filename);
self::assertSame('%d second', $parser->ngettext('%d second', '%d seconds', 1));
}
/**
* @return array[]
*/
public static function provideMoFiles(): array
{
return self::getFiles('./tests/data/*.mo');
}
/**
* @return array[]
*/
public static function provideErrorMoFiles(): array
{
return self::getFiles('./tests/data/error/*.mo');
}
/**
* @return array[]
*/
public static function provideNotTranslatedFiles(): array
{
return self::getFiles('./tests/data/not-translated/*.mo');
}
/**
* @dataProvider provideErrorMoFiles
*/
public function testEmptyMoFile(string $file): void
{
$parser = new MoParser($file);
$translator = new Translator(new InMemoryCache($parser));
if (basename($file) === 'magic.mo') {
self::assertSame(Translator::ERROR_BAD_MAGIC, $parser->error);
} else {
self::assertSame(Translator::ERROR_READING, $parser->error);
}
self::assertSame('Table', $translator->pgettext('Display format', 'Table'));
self::assertSame('"%d" seconds', $translator->ngettext('"%d" second', '"%d" seconds', 10));
}
/**
* @dataProvider provideMoFiles
*/
public function testExists(string $file): void
{
$parser = $this->getTranslator($file);
self::assertTrue($parser->exists('Column'));
self::assertFalse($parser->exists('Column parser'));
}
/**
* @param string $pattern path names pattern to match
*
* @return array[]
*/
private static function getFiles(string $pattern): array
{
$files = glob($pattern);
if ($files === false) {
return [];
}
$result = [];
foreach ($files as $file) {
$result[] = [$file];
}
return $result;
}
private function getTranslator(string $filename): Translator
{
return new Translator(new InMemoryCache(new MoParser($filename)));
}
}
motranslator-5.4.0/tests/PluralFormulaTest.php 0000664 0000000 0000000 00000007015 14765446412 0021565 0 ustar 00root root 0000000 0000000 =3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5;\n",
'n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5',
],
[
' nplurals=1; plural=baz(n);',
'(n)',
],
[
' plural=n',
'n',
],
[
'nplurals',
'n',
],
];
}
}
motranslator-5.4.0/tests/PluralTest.php 0000664 0000000 0000000 00000010014 14765446412 0020230 0 ustar 00root root 0000000 0000000 getTranslator('');
$result = $parser->npgettext('context', "%d pig went to the market\n", "%d pigs went to the market\n", $number);
self::assertSame($expected, $result);
}
/**
* Data provider for test_npgettext.
*
* @return array[]
*/
public static function providerTestNpgettext(): array
{
return [
[
1,
"%d pig went to the market\n",
],
[
2,
"%d pigs went to the market\n",
],
];
}
/**
* Test for ngettext
*/
public function testNgettext(): void
{
$parser = $this->getTranslator('');
$translationKey = implode(chr(0), ["%d pig went to the market\n", "%d pigs went to the market\n"]);
$parser->setTranslation($translationKey, '');
$result = $parser->ngettext("%d pig went to the market\n", "%d pigs went to the market\n", 1);
self::assertSame('', $result);
}
/**
* @return array[]
*/
public static function dataProviderPluralForms(): array
{
return [
['Plural-Forms: nplurals=2; plural=n != 1;'],
['Plural-Forms: nplurals=1; plural=0;'],
['Plural-Forms: nplurals=2; plural=(n > 1);'],
[
'Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n'
. '%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;',
],
['Plural-Forms: nplurals=2; plural=n >= 2 && (n < 11 || n > 99);'],
['Plural-Forms: nplurals=4; plural=n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3;'],
['Plural-Forms: nplurals=3; plural=n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2;'],
[
'Plural-Forms: nplurals=2; plural=n != 1 && n != 2 && n != 3 &'
. '& (n % 10 == 4 || n % 10 == 6 || n % 10 == 9);',
],
];
}
/**
* Test for ngettext
*
* @see https://github.com/phpmyadmin/motranslator/issues/37
*
* @dataProvider dataProviderPluralForms
*/
public function testNgettextSelectString(string $pluralForms): void
{
$parser = $this->getTranslator('');
$parser->setTranslation(
'',
"Project-Id-Version: phpMyAdmin 5.1.0-dev\n"
. "Report-Msgid-Bugs-To: translators@phpmyadmin.net\n"
. "PO-Revision-Date: 2020-09-01 09:12+0000\n"
. "Last-Translator: William Desportes \n"
. 'Language-Team: English (United Kingdom) '
. "\n"
. "Language: en_GB\n"
. "MIME-Version: 1.0\n"
. "Content-Type: text\/plain; charset=UTF-8\n"
. "Content-Transfer-Encoding: 8bit\n"
. $pluralForms . "\n"
. "X-Generator: Weblate 4.2.1-dev\n"
. ''
);
$translationKey = implode(chr(0), ["%d pig went to the market\n", "%d pigs went to the market\n"]);
$parser->setTranslation($translationKey, 'ok');
$result = $parser->ngettext("%d pig went to the market\n", "%d pigs went to the market\n", 1);
self::assertSame('ok', $result);
}
private function getTranslator(string $filename): Translator
{
return new Translator(new InMemoryCache(new MoParser($filename)));
}
}
motranslator-5.4.0/tests/StringReaderTest.php 0000664 0000000 0000000 00000002157 14765446412 0021373 0 ustar 00root root 0000000 0000000 read(-1, -1);
self::assertSame('', $actual);
}
public function testReadIntArray(): void
{
$tempFile = (string) tempnam(sys_get_temp_dir(), 'phpMyAdmin_StringReaderTest');
file_put_contents($tempFile, "\0\0\0\0\0\0\0\0\0\0\0\0");
self::assertFileExists($tempFile);
$stringReader = new StringReader($tempFile);
unlink($tempFile);
$actual = $stringReader->readintarray('V', 2, 2);
self::assertSame([
1 => 0,
2 => 0,
], $actual);
}
}
motranslator-5.4.0/tests/TranslatorTest.php 0000664 0000000 0000000 00000006307 14765446412 0021134 0 ustar 00root root 0000000 0000000 gettext('Column');
self::assertSame($expected, $actual);
}
public function testConstructorWithNullParam(): void
{
$expected = 'Column';
$translator = new Translator(null);
$actual = $translator->gettext($expected);
self::assertSame($expected, $actual);
}
/**
* Test on empty gettext
*/
public function testGettext(): void
{
$translator = $this->getTranslator('');
self::assertSame('Test', $translator->gettext('Test'));
}
/**
* Test set a translation
*/
public function testSetTranslation(): void
{
$translator = $this->getTranslator('');
$translator->setTranslation('Test', 'Translation');
self::assertSame('Translation', $translator->gettext('Test'));
}
/**
* Test get and set all translations
*/
public function testGetSetTranslations(): void
{
$transTable = ['Test' => 'Translation'];
$translator = $this->getTranslator('');
$translator->setTranslations($transTable);
self::assertSame('Translation', $translator->gettext('Test'));
self::assertSame($transTable, $translator->getTranslations());
$translator = $this->getTranslator(null);
$translator->setTranslations($transTable);
self::assertSame($transTable, $translator->getTranslations());
self::assertSame('Translation', $translator->gettext('Test'));
$transTable = [
'Test' => 'Translation',
'shouldIWriteTests' => 'as much as possible',
'is it hard' => 'it depends',
];
$translator = $this->getTranslator('');
$translator->setTranslations($transTable);
self::assertSame($transTable, $translator->getTranslations());
self::assertSame('as much as possible', $translator->gettext('shouldIWriteTests'));
$translator = $this->getTranslator(null);
$translator->setTranslations($transTable);
self::assertSame($transTable, $translator->getTranslations());
self::assertSame('it depends', $translator->gettext('is it hard'));
}
public function testGetTranslationsThrowsException(): void
{
/** @var CacheInterface&MockObject $cache */
$cache = $this->createMock(CacheInterface::class);
$translator = new Translator($cache);
$this->expectException(CacheException::class);
$translator->getTranslations();
}
private function getTranslator(?string $filename): Translator
{
return new Translator(new InMemoryCache(new MoParser($filename)));
}
}
motranslator-5.4.0/tests/data/ 0000775 0000000 0000000 00000000000 14765446412 0016335 5 ustar 00root root 0000000 0000000 motranslator-5.4.0/tests/data/big.mo 0000664 0000000 0000000 00001210367 14765446412 0017445 0 ustar 00root root 0000000 0000000 ^ ` a ( | F @ [ 6 &