Re: A new kind of blocktrans that works with an unknown format string and a dictionary containing substitutes.

2012-12-21 Thread Bill Freeman
On Thu, Dec 20, 2012 at 7:18 AM, Kal Sze  wrote:

> Hello,
>
> While trying to develop my first real Django project, I have come across
> an *apparent* need to i18n'ize any arbitrary, unknown format string, with
> an accompanying dictionary that contains the substitutes to insert into the
> translated format string. For instance, I have a template that is displayed
> when a certain SomeError is raised:
>
> except SomeError as error:
> render_to_response('myapp/some_error.html',
>{ 'the_error': error },
>context_instance=RequestContext(request))
>
> I expect the SomeError object to contain information to be displayed to
> the user. Specifically, SomeError will have a format_string attribute and
> a substitute_dict attribute. The content of format_string is chosen at
> runtime, but it is assumed that translations exist. For instance,
> format_string could be:
>
> "{{user}}, you cannot upload this video because you are out of quota.
> You only have {{capacity}} out of {{quota}} left in your account."
>
> or it could be:
>
> "{{user}}, you cannot upload this video because it is in a format that
> we don't accept ({{video_format}})."
>
> Now assume that French, Spanish, Chinese, etc. translations exist for the
> two example strings above.
>
> And substitute_dict would contain the correct substitute strings. For
> instance:
>
> {
> 'user': 'Alice',
> 'capacity': '50 MB',
> 'quota': '20 GB',
> }
>
> or
>
> {
> 'user': 'Alice',
> 'video_format': '3ivx',
> }
>
> As you can imagine, even Django's built-in {% blocktrans %} tag doesn't
> seem to help here, for two reasons (as far as I understand):
>
>1. it requires the substitutes to be explicitly declared and bound to
>variables right in the template file;
>2. it requires the i18n string to be spelled out between {% blocktrans
>%} and {% endblocktrans %}; it cannot work with an i18n string that is
>itself inside a variable.
>
> Which means that I apparently need a new template tag, maybe something
> called {% blockdicttrans format_string substitute_dict %}.
>
> Is this madness? Have I overlooked some Django or Pythonic way? Or is it
> actually a justifiable use case?
>
> Cheers,
> Kal
>
>
> I will leave the answering of the meat of your question to someone who has
actually performed internationalization (other than to say that support
must be enabled in settings, and that there is a collection of mappings
that must be produced, as you surmise, in a per app sub directory
structure, IIUC).

However, presuming that your try/render code is copied, rather than being a
reduced summary, I would point out that render_to_response has a value
which must be 'return'ed from the view in order to have any effect.

Bill

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



A new kind of blocktrans that works with an unknown format string and a dictionary containing substitutes.

2012-12-20 Thread Kal Sze
Hello,

While trying to develop my first real Django project, I have come across an 
*apparent* need to i18n'ize any arbitrary, unknown format string, with an 
accompanying dictionary that contains the substitutes to insert into the 
translated format string. For instance, I have a template that is displayed 
when a certain SomeError is raised:

except SomeError as error:
render_to_response('myapp/some_error.html',
   { 'the_error': error },
   context_instance=RequestContext(request))

I expect the SomeError object to contain information to be displayed to the 
user. Specifically, SomeError will have a format_string attribute and a 
substitute_dict attribute. The content of format_string is chosen at 
runtime, but it is assumed that translations exist. For instance, 
format_string could be:

"{{user}}, you cannot upload this video because you are out of quota. 
You only have {{capacity}} out of {{quota}} left in your account."

or it could be:

"{{user}}, you cannot upload this video because it is in a format that 
we don't accept ({{video_format}})."

Now assume that French, Spanish, Chinese, etc. translations exist for the 
two example strings above.

And substitute_dict would contain the correct substitute strings. For 
instance:

{
'user': 'Alice',
'capacity': '50 MB',
'quota': '20 GB',
}

or

{
'user': 'Alice',
'video_format': '3ivx',
}

As you can imagine, even Django's built-in {% blocktrans %} tag doesn't 
seem to help here, for two reasons (as far as I understand):

   1. it requires the substitutes to be explicitly declared and bound to 
   variables right in the template file;
   2. it requires the i18n string to be spelled out between {% blocktrans %}
and {% endblocktrans %}; it cannot work with an i18n string that is 
   itself inside a variable.

Which means that I apparently need a new template tag, maybe something 
called {% blockdicttrans format_string substitute_dict %}.

Is this madness? Have I overlooked some Django or Pythonic way? Or is it 
actually a justifiable use case?

Cheers,
Kal

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/CcBuTPAHnd8J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.