Skip to content

Commit 0d847bb

Browse files
committed
Add file each message method.
1 parent bceb9cd commit 0d847bb

4 files changed

Lines changed: 28 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Add `GettextPO::File#each_message` method.
6+
57
## CRuby version 0.2.1 - 2026-03-16
68

79
- CRuby: Fix to work with gettext 0.23.

ext/gettextpo/gettextpo.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,11 @@ gettextpo_po_file_m_domains (VALUE self)
803803

804804
/**
805805
* call-seq: message_iterator (domain = nil) -> GettextPO::MessageIterator
806+
*
807+
* You may find it handy to use the #each_message method instead of
808+
* this to iterate over the messages. This method is for more
809+
* versatile manipulation; the returned iterator can insert messages,
810+
* for example.
806811
*/
807812
VALUE
808813
gettextpo_po_file_m_message_iterator (int argc, VALUE *argv, VALUE self)

mrblib/mrb_gettextpo_common.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ def file_positions
7878
end
7979
end
8080

81+
class File
82+
def each_message(domain = nil) # yields: message
83+
iter = message_iterator(domain)
84+
while true
85+
begin
86+
yield iter.next
87+
rescue StopIteration
88+
return
89+
end
90+
end
91+
end
92+
end
93+
8194
# This class doesn't provide the +new+ class method. See also
8295
# GettextPO::Message#filepos.
8396
class FilePos

test/file.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@
6666
true
6767
end
6868

69+
assert 'each message' do
70+
path = File.expand_path(File.join(__FILE__, "../../test.cruby/resources/ok.po"))
71+
messages = []
72+
GettextPO::File.read(path).each_message { |message| messages << message.msgid }
73+
assert_equal ['', 'msgid1'], messages
74+
true
75+
end
76+
6977
assert 'header field' do
7078
assert_nil GettextPO::File.new.domain_header
7179

0 commit comments

Comments
 (0)