Silme:Tutorial:L10nObject

From Braniecki's Wiki
Revision as of 16:42, 19 May 2008 by Zbraniecki (talk | contribs) (New page: == L10nObject == If you're building an entity list in a memory, or you're storing the data in a database Entity and EntityList objects are everything you need, so just head over to [[Verb...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

L10nObject

If you're building an entity list in a memory, or you're storing the data in a database Entity and EntityList objects are everything you need, so just head over to Verbatim::Tutorial::IOClient.

But if you happen to store your l10n lists in any kind of file format like DTD, XLIFF, Gettext or properties, you need a representation of this kind of objects and we all know that there's much more inside those files than EntityList can cover.

L10nObject is a class that extends EntityList and offers a way to store and manipulate the whole complex text object that contains an EntityList inside.

In the most basic example we can identify three elements each L10n file is made of. Entity, string and a comment.

Verbatim library covers this by offering you two classes - Entity and Comment, and letting you add a string to L10nObject. Since we described Entity class in the previous chapter, I'll focus on how we work with Comments and strings here.

String

String can be added to L10nObject in the very same way as entity.

from mozilla.l10n.object import String, Entity, L10nObject

l10nObject = L10nObject()

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

l10nObject.addString("\n\n")

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

In the above example we've added two entities to an L10nObject, but in the middle we separated them with a string. Two important paradigms about strings in Verbatim project are:

  • We reduce the number of string if possible. It means that we're concatenating strings if they're one next to each other.
  • We convert all strings to unicode.

Since L10nObject is extending EntityList, all methods described for EntityList will work here, with one difference. L10nObject may contain several other object types, so when you want to iterate through L10nObject and you care only of entities, use a method getEntityList to get an EntityList object or getEntities to get a normal list of entities.