1
1
module Fastlane
2
+ # Define a single Locale with the various locale codes depending on the representation needed
2
3
Locale = Struct . new ( :glotpress , :android , :google_play , :ios , :app_store , keyword_init : true ) do
4
+ # Returns the Locale with the given glotpress locale code from the list of all known locales (`Locales.all`)
5
+ #
6
+ # @param [String] The glotpress locale code for the locale to fetch
7
+ # @return [Locale] The locale found
8
+ # @raise [RuntimeException] if the locale with given glotpress code is unknown
3
9
def self . []( code )
4
10
Locales [ code ] . first
5
11
end
6
12
end
7
13
14
+ # A class with static methods to manipulate lists of locales.
15
+ # Exposes various `Array<Locale>` lists like all known locales, the Mag16,
16
+ # and convenience methods to turn list of Strings into list of Locales.
8
17
class Locales
9
18
###################
10
19
## Constants
@@ -77,28 +86,36 @@ def all
77
86
ALL_KNOWN_LOCALES
78
87
end
79
88
80
- # Define from_glotpress(code_or_list), from_android(code_or_list) … methods
89
+ # Define from_glotpress(code_or_list), from_android(code_or_list) … methods.
90
+ #
91
+ # Those can be used in the rare cases where you need to find locales via codes other than the glotpress ones,
92
+ # like searching by android locale code(s) or google_play locale code(s).
93
+ # In most cases, prefer using the `Locales[…]` method instead ()with glotpress locale codes).
81
94
#
82
95
# @param [Array<String>, String] list of locale codes to search for, or single value for single result
83
- # @return [Array<Locale>, Locale] list of found locales (empty if none found), or single locale if a single value was passed (or nil if not found)
96
+ # @return [Array<Locale>, Locale] list of found locales, or single locale if a single value was passed
97
+ # @raise [RuntimeException] if at least one of the locale codes was unknown
84
98
#
85
99
%i[ glotpress android google_play ios app_store ] . each do |key |
86
- define_method ( "from_#{ key } " ) { |args | search ( key , args ) }
100
+ define_method ( "from_#{ key } " ) { |args | search! ( key , args ) }
87
101
end
88
102
89
103
# Return an Array<Locale> based on glotpress locale codes
90
104
#
91
- # @note If you need a single locale, you can use Locale[code] instead of Locales[code]
105
+ # @note If you need a single locale instead of an `Array<Locale>`, you can use Locale[code] instead of Locales[code]
106
+ #
92
107
# @param [String..., Array<String>] Arbitrary list of strings, either passed as a single array parameter, or as a vararg list of params
93
108
# @return [Array<Locale>] The found locales.
109
+ # @raise [RuntimeException] if at least one of the locale codes was unknown
94
110
#
95
111
def []( *list )
96
- # If we passed an Array, `*list` will make it an Array<Array<String>>, so taking `list.first` in those cases to go back to Array<String>
112
+ # If we passed a variadic list of Strings, `*list` will make it a single `Array<String>` and we were already good to go.
113
+ # But if we passed an Array, `*list` will make it an Array<Array<String>> of one item; taking `list.first` will go back to Array<String>.
97
114
list = list . first if list . count == 1 && list . first . is_a? ( Array )
98
115
from_glotpress ( list )
99
116
end
100
117
101
- # Return the subset of the 16 locales most of our apps are localized 100% (what we call the "Magnificent 16")
118
+ # Return the subset of the 16 locales most of our apps are localized 100% (the ones we call the "Magnificent 16")
102
119
#
103
120
# @return [Array<Locale>] List of the Mag16 locales
104
121
def mag16
@@ -110,17 +127,17 @@ def mag16
110
127
private
111
128
112
129
# Search the known locales for just the ones having the provided locale code, where the codes are expressed using the standard for the given key
113
- def search ( key , code_or_list )
130
+ def search! ( key , code_or_list )
114
131
if code_or_list . is_a? ( Array )
115
- code_or_list . map { |code | search ( key , code ) }
132
+ code_or_list . map { |code | search! ( key , code ) }
116
133
else # String
117
134
raise 'The locale code should not contain spaces. Did you accidentally use `%[]` instead of `%w[]` at call site?' if code_or_list . include? ( ' ' )
118
135
119
- ALL_KNOWN_LOCALES . find { |locale | locale . send ( key ) == code_or_list } || not_found ( code_or_list , key )
136
+ ALL_KNOWN_LOCALES . find { |locale | locale . send ( key ) == code_or_list } || not_found! ( code_or_list , key )
120
137
end
121
138
end
122
139
123
- def not_found ( code , key )
140
+ def not_found! ( code , key )
124
141
raise "Unknown locale for #{ key } code '#{ code } '"
125
142
end
126
143
end
0 commit comments