Giter Site home page Giter Site logo

pherrymason / php-po-parser Goto Github PK

View Code? Open in Web Editor NEW
127.0 16.0 57.0 586 KB

Parse Gettext *.PO files with PHP

Home Page: http://raulferras.github.io/PHP-po-parser/

License: BSD 3-Clause "New" or "Revised" License

PHP 100.00%
po-files gettext parser

php-po-parser's Introduction

PoParser

PoParser is a personal project to fulfill a need I got: parse Gettext Portable files (*.po files) and edit its content using PHP.

PoParser requires PHP >= 5.4, but may work in 5.3 too.
Changelog

Latest Stable Version Total Downloads License Build Status Code Coverage Scrutinizer Quality Score

Gitter

Features

It supports following parsing features:

  • header section.
  • msgid, both single and multiline.
  • msgstr, both single and multiline.
  • msgctxt (Message context).
  • msgid_plural (plurals forms).
  • #, keys (flags).
  • # keys (translator comments).
  • #. keys (Comments extracted from source code).
  • #: keys (references).
  • #| keys (previous strings), both single and multiline.
  • #~ keys (old entries), both single and multiline.

Installation

composer require sepia/po-parser

Usage

<?php 
// Parse a po file
$fileHandler = new Sepia\PoParser\SourceHandler\FileSystem('es.po');

$poParser = new Sepia\PoParser\Parser($fileHandler);
$catalog  = $poParser->parse();

// Get an entry
$entry = $catalog->getEntry('welcome.user');

// Update entry
$entry = new Entry('welcome.user', 'Welcome User!');
$catalog->setEntry($entry);

// You can also modify other entry attributes as translator comments, code comments, flags...
$entry->setTranslatorComments(array('This is shown whenever a new user registers in the website'));
$entry->setFlags(array('fuzzy', 'php-code'));

Save Changes back to a file

Use PoCompiler together with FileSystem to save a catalog back to a file:

$fileHandler = new Sepia\PoParser\SourceHandler\FileSystem('en.po');
$compiler = new Sepia\PoParser\PoCompiler();
$fileHandler->save($compiler->compile($catalog));

Documentation

Testing

Tests are done using PHPUnit. To execute tests, from command line type:

php vendor/bin/phpunit

TODO

  • Add compatibility with older disambiguating contexts formats.

php-po-parser's People

Contributors

admad avatar ben-ho avatar chrisminett avatar dexif avatar drowe-wayfair avatar dsas avatar felixgilles avatar fmasa avatar gitter-badger avatar ivan-perez avatar newage avatar ondrejcech avatar particleflux avatar peter279k avatar pherrymason avatar scrutinizer-auto-fixer avatar splinter89 avatar stefaminator avatar vasekpurchart avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-po-parser's Issues

documentation is outdated

// Parse a po file
$poParser = new Sepia\PoParser();

for now PoParser constructor requires $handler

Add function getEntryByKey()

Pleace add new method in Sepia\PoParser\Catalog

public function getEntryByKey($key)
    {
        if (!isset($this->entries[$key])) {
            return null;
        }

        return $this->entries[$key];
    }

No license information

The source does not contain any license information, so I'm not sure under which terms I can use it.

Getting warning when updating multiline text

explode() expects parameter 2 to be string, array given in poparser.php on line 306

This seems to only occur when the text is multiline, it seems the parser is not as per gettext multiline, below is an example of multiline po file generated using xgettext

msgid ""
"With my listing I describe it that I am aware of all the necessary "
"information about Camp Exercises and confirmed that I make myself aware "
"that they involve a lot of physical activity. I also declare that I consider "
"myself a sufficiently good physical and mental condition to deal with "
"Camp exercise. I do it at your own risk. As a participant AFSL myself all "
"rights to claim damages from Pvt. or coaches Camp because of "
"injury, illness or accident that I might be in training."
msgstr ""

Below is my current code

$poparser = new I18n_Pofile();
$poparser->read( 'po-file.po' );
$poparser->update_entry( 'Email', 'Netfang' );
$poparser->write( 'po-file.po' );

Let me know if I m doing something wrong.

I catch the Exception

Uncaught exception 'Exception' with message 'PoParser: Parse error! Unknown key "#|" on line #| msgid "bla-bla-bla"'

How to reproduce (I hope so) (on Mac OS in my case):

  1. xgettext on php-file with get text call in it. Save output into pot-file.
  2. msginit it into po-file.
  3. Translate it.
  4. Change msgid in php-file.
  5. Repeat first step and recreate pot-file.
  6. Use msgmerge to merge pot-file into existing po-file.

Now the message you changed should have fuzzy flag and "#|" comment with previous translation.

  1. Remove msgid from php-file. Repeat steps 5 and 6.

Now the message you removed has been marked as obsolete, so "#|" previous comment receives obsolete flag too.

P.S.
And yes, I didn't find any mentions in the official documentation, but as you can see the comment was generated by the official utilities.

Parser doesn't remove slashes before quotes

Hi,

I believe that when parsed, the slashes should not be part of the string because it's an escape character.

msgid "a\"b\"c"
msgstr "quotes in \"translation\""

$entry->getMsgId() // a"b"c
$entry->getMsgStr() // quotes in "translation"

But you specifically test for that behavior in ReadPoTest::testBasic. Is there any reason for that? Am I missing something?

Compiler wrapping results in buggy po-file

The compilers default wrapping is: 80 chars.

If char 80+81 are an escaped double quote \" it results in an buggy line.

Original (valid) lines in po-file:

msgid ""
"Hello everybody, Hello ladies and gentlemen..... this is a line with more than \"eighty\" chars. And char eighty is an escaped double quote."
msgstr ""
"Hello everybody, Hello ladies and gentlemen..... this is a line with more than \"eighty\" chars. And char eighty is an escaped double quote."

After parsing and compiling the po-file the entry looks like this:

msgid ""
"Hello everybody, Hello ladies and gentlemen..... this is a line with more than \"
"\"eighty\" chars. And char eighty is an escaped double quote."
msgstr ""
"Hello everybody, Hello ladies and gentlemen..... this is a line with more than \"
"\"eighty\" chars. And char eighty is an escaped double quote."

It seems to be the case that wrapString simply works with substr and does not care about the fact that the string is croped in the middle of an escape sequence.

Yes, I will create a test to reproduce this issue.

Insert new entry

It's not possible to add a new entry to a loaded po-file.
This function is already available in mier85@c1366ed

Do you plan to merge this code or implement this function on your own?

Best Regards,
Ben-Ho

How to update multi-line entries?

I have a problem with multiline entries (for example:

  #: app/views/serviceReports.html
  msgid ""
  "A duration must be in the form 'hh' or 'hh:mm'"
  ", and at least one minute long"
  msgstr ""

).
This is the way I'm trying to update entries:

    $language = "it"; # for example
    $poparser = new Sepia\PoParser();
    $entries = $poparser->parse("$language.po");
    foreach ($entries as $entry) {
      for ($i = 0; isset($entry['msgid'][$i]); $i++) {
        $src = $entry['msgid'][$i];
        $dst = $entry['msgstr'][$i];
        if ($src && !$dst) { // no translation: translate it
          $dst = translate($src, "en", $language);
          $poparser->updateEntry($src, $dst);
        }
      }
    }
    $poparser->write("$poDirectory/$language-translated.po");

but $msgstr[$i] is reported to be "undefined offset", for $i > 0, unsurprisingly...

Please, some advice updating multi-line entries...

Po Parser skips first msgid

po file http://pastebin.com/dzbEXb3N

I believe you deliberately skip first msg as to headers [line 57 to 62]

I dont know what are specifications for po file but I believe if we find msgid even incase first_line flag is true we should make it false.

Again just a thought not an expert of po files.

The problem becomes bigger problem if you try updating, it keeps chewing first appearing msgid, number of updats that much msgid lost

php 7.4 compatibility issue

with following lines

if ($x[0] == '"') { $x = substr($x, 1, -1); }
error: trying to access array offset on value of type bool ($x)
PoParser.php line 755

Notice Error: Undefined index

I saw this:
Notice Error: Undefined index: in [PHP-po-parser\Sepia\PoParser.php, line 304]

and I tracked it down to the following PO file entry:

msgid ""
msgstr ""
"Project-Id-Version: 1.0\n"
"POT-Creation-Date: \n"
"PO-Revision-Date: 2015-09-22 15:58+0300\n"
"Language-Team: Support <[email protected]>\n"
"Language: en\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"

#index.ctp:101
msgid "test"
msgstr "test"

msgfmt fails to convert .po to .mo

Hi, when I try to convert produced .po to .mo via msgfmt Locale/cs_CZ/LC_MESSAGES/films.po -o Locale/cs_CZ/LC_MESSAGES/films.mo I get error:

Locale/cs_CZ/LC_MESSAGES/films.po:3: keyword "Project" unknown
Locale/cs_CZ/LC_MESSAGES/films.po: warning: Charset missing in header.
                                            Message conversion to user's charset will not work.
Locale/cs_CZ/LC_MESSAGES/films.po:3:8: syntax error
Locale/cs_CZ/LC_MESSAGES/films.po:4: keyword "Last" unknown
Locale/cs_CZ/LC_MESSAGES/films.po:5: keyword "Language" unknown
Locale/cs_CZ/LC_MESSAGES/films.po:6: keyword "Language" unknown
Locale/cs_CZ/LC_MESSAGES/films.po:7: keyword "MIME" unknown
Locale/cs_CZ/LC_MESSAGES/films.po:8: keyword "Content" unknown
Locale/cs_CZ/LC_MESSAGES/films.po:9: keyword "Content" unknown
msgfmt: found 8 fatal errors

When I compare the first lines from msginit and po-parser, I see the lines are not wrapped in strings ""

$ head -n 14 Locale/cs_CZ/LC_MESSAGES/films.po 
msgid ""
msgstr ""
Project-Id-Version: PACKAGE VERSION
Last-Translator: Automatically generated
Language-Team: none
Language: =cs_CZ
MIME-Version: 1.0
Content-Type: text/plain; charset=ASCII
Content-Transfer-Encoding: 8bit

#: lib/model/evPersonMergeFields.php:1312
msgid "(No screenings are available)"
msgstr ""

I am using 5.1.1 version

I produce the file (if missing) via this command:
shell_exec("msginit --no-translator --input={$templateFile} --locale ={$locale}.UTF-8 --output={$poFile}");

and it looks like:

head -n 14 Locale/cs_CZ/LC_MESSAGES/films.po
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
"Language: =cs_CZ\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: lib/model/evPersonMergeFields.php:1312
msgid "(No screenings are available)"
msgstr ""

So the quotes are lost when i save the catalog to the file

Propose: Changing API

Due to #19 discovering new keys that seem to mean a combination of other keys, I've though of changing the name I give to each key inside an entry and use the original names in specification.

So this:
tcomment -> #
ccomment -> #.
reference -> #:
previous -> #|
obsolete -> #~

I would like to know your thoughts on this as I'm not pretty sure, it is useful to not having to remember what means each #X but as I said @Cellard discovered a new entry which seems to mean previous-obsolete entry. If there are more keys like this, key naming will become messy.

How do I get the msgstr[0] type entries set?

I've tried a plethora of things, can't file anything in the tests that generates the right output when compiled.

$new_parser->setEntry( $key,[
   'msgid' => $msgid,
   'msgid_plural' => $new_entries[$key]['msgid_plural'],
   .. what else?
]);

I'm trying to achieve this type of output:

msgid "test"
msgid_plural "There are %count% values in the house"
msgstr[0] ""
msgstr[1] ""

Nothing I pass populates the plural forms..

Thanks for your kind help!

Quote is adding on very big string

Hello , i'm using your package for editing po files , i'm having some field example:'X' and i'm having very big another field example:'Y' , if i'm editing X field, Y field's messageId is automatically updating and adding some '/' and quotes between the big string . how can i restrict this ???? can u please help me in this ??

Orginal File Before Edit :

image

After Edit :
image

Please check this screenshot , automatically '/' and quote is adding while editing and also line is breaking and creating a newline..

Multiline headers

Is it possible to build in better parser for the headers?
Especially for Plural-Forms.

For example for russian language we have somethingf like:

"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);\n"

So it would be nice to have somehow the access to proper string by the header-key.

Error on compiling obsolete plurals

msgid_plural and msg_str[] are missing the obsolete prefix (#~)

This produces broken .po-files:

#~ msgid "Mutter"
msgid_plural "Mütter"
msgstr[0] "мать"
msgstr[1] "матери"
msgstr[2] "матери"

Please fix it by prepending the TOKEN_OBSOLETE when the entry is obsolete in buildMsgIdPlural and buildMsgStr methods in PoCompiler.php.

It try it by myself now and try to send you an Pull request.

"Notice Error: Undefined index" when having excess lines

I'm getting:
Notice Error: Undefined index: in [sepia\po-parser\src\Sepia\PoParser.php, line 304]

when you po file looks like this:

msgid ""
msgstr ""
"Project-Id-Version: 1.0\n"
"POT-Creation-Date: \n"
"PO-Revision-Date: 2015-09-22 15:58+0300\n"
"Language-Team: Support <[email protected]>\n"
"Language: en\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
msgid "xxxx"
msgstr ""

"yyyy"
"zzzz"

which according to msgfmt --check is valid

Output from write() is ISO-8859, and poedit refuses opening it...

I have a problem, here: when I write a po file with Sepia\PoParser write() method, I get an ISO-8859 encoded text, while PoEdit seems to dislike it, refusing to open: ("Failed to convert file ... to UNICODE").
This is my simple code, if it can be of any help (I am building an auto-translator tool, to simplify manual editing...):

$language = "fr"; # for example
$poparser = new Sepia\PoParser();
$entries = $poparser->parse("$language.po");
  foreach ($entries as $entry) {
    $src = $entry['msgid'][0];
    $dst = $entry['msgstr'][0];
    if ($src && !$dst) { // no translation: translate it
      $dst = translate($src, "en", $language);
      $poparser->updateEntry($src, $dst);
    }
  }
  $poparser->write("$language-translated.po");
  /* Sepia PoParser writes iso-8859-1, convert to UTF8 */
}

fr.po:            GNU gettext message catalogue, ASCII text
fr-translated.po: GNU gettext message catalogue, ISO-8859 text

My locale is en_US.UTF-8:

$ echo $LANG
en_US.UTF-8

Any clue? What am I missing?

Add support of #| comments

Here are examples from official manual

| msgctxt previous-context

| msgid previous-untranslated-string

| msgid previous-untranslated-string-singular

| msgid_plural previous-untranslated-string-plural

Multiline po file (question)

I have a po file:

msgid ""
"%user% acaba de responder tu comentario.<br>Consulta que te ha "
"dicho %link%aquí</a>."
msgstr ""
"%user% acaba de respondre el teu comentari.<br>Consulta que t'ha "
"dit %link%aquí</a>."

This is the result:

array(1) {
  '%user% acaba de responder tu comentario.<br>Consulta que te ha dicho %link%aquí</a>.' =>
  array(2) {
    'msgid' =>
    array(3) {
      [0] =>
      string(0) ""
      [1] =>
      string(63) "%user% acaba de responder tu comentario.<br>Consulta que te ha "
      [2] =>
      string(22) "dicho %link%aquí</a>."
    }
    'msgstr' =>
    array(3) {
      [0] =>
      string(0) ""
      [1] =>
      string(65) "%user% acaba de respondre el teu comentari.<br>Consulta que t'ha "
      [2] =>
      string(20) "dit %link%aquí</a>."
    }
  }
}

Only the array key is a concatenated msgid. Is it intented?

Would not be better to concatenate all the things? Is there a case when we might need this "raw" form?

Shouldn't the "reference" key be an array?

I have not used your script yet but based on your example, I noticed they the "reference" key is not an array. I noticed that sometimes my PO files have multiple references.

Saving file with escaped qoutes causes excess of backslash characters "\"

index.php

file_put_contents('original.po', file_get_contents('_original.po'));

$original = new Sepia\PoParser\SourceHandler\FileSystem('original.po');

$poParser = new Sepia\PoParser\Parser($original);
$catalog  = $poParser->parse();

$compiler = new Sepia\PoParser\PoCompiler();
$original->save($compiler->compile($catalog));

_original.po

msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"POT-Creation-Date: 2018-03-05 08:57+0100\n"
"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\n"
"Last-Translator: NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <EMAIL@ADDRESS>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"

msgid "a\nb\nc"
msgstr "linebreaks"

msgid "a\"b\"c"
msgstr "quotes"

And just create an empty file called original.po

After running index.php the original.po will contain too many backslashes, example:

msgid "a\\nb\\nc"
msgstr "linebreaks"

msgid "a\\\"b\\\"c"
msgstr "quotes"

If you remove the first line in index.php which copys the _original.po content, the amount of backslashes will multiply everytime you run it.

unsafe splitting of UTF-8 strings

Hi, today I've added czech translation which gets split after 80 bytes which is exactly in the middle of UTF-8 multibyte sequence, thus producing non-ascii file.

Slučovací pole v této sekci budou fungovat pouze v případě, že v Hromadné korespondenci vyberet <b>typ příjemce - Firma</b>

It is split as (notice " and rest is on new line ")

msgstr ""
"Slu<C4><8D>ovac<C3><AD> pole v t<C3><A9>to sekci budou fungovat pouze v p<C5><99><C3><AD>pad<C4><9B>, <C5><BE>e v Hromadn<C3>"
"<A9> korespondenci vyberet <b>typ p<C5><99><C3><AD>jemce - Firma</b>"

It would be nice if it either splitted safely, or we had an option to not split lines at all.

Broken wrapping for multibyte characters

After updating from 5.1.6 I noticed that dumped files started wrapping, which was very surprising for me, because I detect the length of the longest string and set that as wrapping width in order to avoid wrapping altogether.

After going through the changes I identified, that it was broken by #92 even though there are multibyte tests.

At first I noticed it in long strings, but was able to reproduce it even with just two words: https://3v4l.org/U4e0Z

$string = 'kategóriáját kötelező';
var_dump(mb_strlen($string));
var_dump(strlen($string));
var_dump(wordwrap($string, 21, " \n"));
var_dump(wordwrap($string, 25, " \n"));
var_dump(wordwrap($string, 26, " \n"));

outputs:

int(21)
int(26)
string(27) "kategóriáját 
kötelező"
string(27) "kategóriáját 
kötelező"
string(26) "kategóriáját kötelező"

So it is quite obivous, that the wordwrap function does not work with multibyte characters correctly and I do not think that this is much of a surprise, since all (or most) of the PHP functions do not, if they are not from the mb_ family.

I can try to come up with a PR, but let me know if there is interest for it, I do not know how active this package is, so to avoid wasting time :)

So confusing to use

I do appreciate this, but it is difficult to use. It lacks instructions and demos. You have code in your main file that does not exists nor works.

$pofile->updateEntry( $msgid, $msgstr ); (?) But no function in any of your files?
$pofile->write('ca.po'); returns Fatal error: Call to undefined method Sepia\PoParser::write()
So basically we can use this to parse and translate a file, but not save it?

No examples of using plurals. I see a function, but no example use.
I see mulitine, but no example.

The parsing is great, I just have to find a different method of updating/changing the text.

Latest tags aren't on Packagist

For some reason the tags in the 4.0 series are not on Packagist, maybe the webhook is broken or something, could you update them ?

Write wrapped messages back

It looks like by default the strings will be wrapped. Like msgid and translated strings msgstr. Poedit and the unix-command msgmerge write the long strings wrapped. It would be good, if it will be possible to set the additional parameter somewhere, like in the writeFile method. to wrap long strings.

I wrote thid code for my project:

if (is_array($msgstr)) {
	foreach ($msgstr as $n => $mstr) {
		$entry['msgstr[' . $n . ']'] = [];

		$mstr = trim($mstr);

		$subentries = explode("\n", wordwrap($mstr, 75));
		if (count($subentries) == 1) {
			$entry['msgstr[' . $n . ']'][] = $mstr;
		} else {
			$entry['msgstr[' . $n . ']'][] = '';
			foreach ($subentries as $nr => $subentry) {
				$entry['msgstr[' . $n . ']'][] = $subentry . ($nr < count($subentries) - 1 ? ' ' : '');
			}
		}
	}
}

By the way, I have no idea, why in case of multiline (in the .po file) the first entry is the empty string:

#: stuff/xxx.php:12
#, php-format
msgid ""
"%s yyy1 sdf jsdlf slfd lsfd lfj lfdg lfdg dlfgj dlfgj dlgj fdlgjdfl gfdlgj "
"dfgj dflgj dlj glfdg lglk jldfgj fdklgj fdlgj fdgjgl dfgl jglkjg lfdjg "
"lsfdjskl dg 111111111"
msgid_plural ""
"%s yyy2 sdf jsdlf slfd lsfd lfj lfdg lfdg dlfgj dlfgj dlgj fdlgjdfl gfdlgj "
"dfgj dflgj dlj glfdg lglk jldfgj fdklgj fdlgj fdgjgl dfgl jglkjg lfdjg "
"lsfdjskl dg 2222222222"
msgstr[0] "%s 111aa"
msgstr[1] ""
"%s 222 f j k l l o k j h j h k k j h z f j k l l o k j h j h k k j h z f j "
"k l l o k j h j h k k j h z f j k l l o k j h j h k k j h z f j k l l o k j "
"h j h k k j h z f j k l l o k j h j h k k j h z f j k l l o k j h j h k k "
"kkk"
msgstr[2] ""
"%s 3333 f fdg fkgj fdjg fdkjg dlfjg fdlkj fdljdklg jfdlgj dklgj ljdflg "
"jfdlgj dflgj fdkljf glkfdjg kfdgj dkljfdl gfdkg fkgj fdlgj flgkj sklgj "
"fdklgj fdklgj sfdkl gjsfdklgjsfdklj gjfdkg sdlk33333333a aa"

Propose refactor

I propose refactor follow SOLID.
Need create different objects:

  • Reader - contain read and write methods. For example DbReader.
  • Writer - like reader. We will have possible write to Db. Don't need realize other reader and writer.
  • Parser - contain .po parser. Must work after read and before writer. We will have possible use other parser. For example Python parser or console tools. Parser can return: collection or array.
  • Entity - contain object for keep one message structure: msgid, msgstr, comments ...
  • Collection - collection for entities. Can provide methods for search msgid in entities.
  • Hydrator - for can convert collection to array and array to collection with entities.

It's allow more flexible structure.

Any suggestions?

Optional whitespace before entity

As it written in https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html the whitespace before entity is optional.

A .po file with the following content:

msgid "one"
msgstr "uno"
msgid "two"
msgstr "dos"

contains two entities. But the parsing result is:

array (
  'one,two' => array(
    'msgid' => array(
      0 => 'one',
      1 => 'two',
    ),
    'msgstr' => array(
      0 => 'uno',
      1 => 'dos',
    ),
  ),
);

instead of:

array (
  'one' => array(
    'msgid' => array(
      0 => 'one',
    ),
    'msgstr' => array(
      0 => 'uno',
    ),
  ),
  'two' => array(
    'msgid' => array(
      0 => 'two',
    ),
    'msgstr' => array(
      0 => 'dos',
    ),
  ),
);

How to fetch source string?

OK, it is possible to fetch translation for source with:

$entry = $catalog->getEntry('welcome.user');

but how to fetch source for translation?

Thanks,
M

Add entry

Hi Raul :

Have a question, Why is not posible to delete a entry in the parser class.

Thanks great work btw

Francisco

Parsing plural forms

I can't see how to parse translations for plural forms, when taking wrapped strings into account Here's the output I get when I print_r after calling parseFile on the attached test.po.tar.gz

Array
(
    [%1$d UK region selected ] => Array
        (
            [msgid] => Array
                (
                    [0] => %1$d UK region selected 
                )

            [msgid_plural] => Array
                (
                    [0] => 
                    [1] => %1$d UK regions 
                    [2] => selected
                )

            [msgstr] => Array
                (
                    [0] => %1$d HX ertvba fryrpgrq
                    [1] => 
                    [2] => %1$d HX ertvbas 
                    [3] => fryrpgrq
                )

        )

)

So msgstr contains the case for each plural form, the first (singular form) is in index 0 and the rest is the plural form. I can't programatically know this though.

The obvious work around is to not use string wrapping, though that is the default for the gnu gettext suite...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.