Skip to content

Commit

Permalink
Refactor code repetitions
Browse files Browse the repository at this point in the history
  • Loading branch information
khaledhosny committed Apr 23, 2014
1 parent 627b2dd commit 3e18f41
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions translate/convert/prop2po.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,6 @@
logger = logging.getLogger(__name__)


def _collapse(store, units):
sources = [u.source for u in units]
targets = [u.target for u in units]
# TODO: only consider the right ones for sources and targets
plural_unit = store.addsourceunit(sources)
plural_unit.target = targets
return plural_unit


class prop2po:
"""convert a .properties file to a .po file for handling the
translation."""
Expand Down Expand Up @@ -165,6 +156,17 @@ def mergestore(self, origpropfile, translatedpropfile):

def fold_gaia_plurals(self, postore):
"""Fold the multiple plural units of a gaia file into a gettext plural."""

def _append_plural_unit(store, plurals, plural):
units = plurals[plural]
sources = [u.source for u in units]
targets = [u.target for u in units]
# TODO: only consider the right ones for sources and targets
plural_unit = store.addsourceunit(sources)
plural_unit.target = targets
plural_unit.addlocation(plural)
del plurals[plural]

new_store = type(postore)()
plurals = {}
current_plural = u""
Expand All @@ -175,9 +177,7 @@ def fold_gaia_plurals(self, postore):
if u"plural(n)" in unit.source:
if current_plural:
# End of a set of plural units
new_unit = _collapse(new_store, plurals[current_plural])
new_unit.addlocation(current_plural)
del plurals[current_plural]
_append_plural_unit(new_store, plurals, current_plural)
current_plural = u""
# start of a set of plural units
location = unit.getlocations()[0]
Expand All @@ -194,18 +194,14 @@ def fold_gaia_plurals(self, postore):
continue
elif current_plural:
# End of a set of plural units
new_unit = _collapse(new_store, plurals[current_plural])
new_unit.addlocation(current_plural)
del plurals[current_plural]
_append_plural_unit(new_store, plurals, current_plural)
current_plural = u""

new_store.addunit(unit)

if current_plural:
# The file ended with a set of plural units
new_unit = _collapse(new_store, plurals[current_plural])
new_unit.addlocation(current_plural)
del plurals[current_plural]
_append_plural_unit(new_store, plurals, current_plural)
current_plural = u""

# if everything went well, there should be nothing left in plurals
Expand Down

0 comments on commit 3e18f41

Please sign in to comment.