[ create a new paste ] login | about

Link: http://codepad.org/ZdXPrMdJ    [ raw code | output | fork ]

PHP, pasted on Mar 9:
<?php

// OtakuCMS translation file generator

// Input PO file

$src = '
#  translator-comments
#. extracted-comments
#: reference...
#, flag...
#| msgid previous-untranslated-string
msgctxt "Dictionary form"
msgid "Enter a comma separated list of user names."
msgstr "Eine kommagetrennte Liste von Benutzernamen."

#: modules/user/views_handler_filter_user_name.inc:112
msgid "Unable to find user: %s"
msgctxt "german"
msgid_plural "Unable to find users: %s"
msgstr[0] "Benutzer konnte nicht gefunden werden: %s"
msgstr[1] "Benutzer konnten nicht gefunden werden:%s"

msgid ""
"Here is an example of how one might continue a very long string\n"
"for the common case the string represents multi-line output.\n"
msgstr "Eine kommagetrennte Liste von Benutzernamen.\n"
"Benutzer konnte nicht gefunden werden: %s"
';

echo "<pre>\n";

Compiler::process_po($src);

print_r(Compiler::$po);

print_r(Compiler::$t);

class Compiler
{
	public static $po; // array of lines of the PO file
	public static $t = array();

	private static $str = '';
	private static $getting_str = false;
	private static $target = '';

	private static $index = 0;
	private static $indexInc = 1;

	public static function process_po($src)
	{
		// Split the PO file into lines
		self::$po = explode("\n", $src);

		$line_types = array('"', 'msgctxt', 'msgid_plural', 'msgid', 'msgstr');

		foreach(self::$po as $line)
		{
			// Examine the start of the line

			$type = '';
			foreach($line_types as $lt)
				if (strpos($line, $lt) === 0)
				{
					$type = $lt;
					break;
				}

			echo "$type\n";

			switch($type)
			{
				case '"':
					self::add_to_str($line);
					self::$getting_str = true;
					break;

				case 'msgctxt':
				case 'msgid':
				case 'msgid_plural':
				case 'msgstr':
					self::end_str();
					self::$target = $type;
					$line = str_replace($type, '', $line);
					self::$str = self::get_str($line);
					self::$getting_str = true;
					break;

				default:
					self::end_str();
				break;
			}
		}
	}

	private static function add_to_str($s)
	{
		self::$str .= self::get_str($s);
	}

	private static function end_str()
	{
		if (self::$getting_str)
		{
			switch (self::$target)
			{
				case 'msgid':
					self::$index += self::$indexInc;
					self::$indexInc = 0;
					self::$t[self::$index]['id'] = self::$str;
					break;

				case 'msgctxt':
					self::$index += self::$indexInc;
					self::$indexInc = 0;
					self::$t[self::$index]['cx'] = self::$str;
					break;

				case 'msgid_plural':
					self::$t[self::$index]['pl'] = self::$str;
					self::$indexInc = 1;
					break;

				case 'msgstr':
					self::$t[self::$index]['tr'][] = self::$str;
					self::$indexInc = 1;
					break;

			}
			self::$getting_str = false;
			self::$str = '';
			self::$target = '';
		}
	}

	private static function get_str($s)
	{
		echo "$s\n";
		$s = rtrim(substr($s, strpos($s, '"') + 1), "\r\" ");
		echo "$s\n";
		return $s;
	}

}


Output:
<pre>






msgctxt
 "Dictionary form"
Dictionary form
msgid
 "Enter a comma separated list of user names."
Enter a comma separated list of user names.
msgstr
 "Eine kommagetrennte Liste von Benutzernamen."
Eine kommagetrennte Liste von Benutzernamen.


msgid
 "Unable to find user: %s"
Unable to find user: %s
msgctxt
 "german"
german
msgid_plural
 "Unable to find users: %s"
Unable to find users: %s
msgstr
[0] "Benutzer konnte nicht gefunden werden: %s"
Benutzer konnte nicht gefunden werden: %s
msgstr
[1] "Benutzer konnten nicht gefunden werden:%s"
Benutzer konnten nicht gefunden werden:%s

msgid
 ""

"
"Here is an example of how one might continue a very long string\n"
Here is an example of how one might continue a very long string\n
"
"for the common case the string represents multi-line output.\n"
for the common case the string represents multi-line output.\n
msgstr
 "Eine kommagetrennte Liste von Benutzernamen.\n"
Eine kommagetrennte Liste von Benutzernamen.\n
"
"Benutzer konnte nicht gefunden werden: %s"
Benutzer konnte nicht gefunden werden: %s

Array
(
    [0] => 
    [1] => #  translator-comments
    [2] => #. extracted-comments
    [3] => #: reference...
    [4] => #, flag...
    [5] => #| msgid previous-untranslated-string
    [6] => msgctxt "Dictionary form"
    [7] => msgid "Enter a comma separated list of user names."
    [8] => msgstr "Eine kommagetrennte Liste von Benutzernamen."
    [9] => 
    [10] => #: modules/user/views_handler_filter_user_name.inc:112
    [11] => msgid "Unable to find user: %s"
    [12] => msgctxt "german"
    [13] => msgid_plural "Unable to find users: %s"
    [14] => msgstr[0] "Benutzer konnte nicht gefunden werden: %s"
    [15] => msgstr[1] "Benutzer konnten nicht gefunden werden:%s"
    [16] => 
    [17] => msgid ""
    [18] => "Here is an example of how one might continue a very long string\n"
    [19] => "for the common case the string represents multi-line output.\n"
    [20] => msgstr "Eine kommagetrennte Liste von Benutzernamen.\n"
    [21] => "Benutzer konnte nicht gefunden werden: %s"
    [22] => 
)
Array
(
    [1] => Array
        (
            [cx] => Dictionary form
            [id] => Enter a comma separated list of user names.
            [tr] => Array
                (
                    [0] => Eine kommagetrennte Liste von Benutzernamen.
                )

        )

    [2] => Array
        (
            [id] => Unable to find user: %s
            [cx] => german
            [pl] => Unable to find users: %s
            [tr] => Array
                (
                    [0] => Benutzer konnte nicht gefunden werden: %s
                    [1] => Benutzer konnten nicht gefunden werden:%s
                )

        )

    [3] => Array
        (
            [id] => Here is an example of how one might continue a very long string\nfor the common case the string represents multi-line output.\n
            [tr] => Array
                (
                    [0] => Eine kommagetrennte Liste von Benutzernamen.\nBenutzer konnte nicht gefunden werden: %s
                )

        )

)


Create a new paste based on this one


Comments: