Skip to content

Instantly share code, notes, and snippets.

@yookoala
Last active November 9, 2018 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yookoala/64c6960f2c08dbf527481aae32391312 to your computer and use it in GitHub Desktop.
Save yookoala/64c6960f2c08dbf527481aae32391312 to your computer and use it in GitHub Desktop.
PHP Translaion String Extraction with xgettext
all: messages.po
clean:
rm -f messages.po
.PHONY: all clean
messages.po:
xgettext \
--language=PHP \
--add-comments=I10N \
--force-po \
-o "$@" \
--keyword=__:1 \
--keyword=__:1,2 \
--keyword=__:1,2,3c \
--keyword=Translation:1c,2,3 \
--keyword=translate:1c,2,3 \
--keyword=staticTranslate:1c,2,3 \
*.php
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-11-10 00:52+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
#: test.php:3
msgid "Hello world"
msgstr ""
#. I10N: This is a display for the number of apple(s).
#: test.php:6
#, php-format
msgid "There is %d apple"
msgid_plural "There are %d apples"
msgstr[0] ""
msgstr[1] ""
#. I10N: This is for display number of apple(s) with named placeholder.
#: test.php:9
msgid "There is {number} apple"
msgid_plural "There are {number} apples"
msgstr[0] ""
msgstr[1] ""
#. I10N: This is for displaying number of days.
#: test.php:12
msgid "There is {number} day"
msgid_plural "There are {number} days"
msgstr[0] ""
msgstr[1] ""
#. I10N: This is a test to use Gettext library
#: test.php:15
#, php-format
msgctxt "comments"
msgid "One comment"
msgid_plural "%s comments"
msgstr[0] ""
msgstr[1] ""
#. I10N: This is a test to use class method for translations
#: test.php:19
#, php-format
msgctxt "using method"
msgid "One method"
msgid_plural "%d methods"
msgstr[0] ""
msgstr[1] ""
#. I10N: This is a second test to use class method for translations
#: test.php:21
#, php-format
msgctxt "using method 2"
msgid "One chained method"
msgid_plural "%d chained methods"
msgstr[0] ""
msgstr[1] ""
#. I10N: This is a test to use static class method for translation
#: test.php:24
#, php-format
msgctxt "using static method"
msgid "One static method"
msgid_plural "%d static methods"
msgstr[0] ""
msgstr[1] ""
<?php
__('Hello world');
// I10N: This is a display for the number of apple(s).
ngettext('There is %d apple', 'There are %d apples');
// I10N: This is for display number of apple(s) with named placeholder.
ngettext('There is {number} apple', 'There are {number} apples');
// I10N: This is for displaying number of days.
__('There is {number} day', 'There are {number} days');
// I10N: This is a test to use Gettext library
$t1 = new Gettext\Translation('comments', 'One comment', '%s comments');
$t2 = $translator
// I10N: This is a test to use class method for translations
->translate('using method', 'One method', '%d methods')
// I10N: This is a second test to use class method for translations
->translate('using method 2', 'One chained method', '%d chained methods');
// I10N: This is a test to use static class method for translation
$t3 = SomeClass::staticTranslate('using static method', 'One static method', '%d static methods');
@yookoala
Copy link
Author

yookoala commented Nov 9, 2018

This gist demonstrates how to customize keywords in xgettext to extract translation string from the function / class method of your choice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment