Skip to content

Commit cdb406a

Browse files
committed
Implement context functions
1 parent df167da commit cdb406a

File tree

11 files changed

+2014
-1
lines changed

11 files changed

+2014
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor/
2+
/build
3+
.phpunit.result.cache
4+
.idea

README.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,55 @@
11
# gettext-context
2-
Implementation of missing context methods for PHP gettext
2+
Implementation of context functions for the PHP gettext extension
3+
4+
![Coverage 100%](assets/coverage.svg)
5+
6+
## About
7+
PHP still lacks full support for gettext, because it does not implement the context functions.
8+
Until this is sorted out, you can use this package to add context support to your localization efforts.
9+
10+
## Requirements
11+
- PHP >= 7.2
12+
- gettext PHP extension
13+
14+
It can be used on lower versions of PHP, but you won't be able to install it with composer (or run tests).
15+
16+
## Installing
17+
1. Download it with composer: `composer require datalinx/gettext-context`
18+
2. Include the `vendor/datalinx/gettext-context/src/gettext-context.php` file when you need it*
19+
20+
\* It's not added to the `autoload` directive, since you might not need or want to always include it in runtime. If you want to always load it, just add the source file to your `composer.json` autoload files list:
21+
22+
```json
23+
"autoload": {
24+
"files": [
25+
"vendor/datalinx/gettext-context/src/gettext-context.php"
26+
]
27+
}
28+
```
29+
30+
## Usage
31+
See the documented [src/gettext-context.php](src/gettext-context.php) file for the list of functions and their parameters.
32+
33+
## Extracting messages with context support
34+
35+
### With xgettext
36+
You can add extra keyword parameters to your `xgettext` call to include the context functions.
37+
For example, this would be used in our package:
38+
39+
```shell
40+
xgettext --force-po --keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 --keyword=dpgettext:2c,3 --keyword=dnpgettext:2c,3,4 -c -o messages.po tests/Test.php
41+
```
42+
43+
### With Poedit
44+
If you're using Poedit, add the following keywords in your Catalog > Properties > Sources Keywords:
45+
46+
- `pgettext:1c,2`
47+
- `npgettext:1c,2,3`
48+
- `dpgettext:2c,3`
49+
- `dnpgettext:2c,3,4`
50+
51+
Then run the "Update from code" procedure :)
52+
53+
## Credits
54+
- [This answer](https://stackoverflow.com/questions/16260798/gettext-how-to-handle-homonyms/16263617#16263617) on Stack Overflow for the implementation.
55+
- [This comment](https://www.php.net/manual/en/book.gettext.php#89975) on the PHP documentation for the messages extraction fix.

assets/coverage.svg

Lines changed: 21 additions & 0 deletions
Loading

build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Generate coverage badge
2+
./vendor/bin/php-coverage-badger build/coverage.xml assets/coverage.svg

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "datalinx/gettext-context",
3+
"description": "Implementation of context functions for the PHP gettext extension",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Omer Sabic",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.3",
14+
"ext-gettext": "*"
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "^8.5",
18+
"jaschilz/php-coverage-badger": "^2.0"
19+
},
20+
"autoload-dev": {
21+
"files": [
22+
"src/gettext-context.php"
23+
]
24+
}
25+
}

0 commit comments

Comments
 (0)