Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 4231f66138c1a7c0f4607e56449e80961f016a69 0 parents
@azatoth authored
Showing with 132 additions and 0 deletions.
  1. +45 −0 pgettext.php
  2. +12 −0 php-xgettext
  3. +14 −0 run_test.sh
  4. +45 −0 sv.po
  5. +16 −0 test.php
45 pgettext.php
@@ -0,0 +1,45 @@
+<?php
+
+/* These functions are related to the normal gettext functions except that they are
+ * taking an context variable as well */
+function pgettext($msg_ctxt, $msgid) {
+ return dpgettext(textdomain(null), $msg_ctxt, $msgid);
+}
+
+function dpgettext($domain, $msg_ctxt, $msgid) {
+ return dcpgettext($domain, $msg_ctxt, $msgid, LC_MESSAGES);
+}
+
+function dcpgettext($domain, $msg_ctxt, $msgid, $category) {
+ $msg_ctxt_id = "{$msg_ctxt}\004{$msgid}";
+ $translation = dcgettext( $domain, $msg_ctxt_id, $category);
+ if( $translation == $msg_ctxt_id ) {
+ return $msgid;
+ } else {
+ return $translation;
+ }
+}
+
+function npgettext($msg_ctxt, $msgid, $msgid_plural, $n) {
+ return dnpgettext(textdomain(null), $msg_ctxt, $msgid, $msgid_plural, $n);
+}
+
+function dnpgettext($domain, $msg_ctxt, $msgid, $msgid_plural, $n) {
+ return dcnpgettext($domain, $msg_ctxt, $msgid, $msgid_plural, $n, LC_MESSAGES);
+}
+
+function dcnpgettext($domain, $msg_ctxt, $msgid, $msgid_plural, $n, $category) {
+ $msg_ctxt_id = "{$msg_ctxt}\004{$msgid}";
+ $translation = dcngettext( $domain, $msg_ctxt_id, $msgid_plural, $n, $category);
+ if( $translation == $msg_ctxt_id || $translation == $msgid_plural ) {
+ return $n == 1 ? $msgid : $msgid_plural;
+ } else {
+ return $translation;
+ }
+}
+
+/* PHP also lacks gettext_noop; Even though this isn't an simple macro expansion as
+ * it is in the c code, I assume it won't affect the performance much when used. */
+function gettext_noop($msgid) {
+ return $msgid;
+}
12 php-xgettext
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+xgettext \
+ --language=PHP \
+ --keyword="pgettext:1c,2" \
+ --keyword="dpgettext:2c,3" \
+ --keyword="dcpgettext:2c,3" \
+ --keyword="npgettext:1c,2,3" \
+ --keyword="dnpgettext:2c,3,4" \
+ --keyword="dcnpgettext:2c,3,4" \
+ --keyword="gettext_noop" \
+ "$@"
14 run_test.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+echo "before catalog generation"
+php test.php
+./php-xgettext --from-code="UTF-8" --package-name="test" --package-version="1.0" --output=messages.pot test.php
+
+msgmerge sv.po messages.pot -U
+msgfmt sv.po
+mkdir sv/LC_MESSAGES -p
+mv messages.mo sv/LC_MESSAGES/
+echo "after catalog generation"
+php test.php
+
+rm -rf sv messages.*
45 sv.po
@@ -0,0 +1,45 @@
+# Swedish translations for test package
+# Svenska översättningar för paket test.
+# Copyright (C) 2011 THE test'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the test package.
+# Carl Fürstenberg <azatoth@azaboxen.carl>, 2011.
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: test 1.0\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2011-08-11 02:57+0200\n"
+"PO-Revision-Date: 2011-08-11 02:58+0200\n"
+"Last-Translator: Carl Fürstenberg <azatoth@azaboxen.carl>\n"
+"Language-Team: Swedish\n"
+"Language: sv\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n != 1);\n"
+
+#: test.php:12
+#, php-format
+msgctxt "body"
+msgid "One heart\n"
+msgid_plural "%d hearts\n"
+msgstr[0] "Ett hjärta\n"
+msgstr[1] "%d hjärtan\n"
+
+#: test.php:13
+#, php-format
+msgctxt "place"
+msgid "One heart\n"
+msgid_plural "%d hearts\n"
+msgstr[0] "Ett centrum\n"
+msgstr[1] "%d centrum\n"
+
+#: test.php:15
+msgctxt "door"
+msgid "Open\n"
+msgstr "Öppna\n"
+
+#: test.php:16
+msgctxt "book"
+msgid "Open\n"
+msgstr "Slå upp\n"
16 test.php
@@ -0,0 +1,16 @@
+<?php
+include_once("pgettext.php");
+$domain = 'messages';
+$directory = dirname(__FILE__);
+$locale ="sv_SE.utf8";
+setlocale( LC_MESSAGES, $locale);
+bindtextdomain($domain, $directory);
+textdomain($domain);
+bind_textdomain_codeset($domain, 'UTF-8');
+
+foreach(array(1,2) as $nbr) {
+ printf(npgettext("body", "One heart\n", "%d hearts\n", $nbr), $nbr);
+ printf(npgettext("place", "One heart\n", "%d hearts\n", $nbr), $nbr);
+}
+printf(pgettext("door", "Open\n"));
+printf(pgettext("book", "Open\n"));
Please sign in to comment.
Something went wrong with that request. Please try again.