File: basic.rst

package info (click to toggle)
sqlalchemy-i18n 1.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 396 kB
  • sloc: python: 2,164; makefile: 157
file content (89 lines) | stat: -rw-r--r-- 2,232 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

Basic usage
===========

Current translation
-------------------

Current translation is a hybrid property in parent object that returns the associated translation object for current locale.::


    article = Article()
    article.current_translation.name = 'Some article'


You can also directly set the current translation::


    article.current_translation = ArticleTranslation(name='Some article')


Articles and translations can be efficiently fetched using various SQLAlchemy loading strategies::


    session.query(Article).options(joinedload(Article.current_translation))


Fallback translation
--------------------

If there is no translation available for the current locale then fallback locale is being used. Fallback translation is a convenient hybrid property
for accessing this translation object.::


    article = Article()
    article.translations.en.name = 'Some article'

    article.fallback_translation.name  # Some article


Fallback translation is especially handy in situations where you don't necessarily have all the objects translated in various languages but need to fetch them efficiently. ::


    query = (
        session.query(Article)
        .options(joinedload(Article.current_translation))
        .options(joinedload(Article.fallback_translation))
    )



Translatable columns as hybrids
-------------------------------

For each translatable column SQLAlchemy-i18n creates a hybrid property in the parent class. These hybrid properties always point at the current translation.

Example: ::


    article = Article()
    article.name = u'Some article'

    article.translations['en'].name  # u'Some article'


If the there is no translation available for current locale then these hybrids return the translation for fallback locale. Let's assume the current locale here is 'fi'::


    article = Article()
    article.translations.fi.name = ''
    article.translations.en.name = 'Some article'

    article.name  # 'Some article'



Accessing translations
----------------------

Dictionary based access::


    article.translations['en'].name = u'Some article'


Attribute access::

    article.translations.en.name = u'Some article'
    article.translations.fi.name = u'Joku artikkeli'