|
| 1 | +--- |
| 2 | +html_meta: |
| 3 | + "description": "Translating content items in Plone, creating translations programmatically, and working with translators." |
| 4 | + "property=og:description": "Translating content items in Plone, creating translations programmatically, and working with translators." |
| 5 | + "property=og:title": "Translated content" |
| 6 | + "keywords": "Plone, Internationalization, i18n, language, translated, content, localization" |
| 7 | +--- |
| 8 | + |
| 9 | +(translated-content-label)= |
| 10 | + |
| 11 | +# Translated content |
| 12 | + |
| 13 | +```{admonition} Description |
| 14 | +Translating content items in Plone, creating translations programmatically, and working with translators. |
| 15 | +``` |
| 16 | + |
| 17 | + |
| 18 | +## Introduction |
| 19 | + |
| 20 | +Plone ships out of the box with a multilingual solution for translating user generated content. |
| 21 | +For all practical purposes, you should use that package, `plone.app.multilingual`. |
| 22 | + |
| 23 | +```{note} |
| 24 | +For earlier Plone versions, there were other solutions like `LinguaPlone` and `raptus.multilanguageplone`. |
| 25 | +Refer to the [Plone 4 version of this document](https://docs.plone.org/4/en/develop/plone/i18n/translating_content.html) if you need that information. |
| 26 | +``` |
| 27 | + |
| 28 | + |
| 29 | +## `plone.app.multilingual` |
| 30 | + |
| 31 | +`plone.app.multilingual` was designed originally to provide Plone a whole multilingual story. |
| 32 | +Using ZCA technologies, it enables translations to Dexterity and Archetypes content types, as well managed via a unified user interface. |
| 33 | + |
| 34 | +This module provides the user interface for managing content translations. |
| 35 | +It is the application package of the next generation Plone multilingual engine. |
| 36 | +It is designed to work with Dexterity content types and the *old fashioned* Archetypes based content types as well. |
| 37 | +It only works with Plone 4.1 and above, due to the use of UUIDs for referencing the translations. |
| 38 | + |
| 39 | +For more information see [`plone.app.multilingual`](https://github.com/plone/plone.app.multilingual). |
| 40 | + |
| 41 | + |
| 42 | +### Installation |
| 43 | + |
| 44 | +To use this package with both Dexterity and Archetypes based content types, you should add the following line to your `eggs` buildout section: |
| 45 | + |
| 46 | +```cfg |
| 47 | +eggs = |
| 48 | + plone.app.multilingual[archetypes, dexterity] |
| 49 | +``` |
| 50 | + |
| 51 | +If you need to use this package only with Archetypes based content types, you only need the following line: |
| 52 | + |
| 53 | +```cfg |
| 54 | +eggs = |
| 55 | + plone.app.multilingual[archetypes] |
| 56 | +``` |
| 57 | + |
| 58 | +While Archetypes is default in Plone for now, you can strip `[archetypes]`. |
| 59 | +This may change in the future, so we recommend adding an appendix as shown above. |
| 60 | + |
| 61 | + |
| 62 | +### Setup |
| 63 | + |
| 64 | +After re-running your buildout and installing the newly available add-ons, you should go to the {guilabel}`Languages` section of your site's control panel, and select at least two or more languages for your site. |
| 65 | +You will now be able to create translations of Plone's default content types, or to link existing content as translations. |
| 66 | + |
| 67 | + |
| 68 | +### Marking objects as translatables |
| 69 | + |
| 70 | + |
| 71 | +#### Archetypes |
| 72 | + |
| 73 | +By default, if `plone.app.multilingual` is installed, Archetypes-based content types are marked as translatables. |
| 74 | + |
| 75 | + |
| 76 | +#### Dexterity |
| 77 | + |
| 78 | +Users should mark a dexterity content type as translatable by assigning a multilingual behavior to the definition of the content type either via file system, supermodel, or through the web. |
| 79 | + |
| 80 | + |
| 81 | +### Marking fields as language independent |
| 82 | + |
| 83 | + |
| 84 | +#### Archetypes |
| 85 | + |
| 86 | +The language independent fields on Archetype-based content are marked as follows. |
| 87 | +This is the same as in previous version of Plone with `LinguaPlone` in place: |
| 88 | + |
| 89 | +```python |
| 90 | +atapi.StringField( |
| 91 | + 'myField', |
| 92 | + widget=atapi.StringWidget( |
| 93 | + #... |
| 94 | + ), |
| 95 | + languageIndependent=True |
| 96 | +) |
| 97 | +``` |
| 98 | + |
| 99 | +#### Dexterity |
| 100 | + |
| 101 | +There are four ways to translate Dexterity-based types. |
| 102 | + |
| 103 | + |
| 104 | +##### Grok directive |
| 105 | + |
| 106 | +In your content type class declaration: |
| 107 | + |
| 108 | +```python |
| 109 | +from plone.app.multilingual.dx import directives |
| 110 | +directives.languageindependent('field') |
| 111 | +``` |
| 112 | + |
| 113 | + |
| 114 | +##### Supermodel |
| 115 | + |
| 116 | +In your content type XML file declaration: |
| 117 | + |
| 118 | +```xml |
| 119 | +<field name="myField" type="zope.schema.TextLine" lingua:independent="true"> |
| 120 | + <description /> |
| 121 | + <title>myField</title> |
| 122 | +</field> |
| 123 | +``` |
| 124 | + |
| 125 | + |
| 126 | +##### Native |
| 127 | + |
| 128 | +In your code: |
| 129 | + |
| 130 | +```python |
| 131 | +from plone.app.multilingual.dx.interfaces import ILanguageIndependentField |
| 132 | +alsoProvides(ISchema['myField'], ILanguageIndependentField) |
| 133 | +``` |
| 134 | + |
| 135 | + |
| 136 | +##### Through the web |
| 137 | + |
| 138 | +Via the content type definition in the {guilabel}`Dexterity Content Types` control panel. |
| 139 | + |
| 140 | + |
| 141 | +### Language get/set via a unified adapter |
| 142 | + |
| 143 | +In order to access and modify the language of a content type regardless of the type (Archetypes/Dexterity) there is an interface/adapter, `plone.app.multilingual.interfaces.ILanguage`. |
| 144 | + |
| 145 | +In your code, you can use the following. |
| 146 | + |
| 147 | +```python |
| 148 | +from plone.app.multilingual.interfaces import ILanguage |
| 149 | +language = ILanguage(context).get_language() |
| 150 | +``` |
| 151 | + |
| 152 | +If you want to set the language of a content type: |
| 153 | + |
| 154 | +```python |
| 155 | +language = ILanguage(context).set_language('ca') |
| 156 | +``` |
| 157 | + |
| 158 | + |
| 159 | +### ITranslationManager adapter |
| 160 | + |
| 161 | +The most interesting adapter that `plone.app.multilingual` provides is `plone.app.multilingual.interfaces.ITranslationManager`. |
| 162 | + |
| 163 | +It adapts any `ITranslatable` object to provide convenience methods to manage the translations for that object. |
| 164 | + |
| 165 | + |
| 166 | +#### Add a translation |
| 167 | + |
| 168 | +Given an object `obj`, and we want to translate it to the Catalan language ('ca'): |
| 169 | + |
| 170 | +```python |
| 171 | +from plone.app.multilingual.interfaces import ITranslationManager |
| 172 | +ITranslationManager(obj).add_translation('ca') |
| 173 | +``` |
| 174 | + |
| 175 | + |
| 176 | +#### Register a translation for an already existing content |
| 177 | + |
| 178 | +Given an object `obj`, and we want to add `obj2` as a translation for the Catalan language ('ca'): |
| 179 | + |
| 180 | +```python |
| 181 | +ITranslationManager(obj).register_translation('ca', obj2) |
| 182 | +``` |
| 183 | + |
| 184 | + |
| 185 | +#### Get translations for an object |
| 186 | + |
| 187 | +Given an object `obj`: |
| 188 | + |
| 189 | +```python |
| 190 | +ITranslationManager(obj).get_translations() |
| 191 | +``` |
| 192 | + |
| 193 | +and if we want a concrete translation: |
| 194 | + |
| 195 | +```python |
| 196 | +ITranslationManager(obj).get_translation('ca') |
| 197 | +``` |
| 198 | + |
| 199 | + |
| 200 | +#### Check if an object has translations |
| 201 | + |
| 202 | +Given an object `obj`: |
| 203 | + |
| 204 | +```python |
| 205 | +ITranslationManager(obj).get_translated_languages() |
| 206 | +``` |
| 207 | + |
| 208 | +or: |
| 209 | + |
| 210 | +```python |
| 211 | +ITranslationManager(obj).has_translation('ca') |
| 212 | +``` |
| 213 | + |
| 214 | +For more information, see https://github.com/plone/plone.app.multilingual/blob/ee81a3015ac3f26505e82638030a95d786a1a444/src/plone/app/multilingual/interfaces.py#L76. |
0 commit comments