Build a Gettext Wrapper

297 views Asked by At

These days I'm working on two projects based on laravel and I have to implement the localization. So I found out that the most used method is using Gettext. I have some difficulties translating strings with only gettext() and ngettext(). I'd build a wrapper able to translate strings like so:

fantasticFunction('Hi my name is :name and I have {n, one friend, :n friends}', [ 'name' => 'Luca', 'n' => 1 ])

Instead of writing:

sprintf('Hi my name is %s and I have', name) . ' ' . $n . ngettext('friend', 'friends', $n)

so the problem is: if I'd make such as wrapper, then tools like poedit would not recognize that strings. How can I solve?

1

There are 1 answers

0
Václav Slavík On

You need to add fantasticFunction as a keyword for extraction — this is well documented in xgettext docs and Poedit has properties UI for it.

But nothing will help you with that {n, one friend, :n friends} thingie. If you decided to use gettext, you need to actually follow its model and this isn't compatible with it. Do have a look at how ngettext works and read up on plural forms in different languages in gettext before inventing your own approach. You're vague about your "difficulties" with using gettext's plurals support (which is generally speaking competently done), are you sure it isn't just a case of NIH syndrome?