11module Fastlane
2+ # Define a single Locale with the various locale codes depending on the representation needed
23 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
39 def self . []( code )
410 Locales [ code ] . first
511 end
612 end
713
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.
817 class Locales
918 ###################
1019 ## Constants
@@ -77,28 +86,36 @@ def all
7786 ALL_KNOWN_LOCALES
7887 end
7988
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).
8194 #
8295 # @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
8498 #
8599 %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 ) }
87101 end
88102
89103 # Return an Array<Locale> based on glotpress locale codes
90104 #
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+ #
92107 # @param [String..., Array<String>] Arbitrary list of strings, either passed as a single array parameter, or as a vararg list of params
93108 # @return [Array<Locale>] The found locales.
109+ # @raise [RuntimeException] if at least one of the locale codes was unknown
94110 #
95111 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>.
97114 list = list . first if list . count == 1 && list . first . is_a? ( Array )
98115 from_glotpress ( list )
99116 end
100117
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")
102119 #
103120 # @return [Array<Locale>] List of the Mag16 locales
104121 def mag16
@@ -110,17 +127,17 @@ def mag16
110127 private
111128
112129 # 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 )
114131 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 ) }
116133 else # String
117134 raise 'The locale code should not contain spaces. Did you accidentally use `%[]` instead of `%w[]` at call site?' if code_or_list . include? ( ' ' )
118135
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 )
120137 end
121138 end
122139
123- def not_found ( code , key )
140+ def not_found! ( code , key )
124141 raise "Unknown locale for #{ key } code '#{ code } '"
125142 end
126143 end
0 commit comments