Accessing language and translation items

I have two questions on language and translation issues and would be grateful for some hints:

  1. How can I get the date format string for the current language within a module and within a proteus script stored in the the database table “ir_lang” (field “date”)?
  2. Can I store translatations in the locale/*.po files for items, which are not connected to fields, buttons, views and so on? I want to store translations for two terms (English “Order”, “Delivery” and German “Bestellung”, “Lieferung”) and access them from module and proteus script to use them in a string, which will be added to invoice.lines as a comment.
Lang = pool.get('ir.lang')
lang = Lang.get()
config = proteus.config.set_trytond(...()

The locale content should only contain file that were generated by the translation wizards.

You can create record in ir.message and get there translated version using trytond.i18n.gettext.

proteus script does not provide any API for that because its goal is to behave like a client. So if you can not do it in the UI client, you can not with proteus.

But maybe instead of using proteus script, you should write script for trytond-console.

Thank’s, it works fine!

Here is my implentation for creating and translating the two terms (English “Order”, “Delivery” and German “Bestellung”, “Lieferung”), for everyone who has the same issue.

Assume the module name is sort_invoice. The xml file sort_invoice.xml is the place to create the records for ir.message:

    <record model="ir.message" id="msg_sort_invoice_order">
      <field name="text">Order from</field>
    <record model="ir.message" id="msg_sort_invoice_delivery">
      <field name="text">Delivery from</field>

The file locale/de.po contains the translations (here in German):

msgctxt "model:ir.message,text:msg_sort_invoice_order"
msgid "Order from"
msgstr "Bestellung vom"

msgctxt "model:ir.message,text:msg_sort_invoice_delivery"
msgid "Delivery from"
msgstr "Lieferung vom"

In the module the translation can be accessed with:

from trytond import i18n
order_msg = i18n.gettext('sort_invoice.msg_sort_invoice_order')
delivery_msg = i18n.gettext('sort_invoice.msg_sort_invoice_delivery')

Pay attention to the Parameter of gettext. The documentation of trytond.i18n.gettext is a bit unprecise. The message_id of the xml file has to be supplemented by the module name in front.

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.