views:

71

answers:

2

I have an app that I'm migrating portions of to Django, but python and php have a different string format. E.g.

"Hello %1s" in php vs. "Hello {0}" or "Hello {name}" in python.

We'll be maintaining both apps for a while, but is there a way to use the python format in PHP or vice versa?

A: 

PHP's gettext doesn't expand/substitute %s - this is done by output functions (e.g. printf). It also appears to be the case in Python.

Most importantly, in Python you can use %s to represent strings, see http://docs.python.org/library/stdtypes.html#string-formatting

So you should be able to use %s-style strings in both.

chronos
That's part of sprintf to order the arguments:http://php.net/manual/en/function.sprintf.php
dd
you are correct - I've never used order specifiers, so wrongly assumed these do not exist :)
chronos
Have you had a look at %s in Python?
chronos
Yep, and I've used '%s' before, but then we'd lose ordering - which for us would be important - but it may work for some strings.
dd
Can you specify string values in the order they are used in the format string, so as to avoid using ordering modifiers at all? I do not know if Django imposes any limits on that.An alternative solution could be to use $-style variables in-string in PHP - i.e. _("Hello $1!") - and to regex-replace all $([\d]+) occurrences to {\1} for Python. This would be inefficient and extra verbose, but other than that I can't see any other solution right now.
chronos
A: 

Would something like this php PECL extension help you?

http://pecl.php.net/package/python

Regards.

mediaslave