Commits

David Jean Louis committed faf4e52

Fixed issue #10: Finding entries with the same msgid and different context (msgctxt)

Comments (0)

Files changed (2)

         fhandle.write(contents)
         fhandle.close()
 
-    def find(self, st, by='msgid', include_obsolete_entries=False):
+    def find(self, st, by='msgid', include_obsolete_entries=False,
+             msgctxt=False):
         """
         Find entry which msgid (or property identified by the *by*
         attribute) matches the string *st*.
           - *st*: string, the string to search for
           - *by*: string, the comparison attribute
           - *include_obsolete_entries*: boolean, whether to also search in 
-            entries that are obsolete.
+            entries that are obsolete
+          - *msgctxt*: string, allows to specify a specific message context
+            for the search.
 
         **Examples**:
 
         >>> entry = po.find('Thursday')
         >>> entry.msgstr
         u'Jueves'
+        >>> entry = po.find('test context')
+        >>> entry.msgstr
+        u'test context 1'
+        >>> entry = po.find('test context', msgctxt='@context1')
+        >>> entry.msgstr
+        u'test context 1'
+        >>> entry = po.find('test context', msgctxt='@context2')
+        >>> entry.msgstr
+        u'test context 2'
         >>> entry = po.find('Some unexistant msgid')
         >>> entry is None
         True
         """
         for e in self:
             if getattr(e, by) == st:
+                if msgctxt and e.msgctxt != msgctxt:
+                    continue
                 return e
         if include_obsolete_entries:
             for e in self.obsolete_entries():
 msgid "Some msgid"
 msgstr "Some msgstr"
 
+# test context 1
+msgctxt "@context1"
+msgid "test context"
+msgstr "test context 1"
+
+# test context 2
+msgctxt "@context2"
+msgid "test context"
+msgstr "test context 2"
+
+# test context
+msgctxt "@context1"
+msgid "Some msgid"
+msgstr "Some msgstr"
+
 # Added for previous msgid/msgid_plural/msgctxt testing
 #| msgctxt "@previous_context"
 #| msgid ""