README.rst 2.14 KB
Newer Older
Florent Cayré's avatar
Florent Cayré committed
1
2
Summary
-------
Florent Cayré's avatar
Florent Cayré committed
3
4
5
6
7
8
9

This cube provides a way to manage and display entity field (attribute)
translations, stored in the application database.

To achieve this for your particular entity type, you have to:

* add an 'i18nfield_of' relation definition, linking from the 'I18nField'
Florent Cayré's avatar
Florent Cayré committed
10
  entity type to your translatable entity type(s)
Florent Cayré's avatar
Florent Cayré committed
11
12

* add a 'ref_lang' relation definition, linking from your entity types
13
  to the 'I18nLang' entity type
Florent Cayré's avatar
Florent Cayré committed
14
15

* make your business entity type inherit the 'TranslatableEntityMixin' and
Florent Cayré's avatar
Florent Cayré committed
16
  list its translatable fields in its 'i18nfields' attribute
Florent Cayré's avatar
Florent Cayré committed
17
18

* create a simple adapter that inherits '_TranslatableEntityAdapter' and is
Florent Cayré's avatar
Florent Cayré committed
19
  selectable for your translatable entity types
Florent Cayré's avatar
Florent Cayré committed
20

21
That's all for the code part. Just add at least one I18nLang instance in your
Florent Cayré's avatar
Florent Cayré committed
22
23
24
25
26
27
28
29
30
31
database and link your translatable entities to their reference language.

The default web UI will then display a new 'translations' action menu to
users who can modify translatable entities, and display your translatable
entities in the web request language by default.

Example
-------

If you want to translate all fields of the Card entity type (from the 'card'
32
cube), you will need in your schema.py file::
Florent Cayré's avatar
Florent Cayré committed
33
34

 from yams.buildobjs import RelationDefinition
Noé Gaumont's avatar
Noé Gaumont committed
35
36


Florent Cayré's avatar
Florent Cayré committed
37
38
39
40
41
 class i18nfield_of(RelationDefinition):
     subject = 'I18nField'
     object = 'Card'
     cardinality = '1*'
     composite = 'object'
Noé Gaumont's avatar
Noé Gaumont committed
42
43


Florent Cayré's avatar
Florent Cayré committed
44
45
 class ref_lang(RelationDefinition):
     subject = 'Card'
46
     object = 'I18nLang'
Florent Cayré's avatar
Florent Cayré committed
47
48
49
50
     cardinality = '1*'
     inlined = True


51
And in the entities.py file::
Florent Cayré's avatar
Florent Cayré committed
52

53
   from cubicweb.predicates import is_instance
Noé Gaumont's avatar
Noé Gaumont committed
54
55

   from cubicweb_card.entities import Card as OrigCard
56
   from cubicweb_i18nfield.entities import (TranslatableEntityMixin,
Florent Cayré's avatar
Florent Cayré committed
57
                                         _TranslatableEntityAdapter)
Noé Gaumont's avatar
Noé Gaumont committed
58
59


Florent Cayré's avatar
Florent Cayré committed
60
61
   class Card(TranslatableEntityMixin, OrigCard):
       i18nfields = (u'title', u'synopsis', u'content')
Noé Gaumont's avatar
Noé Gaumont committed
62

Florent Cayré's avatar
Florent Cayré committed
63
64
       def dc_title(self):
           return self.printable_value('title', format='text/plain')
Noé Gaumont's avatar
Noé Gaumont committed
65
66


Florent Cayré's avatar
Florent Cayré committed
67
68
   class MyTranslatableEntityAdapter(_TranslatableEntityAdapter):
       __select__ = _TranslatableEntityAdapter.__select__ & is_instance('Card')
69
70
71
72
73
74
75

Run tests
---------

In a virtualenv:

```
Noé Gaumont's avatar
Noé Gaumont committed
76
python2 -m tox
77
```