Silme:Tutorial:Entity

From Braniecki's Wiki
Revision as of 13:44, 19 May 2008 by Zbraniecki (talk | contribs)
Jump to navigation Jump to search

Entity

The basic unit we use in the localization world is an entity. Entity is a simple object made of ID and a value. ID is a somehow unique identifier of the entity, and the value is an actual string that you want to present to the user.

In Verbatim creating a new Entity looks like this:

from mozilla.l10n.object import Entity

entity = Entity('id','value')

This simple object is the most atomic unit in every localization format, be it DTD, Gettext, XLIFF or L20n. It's an abstract representation of a row in the l10n array in SQL or it represents a single string in HTML file (in this case ID has to be a unique identifier of the string position in the file).

Of course Entity may be much more complex than that. In some cases you can have a meta data for the single entity, or a comment that is tied to the entity. Because of how popular those two cases are among l10n formats, Entity object has predefined dictionary objects for those two:


from mozilla.l10n.object import Entity

entity = Entity('id','value') entity.comment['pre'] = u'Comment prefixing the entity' entity.parameter['author'] = 'Ben'

EntityList

EntityList is a simplest possible class used to store the sequence of entities.

from mozilla.l10n.object import Entity, EntityList

entityList = EntityList()

entity1 = Entity('example.id','Test value') entityList.addEntity(entity1)

entity2 = Entity('example.id2','Test value2') entityList.addEntity(entity2)

You can operate on the EntityList to get either all Entities as a new list or just ids of the entities as a new list

from mozilla.l10n.object import EntityList

entityList = EntityList()

entity1 = entityList[0] # returns a first entity from the list

entities = entityList.getEntities() # returns a list of entities

entityIdList = entityList.getEntityIds() # returns a list of ids from the entityList


Advanced

You may decide to extend Entity class or just add your own properties to the Entity object.