ugettext: How to deal with variables within a sentence?

from django.utils.translation import ugettext_lazy as _

_(u"I am off to school at '%s' o'clock" % time)

This is kind of odd, since I would get a whole line to translate like this

"I am off to school at \'%s\' o'clock"

Now if the translator removes the \'%s\' by mistake, it would break the code.

Should I better separate the sentence into two parts? But this might give the translator trouble to understand the context of the sentence.

_(u"I am off to school at ") + '%s' + _(u"o'clock") % time

Is there a better approach to this?


If you make use of named string interpolation as opposed to positional string interpolation, this should protect you from an exception if the translator forgets one of the parameters from his translated string.

An example from the django docs:

def my_view(request, m, d):
    output = _('Today is %(month)s %(day)s.') % {'month': m, 'day': d}
    return HttpResponse(output)

Note the dictionary of {'name': 'value'} pairs to be used in string substitution.

For this reason, you should use named-string interpolation (e.g., %(day)s) instead of positional interpolation (e.g., %s or %d) whenever you have more than a single parameter. If you used positional interpolation, translations wouldn't be able to reorder placeholder text.

(django docs)

Need Your Help

Scala / SBT - dependency not resolved on PaaS (cloudControl & heroku)

scala heroku sbt cloudcontrol stormpath

I have a simple Scala application built using I am using Scala 2.11.2 and SBT 0.13.0. All of my dependencies are listed here:

Atomic instructions and atomic type

c++ c++11

Are there types which I can read or modify or compare atomically by default?