Package translate :: Package storage :: Module base :: Class TranslationUnit |
[hide private]
[frames] | no frames]

# Class TranslationUnit

source code

Base class for translation units.

Our concept of a translation unit is influenced heavily by XLIFF: http://www.oasis-open.org/committees/xliff/documents/xliff-specification.htm

As such most of the method- and variable names borrows from XLIFF terminology.

A translation unit consists of the following:

- A source string. This is the original translatable text.
- A target string. This is the translation of the source.
- Zero or more notes on the unit. Notes would typically be some comments from a translator on the unit, or some comments originating from the source code.
- Zero or more locations. Locations indicate where in the original source code this unit came from.
- Zero or more errors. Some tools (eg. pofilter) can run checks on translations and produce error messages.

Instance Methods | [hide private]
__init__(self, source) Constructs a TranslationUnit containing the given source string. | source code
Boolean |
__eq__(self, other) Compares two TranslationUnits. | source code
multistring_to_rich(cls, mulstring) Convert a multistring to a list of "rich" string trees: | source code
string |
getid(self) A unique identifier for this unit. | source code
getcontext(self) Get the message context. | source code
markreviewneeded(self, needsreview=True, explanation=None) Marks the unit to indicate whether it needs review. | source code
istranslated(self) Indicates whether this unit is translated. | source code
istranslatable(self) Indicates whether this unit can be translated. | source code
isfuzzy(self) Indicates whether this unit is fuzzy. | source code
markfuzzy(self, value=True) Marks the unit as fuzzy or not. | source code
isheader(self) Indicates whether this unit is a header. | source code
isreview(self) Indicates whether this unit needs review. | source code
isblank(self) Used to see if this unit has no source or target string. | source code
hasplural(self) Tells whether or not this specific unit has plural strings. | source code
merge(self, otherunit, overwrite=False, comments=True) Do basic format agnostic merging. | source code
unit_iter(self) Iterator that only returns this unit. | source code
getunits(self) This unit in a list. | source code
Inherited from `object`: `__delattr__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__repr__`, `__setattr__`, `__str__`
Source
setsource(self, source) Sets the source string to the given value. | source code
_get_rich_source(self) | source code
_set_rich_source(self, value) | source code
getsourcelanguage(self) | source code
Target
settarget(self, target) Sets the target string to the given value. | source code
_get_rich_target(self) | source code
_set_rich_target(self, value) | source code
Integer |
gettargetlen(self) Returns the length of the target string. | source code
gettargetlanguage(self) | source code
Notes
getnotes(self, origin=None) Returns all notes about this unit. | source code
addnote(self, text, origin=None) Adds a note (comment). | source code
removenotes(self) Remove all the translator's notes. | source code
Locations
List |
getlocations(self) A list of source code locations. | source code
addlocation(self, location) Add one location to the list of locations. | source code
addlocations(self, location) Add a location or a list of locations. | source code
Errors
adderror(self, errorname, errortext) Adds an error message to this unit. | source code
Dictionary |
geterrors(self) Get all error messages. | source code

Class Methods | [hide private]
rich_to_multistring(cls, elem_list) Convert a "rich" string tree to a `multistring`: |
buildfromunit(cls, unit) Build a native unit from a foreign unit, preserving as much information as possible. |

Class Variables | [hide private]
| rich_parsers = ``[``]`` A list of functions to use for parsing a string into a rich string tree.

Properties | [hide private]
| xid
| rid
Inherited from `object`: `__class__`
Source
| source
| rich_source
Target
| target
| rich_target

Method Details | [hide private]

### __init__(self, source) (Constructor) | source code
Constructs a TranslationUnit containing the given source string. Overrides: object.__init__

### __eq__(self, other) (Equality operator) | source code
Compares two TranslationUnits. Parameters: `other` (TranslationUnit) - Another TranslationUnit Returns: Boolean Returns True if the supplied TranslationUnit equals this unit.

### rich_to_multistring(cls, elem_list) Class Method |
Convert a "rich" string tree to a `multistring`:
    >>> from translate.storage.placeables.interfaces import X
    >>> rich = [StringElem(['foo', X(id='xxx', sub=[' ']), 'bar'])]
    >>> TranslationUnit.rich_to_multistring(rich)
    multistring(u'foo bar')

### multistring_to_rich(cls, mulstring) | source code
Convert a multistring to a list of "rich" string trees:
    >>> target = multistring([u'foo', u'bar', u'baz'])
    >>> TranslationUnit.multistring_to_rich(target)
    [<StringElem([<StringElem([u'foo'])>])>,
     <StringElem([<StringElem([u'bar'])>])>,
     <StringElem([<StringElem([u'baz'])>])>]

### gettargetlen(self) | source code
Returns the length of the target string. Returns: Integer Note: Plural forms might be combined.

### getid(self) | source code
A unique identifier for this unit. Returns: string an identifier for this unit that is unique in the store Derived classes should override this in a way that guarantees a unique identifier for each unit in the store.

### getlocations(self) | source code
A list of source code locations. Returns: List Note: Shouldn't be implemented if the format doesn't support it.

### addlocation(self, location) | source code
Add one location to the list of locations. Note: Shouldn't be implemented if the format doesn't support it.

### addlocations(self, location) | source code
Add a location or a list of locations. Note: Most classes shouldn't need to implement this, but should rather implement addlocation(). Warning: This method might be removed in future.

### getnotes(self, origin=None) | source code
Returns all notes about this unit. It will probably be freeform text or something reasonable that can be synthesised by the format. It should not include location comments (see getlocations()).

### addnote(self, text, origin=None) | source code
Adds a note (comment). Parameters: `text` (string) - Usually just a sentence or two. `origin` (string) - Specifies who/where the comment comes from. Origin can be one of the following text strings: 'translator' 'developer', 'programmer', 'source code' (synonyms)

### adderror(self, errorname, errortext) | source code
Adds an error message to this unit. Parameters: `errorname` (string) - A single word to id the error. `errortext` (string) - The text describing the error.

### markreviewneeded(self, needsreview=True, explanation=None) | source code
Marks the unit to indicate whether it needs review. Parameters: `needsreview` - Defaults to True. `explanation` - Adds an optional explanation as a note.

### istranslated(self) | source code
Indicates whether this unit is translated. This should be used rather than deducing it from .target, to ensure that other classes can implement more functionality (as XLIFF does).

### istranslatable(self) | source code
Indicates whether this unit can be translated. This should be used to distinguish real units for translation from header, obsolete, binary or other blank units.

### isblank(self) | source code
Used to see if this unit has no source or target string. Note: This is probably used more to find translatable units, and we might want to move in that direction rather and get rid of this.

Property Details | [hide private]

### source Get Method: unreachable(self) Set Method: setsource(self, source) - Sets the source string to the given value.

### target Get Method: unreachable(self) Set Method: settarget(self, target) - Sets the target string to the given value.

### rich_source Get Method: _get_rich_source(self) Set Method: _set_rich_source(self, value)

### rich_target Get Method: _get_rich_target(self) Set Method: _set_rich_target(self, value)

### xid Get Method: unreachable(self) Set Method: unreachable(self, value)

### rid Get Method: unreachable(self) Set Method: unreachable(self, value)