Skip to content

Commit 01483c4

Browse files
committed
Treat an empty one/other plural form as missing, not as content
`build_pot_entry` guarded with `.nil?`, but an empty-string form is truthy in Ruby, so `var_dict['one'] || var_dict['other']` returned `''` for an empty `one` and produced an entry with an empty `msgid` — which gettext folds into the header, silently dropping the entry from the `.pot`. Extract `msgid_for` (the `one`-or-`other` choice), shared by the forward and reverse paths so the written `msgid` always matches the one looked up later, and treat an empty `one` like an absent one (fall back to `other`). The required-form guard now rejects empty strings too, so an empty `other` fails with the existing clear error instead of emitting a broken entry.
1 parent 29b3f6f commit 01483c4

2 files changed

Lines changed: 50 additions & 5 deletions

File tree

lib/fastlane/plugin/wpmreleasetoolkit/helper/ios/ios_stringsdict_helper.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,22 @@ def self.context_for(key:, variable:, single_variable:)
206206
single_variable ? key : "#{key}:#{variable}"
207207
end
208208

209+
# The English string that becomes a variable's gettext `msgid`: the `one`
210+
# form when it has content, otherwise `other`. Forward and reverse both
211+
# use this, so the `msgid` written to the `.pot` matches the one later
212+
# looked up in the translated `.po` — and an empty `one` no longer yields
213+
# an empty `msgid` (which gettext drops, silently losing the entry).
214+
def self.msgid_for(var_dict)
215+
one = var_dict['one']
216+
one.nil? || one.empty? ? var_dict['other'] : one
217+
end
218+
209219
def self.build_pot_entry(context:, var_dict:, key:, variable:)
210-
singular = var_dict['one'] || var_dict['other']
220+
singular = msgid_for(var_dict)
211221
plural = var_dict['other']
212-
if singular.nil? || plural.nil?
222+
if singular.nil? || singular.empty? || plural.nil? || plural.empty?
213223
UI.user_error!("Stringsdict entry '#{key}' is missing a required plural form " \
214-
"(needs at least 'other'; 'one' recommended for the singular).")
224+
"(needs a non-empty 'other'; 'one' recommended for the singular).")
215225
end
216226

217227
warn_dropped_categories(key: key, variable: variable, var_dict: var_dict)
@@ -247,8 +257,7 @@ def self.warn_dropped_categories(key:, variable:, var_dict:)
247257

248258
# The translated plural forms for a context, or `[]` if untranslated.
249259
def self.translated_forms(parsed_po:, context:, source_var:)
250-
msgid = source_var['one'] || source_var['other']
251-
entry = parsed_po[context, msgid]
260+
entry = parsed_po[context, msgid_for(source_var)]
252261
return [] if entry.nil? || entry.msgstr.nil?
253262

254263
forms = entry.msgstr.split(PLURAL_SEPARATOR, -1)

spec/ios_stringsdict_helper_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,42 @@ def convert_po_to_stringsdict(po_content, template:, locale:)
182182
expect(content).not_to include('No homes found')
183183
end
184184
end
185+
186+
it 'treats an empty `one` as absent — uses `other` as the singular rather than dropping the entry' do
187+
in_tmp_dir do |dir|
188+
src = File.join(dir, 'empty-one.stringsdict')
189+
described_class.write(
190+
data: {
191+
'k' => {
192+
'NSStringLocalizedFormatKey' => '%#@c@',
193+
'c' => { 'NSStringFormatSpecTypeKey' => 'NSStringPluralRuleType', 'one' => '', 'other' => '%d items' }
194+
}
195+
},
196+
path: src
197+
)
198+
pot = File.join(dir, 'out.pot')
199+
count = described_class.generate_pot(stringsdict_paths: src, output_path: pot)
200+
expect(count).to eq(1) # entry is emitted, not silently dropped via an empty msgid
201+
expect(File.read(pot)).to include('msgid "%d items"') # `other` becomes the msgid
202+
end
203+
end
204+
205+
it 'raises when `other` is empty (an empty form counts as missing)' do
206+
in_tmp_dir do |dir|
207+
src = File.join(dir, 'empty-other.stringsdict')
208+
described_class.write(
209+
data: {
210+
'k' => {
211+
'NSStringLocalizedFormatKey' => '%#@c@',
212+
'c' => { 'NSStringFormatSpecTypeKey' => 'NSStringPluralRuleType', 'one' => '%d item', 'other' => '' }
213+
}
214+
},
215+
path: src
216+
)
217+
expect { described_class.generate_pot(stringsdict_paths: src, output_path: File.join(dir, 'o.pot')) }
218+
.to raise_error(FastlaneCore::Interface::FastlaneError, /missing a required plural form/)
219+
end
220+
end
185221
end
186222

187223
describe '.generate_stringsdict_from_po' do

0 commit comments

Comments
 (0)