Skip to main content

Provides a way to translate entity fields individually.

Project description

Summary

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’ entity type to your translatable entity type(s)

  • add a ‘ref_lang’ relation definition, linking from your entity types to the ‘I18nLang’ entity type

  • make your business entity type inherit the ‘TranslatableEntityMixin’ and list its translatable fields in its ‘i18nfields’ attribute

  • create a simple adapter that inherits ‘_TranslatableEntityAdapter’ and is selectable for your translatable entity types

That’s all for the code part. Just add at least one I18nLang instance in your 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’ cube), you will need in your schema.py file:

from yams.buildobjs import RelationDefinition


class i18nfield_of(RelationDefinition):
    subject = 'I18nField'
    object = 'Card'
    cardinality = '1*'
    composite = 'object'


class ref_lang(RelationDefinition):
    subject = 'Card'
    object = 'I18nLang'
    cardinality = '1*'
    inlined = True

And in the entities.py file:

from cubicweb.selectors import is_instance

from cubes.card.entities import Card as OrigCard
from cubes.i18nfield.entities import (TranslatableEntityMixin,
                                      _TranslatableEntityAdapter)


class Card(TranslatableEntityMixin, OrigCard):
    i18nfields = (u'title', u'synopsis', u'content')

    def dc_title(self):
        return self.printable_value('title', format='text/plain')


class MyTranslatableEntityAdapter(_TranslatableEntityAdapter):
    __select__ = _TranslatableEntityAdapter.__select__ & is_instance('Card')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cubicweb-i18nfield-0.1.0.tar.gz (15.2 kB view details)

Uploaded Source

File details

Details for the file cubicweb-i18nfield-0.1.0.tar.gz.

File metadata

File hashes

Hashes for cubicweb-i18nfield-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4f3bdeaef156b582fc955a5ea71b2db2500d55ed776c04491556ada3953c1889
MD5 ff3e54761af380b8a512d5187d73e656
BLAKE2b-256 81b02ab481b0411d8025b1ffa1d4f4b6d264361fa34a826a521c085c8e20d4d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page