Merge lp:~jtv/launchpad/recife-findtranslationmessage into lp:~launchpad/launchpad/recife

Proposed by Jeroen T. Vermeulen
Status: Merged
Approved by: Edwin Grubbs
Approved revision: no longer in the source branch.
Merged at revision: 9166
Proposed branch: lp:~jtv/launchpad/recife-findtranslationmessage
Merge into: lp:~launchpad/launchpad/recife
Diff against target: 0 lines
To merge this branch: bzr merge lp:~jtv/launchpad/recife-findtranslationmessage
Reviewer Review Type Date Requested Status
Edwin Grubbs (community) code Approve
Review via email: mp+34297@code.launchpad.net

Commit message

POTMsgSet.findTranslationMessage

Description of the change

= POTMsgSet.findTranslationMessage =

For the Recife feature branch. This makes a POTMsgSet available in the class interface: findTranslationMessage. We'll be using that to find TranslationMessages for a POTMsgSet that match a specific translation text.

There was already a private method of a similar name, but it accepted a dict mapping plural form numbers to POTranslation objects. The desired new interface takes a dict mapping plural form numbers to translation strings. So I implemented the new method as a wrapper around the former.

The conversion from dict-of-strings to dict-of-POTranslations happens in _findPOTranslations. This method actually quietly supported an additional format: list-of-strings. It more or less worked, more or less by accident. For each plural form number it checked if that form was in the translations dict, and if so, looked it up. If passed a list, that same code would merrily do a linear search for the plural-form number in the list. The list only contains strings, not numbers, so it never found anything and that's how it never raised an exception when we abused it.

I rewrote that code to be slightly more sensible and now it blows up if passed a list. You'll see that in lib/lp/translations/model/potmsgset.py; look for "Set all POTranslations" to find it. This also affected a few tests, which I fixed up pragmatically. In tests it can be convenient to pass a list of strings.

Finally, I inverted the default divergence policy: if there are matching diverged and shared messages, which one should it prefer? The original internal method preferred the shared one because that suited the present use-case. But in general a diverged message takes precedence over a shared one, so I considered that a more suitable default.

To test,
{{{
./bin/test -vvc -m lp.translations.tests.test_potmsgset
./bin/test -vvc -m lp.translations.tests.test_side
}}}

No lint,

Jeroen

To post a comment you must log in.
Revision history for this message
Edwin Grubbs (edwin-grubbs) wrote :

Hi Jeroen,

This is a nice branch. I just have one comment below that we already discussed on IRC.

-Edwin

>=== modified file 'lib/lp/translations/model/potmsgset.py'
>--- lib/lp/translations/model/potmsgset.py 2010-08-26 08:51:48 +0000
>+++ lib/lp/translations/model/potmsgset.py 2010-09-01 14:11:25 +0000
>@@ -560,25 +562,39 @@
> potranslations = {}
> # Set all POTranslations we can have (up to MAX_PLURAL_FORMS)
> for pluralform in xrange(TranslationConstants.MAX_PLURAL_FORMS):
>- if (pluralform in translations and
>- translations[pluralform] is not None):
>+ translation = translations.get(pluralform)
>+ if translation is not None:
> # Find or create a POTranslation for the specified text
>- translation = translations[pluralform]
> potranslations[pluralform] = (
> POTranslation.getOrCreateTranslation(translation))
> else:
> potranslations[pluralform] = None
> return potranslations
>
>- def _findTranslationMessage(self, pofile, potranslations,
>- prefer_shared=True):
>- """Find a matching message in this `pofile`.
>+ def findTranslationMessage(self, pofile, translations=None,
>+ potranslations=None, prefer_shared=False):

The potranslations parameter can be removed.

>+ """Find the best matching message in this `pofile`.
>
> The returned message matches exactly the given `translations`
> strings (except plural forms not supported by `pofile`, which
>- are ignored).
>-
>- :param potranslations: A list of translation strings.
>+ are ignored in the comparison).
>+
>+ :param translations: A dict mapping plural forms to translation
>+ strings.
>+ :param prefer_shared: Whether to prefer a shared match over a
>+ diverged one.
>+ """
>+ potranslations = self._findPOTranslations(translations)
>+ return self._findMatchingTranslationMessage(
>+ pofile, potranslations, prefer_shared=prefer_shared)

review: Approve (code)

Preview Diff

Empty

Subscribers

People subscribed via source and target branches