Skip to content

Commit 56dd66d

Browse files
committed
Skip Android UI-mode qualifier dirs (e.g. values-car) when pruning
The locale-detection regex's 2-3 letter language branch also matches Android UI-mode qualifiers like `values-car`, which would prune valid car-mode resources. `car` is also a valid 3-letter ISO 639 language code and cannot be distinguished from a locale by shape alone, so exclude the documented UI-mode qualifiers explicitly. Changes: - Add UI_MODE_QUALIFIERS exclusion list and apply it in locale detection - Add a values-kmr test covering 3-letter legacy locales - Note locale-only scope in the CHANGELOG entry
1 parent 904e2de commit 56dd66d

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_prune_orphaned_translations_action.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66
module Fastlane
77
module Actions
88
class AndroidPruneOrphanedTranslationsAction < Action
9-
# Matches an Android `values-<qualifier>` directory whose qualifier is a locale (a language code, optional
10-
# region, or a BCP-47 `b+` form), so non-locale qualifier dirs (e.g. `values-night`, `values-v21`,
11-
# `values-land`) are left untouched.
9+
# Matches an Android `values-<qualifier>` directory whose qualifier is a locale: a 2- or 3-letter language
10+
# code (`fr`, `kmr`), an optional region (`pt-rBR`, `es-r419`), or a BCP-47 `b+` form (`b+sr+Latn`), so
11+
# non-locale qualifier dirs (e.g. `values-night`, `values-v21`, `values-land`) are left untouched.
1212
LOCALE_VALUES_DIR_REGEX = /\Avalues-(?:b\+[a-zA-Z]+(?:\+[a-zA-Z0-9]+)*|[a-z]{2,3}(?:-r(?:[A-Z]{2}|\d{3}))?)\z/
1313

14+
# Android UI-mode qualifiers, which are never locales:
15+
# https://developer.android.com/guide/topics/resources/providing-resources#UiModeQualifier
16+
# `car` is indistinguishable by shape from a 3-letter ISO 639 language code, so `LOCALE_VALUES_DIR_REGEX`
17+
# would otherwise treat `values-car` as a locale and prune its (valid) car-mode resources. There's no purely
18+
# syntactic way to tell the two apart, so we exclude the documented UI-mode qualifiers explicitly. (`car` is
19+
# the only one short enough to currently match the regex; the rest are listed to capture the full set.)
20+
UI_MODE_QUALIFIERS = %w[car desk television appliance watch vrheadset].freeze
21+
1422
DEFAULT_SOURCE_STRINGS_FILE_NAME = 'strings.xml'
1523
DEFAULT_SOURCE_STRINGS_RELATIVE_PATH = File.join('values', DEFAULT_SOURCE_STRINGS_FILE_NAME).freeze
1624

@@ -20,7 +28,8 @@ def self.run(params)
2028
valid_keys = collect_keys(source_paths)
2129

2230
locale_files = Dir.glob(File.join(res_dir, 'values-*', DEFAULT_SOURCE_STRINGS_FILE_NAME)).select do |file|
23-
File.basename(File.dirname(file)).match?(LOCALE_VALUES_DIR_REGEX)
31+
dir_name = File.basename(File.dirname(file))
32+
dir_name.match?(LOCALE_VALUES_DIR_REGEX) && !UI_MODE_QUALIFIERS.include?(dir_name.delete_prefix('values-'))
2433
end
2534
total_pruned = 0
2635

spec/android_prune_orphaned_translations_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,30 @@ def write_file(path, content)
109109
end
110110
end
111111

112+
# `kmr` (Northern Kurdish) is a real 3-letter legacy locale used by e.g. WordPress-Android, so it must still be
113+
# pruned — guarding against an over-eager "restrict locales to 2 letters" fix for the `values-car` collision.
114+
it 'prunes 3-letter legacy locale directories (e.g. values-kmr)' do
115+
Dir.mktmpdir do |dir|
116+
res_dir = File.join(dir, 'res')
117+
write_file(File.join(res_dir, 'values', 'strings.xml'), default_strings)
118+
kmr_file = File.join(res_dir, 'values-kmr', 'strings.xml')
119+
write_file(kmr_file, <<~XML)
120+
<?xml version="1.0" encoding="UTF-8"?>
121+
<resources>
122+
<string name="hello">Silav</string>
123+
<string name="orphan_string">Sêwî</string>
124+
</resources>
125+
XML
126+
127+
pruned = run_described_fastlane_action(res_dir: res_dir)
128+
129+
expect(pruned).to eq(1)
130+
content = File.read(kmr_file)
131+
expect(content).to include('name="hello"')
132+
expect(content).not_to include('orphan_string')
133+
end
134+
end
135+
112136
it 'treats keys from `additional_source_strings_paths` as valid (flavor overlay case)' do
113137
Dir.mktmpdir do |dir|
114138
res_dir = File.join(dir, 'res')

0 commit comments

Comments
 (0)