[gettext] provide GNU contexts to avoid translations ambiguities 3.5
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Wed, 16 Sep 2009 21:29:17 +0200
branch3.5
changeset 3275 5247789df541
parent 3274 7d53d8491932
child 3278 293068aeee41
[gettext] provide GNU contexts to avoid translations ambiguities
common/i18n.py
cwconfig.py
dbapi.py
devtools/devctl.py
schema.py
server/session.py
web/formfields.py
web/request.py
web/views/formrenderers.py
--- a/common/i18n.py	Wed Sep 16 21:23:35 2009 +0200
+++ b/common/i18n.py	Wed Sep 16 21:29:17 2009 +0200
@@ -25,10 +25,14 @@
     output.close()
 
 
-def add_msg(w, msgid):
+def add_msg(w, msgid, msgctx=None):
     """write an empty pot msgid definition"""
     if isinstance(msgid, unicode):
         msgid = msgid.encode('utf-8')
+    if msgctx:
+        if isinstance(msgctx, unicode):
+            msgctx = msgctx.encode('utf-8')
+        w('msgctxt "%s"\n' % msgctx)
     msgid = msgid.replace('"', r'\"').splitlines()
     if len(msgid) > 1:
         w('msgid ""\n')
--- a/cwconfig.py	Wed Sep 16 21:23:35 2009 +0200
+++ b/cwconfig.py	Wed Sep 16 21:29:17 2009 +0200
@@ -812,7 +812,7 @@
             self.info("loading language %s", language)
             try:
                 tr = translation('cubicweb', path, languages=[language])
-                self.translations[language] = tr.ugettext
+                self.translations[language] = (tr.ugettext, tr.upgettext)
             except (ImportError, AttributeError, IOError):
                 self.exception('localisation support error for language %s',
                                language)
--- a/dbapi.py	Wed Sep 16 21:23:35 2009 +0200
+++ b/dbapi.py	Wed Sep 16 21:29:17 2009 +0200
@@ -214,10 +214,13 @@
             self.lang = 'en'
         # use req.__ to translate a message without registering it to the catalog
         try:
-            self._ = self.__ = self.translations[self.lang]
+            gettext, pgettext = self.translations[self.lang]
+            self._ = self.__ = gettext
+            self.pgettext = pgettext
         except KeyError:
             # this occurs usually during test execution
             self._ = self.__ = unicode
+            self.pgettext = lambda x,y: y
         self.debug('request default language: %s', self.lang)
 
     def decorate_rset(self, rset):
--- a/devtools/devctl.py	Wed Sep 16 21:23:35 2009 +0200
+++ b/devtools/devctl.py	Wed Sep 16 21:29:17 2009 +0200
@@ -121,6 +121,7 @@
         entities = [e for e in schema.entities() if not e in libschema]
     else:
         entities = schema.entities()
+    rinlined = uicfg.autoform_is_inlined
     done = set()
     for eschema in sorted(entities):
         etype = eschema.type
@@ -129,8 +130,14 @@
         if not eschema.is_final():
             add_msg(w, 'This %s' % etype)
             add_msg(w, 'New %s' % etype)
-            add_msg(w, 'add a %s' % etype)
-            add_msg(w, 'remove this %s' % etype)
+            for rschema, targetschemas, role in eschema.relation_definitions(True):
+                targetschemas = [tschema for tschema in targetschemas
+                                 if rinlined.etype_get(eschema, rschema, role, tschema)]
+                for tschema in targetschemas:
+                    add_msg(w, 'add a %s' % tschema,
+                            'inlined:%s.%s.%s' % (etype, rschema, role))
+                    add_msg(w, 'remove this %s' % tschema,
+                            'inlined:%s:%s:%s' % (etype, rschema, role))
         if eschema.description and not eschema.description in done:
             done.add(eschema.description)
             add_msg(w, eschema.description)
@@ -143,7 +150,8 @@
         relations = schema.relations()
     for rschema in sorted(set(relations)):
         rtype = rschema.type
-        add_msg(w, rtype)
+        for subjschema in rschema.subjects():
+            add_msg(w, rtype, subjschema.type)
         done.add(rtype)
         if not (schema.rschema(rtype).is_final() or rschema.symetric):
             add_msg(w, '%s_object' % rtype)
@@ -157,7 +165,7 @@
         if eschema.is_final():
             continue
         for role, rschemas in (('subject', eschema.subject_relations()),
-                            ('object', eschema.object_relations())):
+                               ('object', eschema.object_relations())):
             for rschema in rschemas:
                 if rschema.is_final():
                     continue
--- a/schema.py	Wed Sep 16 21:23:35 2009 +0200
+++ b/schema.py	Wed Sep 16 21:29:17 2009 +0200
@@ -100,7 +100,7 @@
         etype = ETYPE_NAME_MAP[etype]
     return etype
 
-def display_name(req, key, form=''):
+def display_name(req, key, form='', context=None):
     """return a internationalized string for the key (schema entity or relation
     name) in a given form
     """
@@ -111,7 +111,11 @@
         key = key + '_' + form
     # ensure unicode
     # added .lower() in case no translation are available
-    return unicode(req._(key)).lower()
+    if context:
+        return req.pgettext(context, key).lower()
+    else:
+        return unicode(req._(key)).lower()
+
 __builtins__['display_name'] = deprecated('display_name should be imported from cubicweb.schema')(display_name)
 
 def ERSchema_display_name(self, req, form=''):
--- a/server/session.py	Wed Sep 16 21:23:35 2009 +0200
+++ b/server/session.py	Wed Sep 16 21:29:17 2009 +0200
@@ -210,13 +210,18 @@
         vreg = self.vreg
         language = language or self.user.property_value('ui.language')
         try:
-            self._ = self.__ = vreg.config.translations[language]
+            gettext, pgettext = vreg.config.translations[language]
+            self._ = self.__ = gettext
+            self.pgettext = pgettext
         except KeyError:
             language = vreg.property_value('ui.language')
             try:
-                self._ = self.__ = vreg.config.translations[language]
+                gettext, pgettext = vreg.config.translations[language]
+                self._ = self.__ = gettext
+                self.pgettext = pgettext
             except KeyError:
                 self._ = self.__ = unicode
+                self.pgettext = lambda x,y: y
         self.lang = language
 
     def change_property(self, prop, value):
--- a/web/formfields.py	Wed Sep 16 21:23:35 2009 +0200
+++ b/web/formfields.py	Wed Sep 16 21:29:17 2009 +0200
@@ -513,6 +513,7 @@
         help = rschema.rproperty(targetschema, eschema, 'description')
     kwargs['required'] = card in '1+'
     kwargs['name'] = rschema.type
+    kwargs['label'] = (eschema.type, rschema.type)
     kwargs.setdefault('help', help)
     if rschema.is_final():
         if skip_meta_attr and rschema in eschema.meta_attributes():
--- a/web/request.py	Wed Sep 16 21:23:35 2009 +0200
+++ b/web/request.py	Wed Sep 16 21:29:17 2009 +0200
@@ -114,7 +114,8 @@
         self.set_default_language(vreg)
 
     def set_language(self, lang):
-        self._ = self.__ = self.translations[lang]
+        gettext, self.pgettext = self.translations[lang]
+        self._ = self.__ = gettext
         self.lang = lang
         self.cnx.set_session_props(lang=lang)
         self.debug('request language: %s', lang)
--- a/web/views/formrenderers.py	Wed Sep 16 21:23:35 2009 +0200
+++ b/web/views/formrenderers.py	Wed Sep 16 21:29:17 2009 +0200
@@ -87,7 +87,10 @@
     def render_label(self, form, field):
         if field.label is None:
             return u''
-        label = self.req._(field.label)
+        if isinstance(field.label, tuple): # i.e. needs contextual translation
+            label = self.req.pgettext(*field.label)
+        else:
+            label = self.req._(field.label)
         attrs = {'for': form.context[field]['id']}
         if field.required:
             attrs['class'] = 'required'
@@ -485,7 +488,7 @@
 
     def inline_relation_form(self, w, form, rschema, targettype, role):
         entity = form.edited_entity
-        __ = self.req.__
+        __ = self.req.pgettext
         w(u'<div id="inline%sslot">' % rschema)
         existant = entity.has_eid() and entity.related(rschema)
         if existant:
@@ -513,8 +516,9 @@
                 entity.eid, targettype, rschema, role)
             if card in '1?':
                 js = "toggleVisibility('%s'); %s" % (divid, js)
+            ctx = 'inlined:%s.%s.%s' % (entity.e_schema, rschema, role)
             w(u'<a class="addEntity" id="add%s:%slink" href="javascript: %s" >+ %s.</a>'
-              % (rschema, entity.eid, js, __('add a %s' % targettype)))
+              % (rschema, entity.eid, js, __(ctx, 'add a %s' % targettype)))
             w(u'</div>')
             w(u'<div class="trame_grise">&#160;</div>')
         w(u'</div>')