|
| 1 | +module Fastlane |
| 2 | + Locale = Struct.new(:glotpress, :android, :google_play, :ios, :app_store, keyword_init: true) do |
| 3 | + def self.[](code) |
| 4 | + Locales[code].first |
| 5 | + end |
| 6 | + end |
| 7 | + |
| 8 | + class Locales |
| 9 | + |
| 10 | + ################### |
| 11 | + ## Constants |
| 12 | + ALL_KNOWN_LOCALES = [ |
| 13 | + Locale.new(glotpress: "ar", android: "ar", google_play: "ar" ), |
| 14 | + Locale.new(glotpress: "de", android: "de", google_play: "de-DE"), |
| 15 | + Locale.new(glotpress: "en-gb", android: "en-rGB", google_play: "en-US"), |
| 16 | + Locale.new(glotpress: "es", android: "es", google_play: "es-ES"), |
| 17 | + Locale.new(glotpress: "fr-ca", android: "fr-rCA", google_play: "fr-CA"), |
| 18 | + Locale.new(glotpress: "fr", android: "fr", google_play: "fr-FR"), |
| 19 | + Locale.new(glotpress: "he", android: "he", google_play: "iw-IL"), |
| 20 | + Locale.new(glotpress: "id", android: "id", google_play: "id" ), |
| 21 | + Locale.new(glotpress: "it", android: "it", google_play: "it-IT"), |
| 22 | + Locale.new(glotpress: "ja", android: "ja", google_play: "ja-JP"), |
| 23 | + Locale.new(glotpress: "ko", android: "ko", google_play: "ko-KR"), |
| 24 | + Locale.new(glotpress: "nl", android: "nl", google_play: "nl-NL"), |
| 25 | + Locale.new(glotpress: "pl", android: "pl", google_play: "pl-PL"), |
| 26 | + Locale.new(glotpress: "pt-br", android: "pt-rBR", google_play: "pt-BR"), |
| 27 | + Locale.new(glotpress: "ru", android: "ru", google_play: "ru-RU"), |
| 28 | + Locale.new(glotpress: "sr", android: "sr", google_play: "sr" ), |
| 29 | + Locale.new(glotpress: "sv", android: "sv", google_play: "sv-SE"), |
| 30 | + Locale.new(glotpress: "th", android: "th", google_play: "th" ), |
| 31 | + Locale.new(glotpress: "tr", android: "tr", google_play: "tr-TR"), |
| 32 | + Locale.new(glotpress: "vi", android: "vi", google_play: "vi" ), |
| 33 | + Locale.new(glotpress: "zh-cn", android: "zh-rCN", google_play: "zh-CN"), |
| 34 | + Locale.new(glotpress: "zh-tw", android: "zh-rTW", google_play: "zh-TW"), |
| 35 | + Locale.new(glotpress: "az", android: "az"), |
| 36 | + Locale.new(glotpress: "el", android: "el"), |
| 37 | + Locale.new(glotpress: "es-mx", android: "es-rMX"), |
| 38 | + Locale.new(glotpress: "es-cl", android: "es-rCL"), |
| 39 | + Locale.new(glotpress: "gd", android: "gd"), |
| 40 | + Locale.new(glotpress: "hi", android: "hi"), |
| 41 | + Locale.new(glotpress: "hu", android: "hu"), |
| 42 | + Locale.new(glotpress: "nb", android: "nb"), |
| 43 | + Locale.new(glotpress: "pl", android: "pl"), |
| 44 | + Locale.new(glotpress: "th", android: "th"), |
| 45 | + Locale.new(glotpress: "uz", android: "uz"), |
| 46 | + Locale.new(glotpress: "zh-tw", android: "zh-rHK"), |
| 47 | + Locale.new(glotpress: "eu", android: "eu"), |
| 48 | + Locale.new(glotpress: "ro", android: "ro"), |
| 49 | + Locale.new(glotpress: "mk", android: "mk"), |
| 50 | + Locale.new(glotpress: "en-au", android: "en-rAU"), |
| 51 | + Locale.new(glotpress: "sr", android: "sr"), |
| 52 | + Locale.new(glotpress: "sk", android: "sk"), |
| 53 | + Locale.new(glotpress: "cy", android: "cy"), |
| 54 | + Locale.new(glotpress: "da", android: "da"), |
| 55 | + Locale.new(glotpress: "bg", android: "bg"), |
| 56 | + Locale.new(glotpress: "sq", android: "sq"), |
| 57 | + Locale.new(glotpress: "hr", android: "hr"), |
| 58 | + Locale.new(glotpress: "cs", android: "cs"), |
| 59 | + Locale.new(glotpress: "pt-br", android: "pt-rBR"), |
| 60 | + Locale.new(glotpress: "en-ca", android: "en-rCA"), |
| 61 | + Locale.new(glotpress: "ms", android: "ms"), |
| 62 | + Locale.new(glotpress: "es-ve", android: "es-rVE"), |
| 63 | + Locale.new(glotpress: "gl", android: "gl"), |
| 64 | + Locale.new(glotpress: "is", android: "is"), |
| 65 | + Locale.new(glotpress: "es-co", android: "es-rCO"), |
| 66 | + Locale.new(glotpress: "kmr", android: "kmr") |
| 67 | + ].freeze |
| 68 | + |
| 69 | + MAG16_GP_CODES = %w[ar de es fr he id it ja ko nl pt-br ru sv tr zh-cn zh-tw].freeze |
| 70 | + |
| 71 | + ################### |
| 72 | + ## Static Methods |
| 73 | + |
| 74 | + class << self |
| 75 | + |
| 76 | + # @return [Array<Locale>] Array of all the known locales |
| 77 | + # |
| 78 | + def all |
| 79 | + ALL_KNOWN_LOCALES |
| 80 | + end |
| 81 | + |
| 82 | + # Define from_glotpress(code_or_list), from_android(code_or_list) … methods |
| 83 | + # |
| 84 | + # @param [Array<String>, String] list of locale codes to search for, or single value for single result |
| 85 | + # @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) |
| 86 | + # |
| 87 | + %i[glotpress android google_play ios app_store].each do |key| |
| 88 | + define_method("from_#{key}") { |args| search(key, args) } |
| 89 | + end |
| 90 | + |
| 91 | + # Return an Array<Locale> based on glotpress locale codes |
| 92 | + # |
| 93 | + # @note If you need a single locale, you can use Locale[code] instead of Locales[code] |
| 94 | + # @param [String..., Array<String>] Arbitrary list of strings, either passed as a single array parameter, or as a vararg list of params |
| 95 | + # @return [Array<Locale>] The found locales. |
| 96 | + # |
| 97 | + def [](*list) |
| 98 | + # 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> |
| 99 | + list = list.first if list.count == 1 && list.first.is_a?(Array) |
| 100 | + from_glotpress(list) |
| 101 | + end |
| 102 | + |
| 103 | + # Return the subset of the 16 locales most of our apps are localized 100% (what we call the "Magnificent 16") |
| 104 | + # |
| 105 | + # @return [Array<Locale>] List of the Mag16 locales |
| 106 | + def mag16 |
| 107 | + from_glotpress(MAG16_GP_CODES) |
| 108 | + end |
| 109 | + |
| 110 | + ################### |
| 111 | + |
| 112 | + private |
| 113 | + |
| 114 | + # 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 |
| 115 | + def search(key, code_or_list) |
| 116 | + if code_or_list.is_a?(Array) |
| 117 | + code_or_list.map { |code| search(key, code) } |
| 118 | + else # String |
| 119 | + raise 'The locale code should not contain spaces. Did you accidentally use `%[]` instead of `%w[]` at call site?' if code_or_list.include?(' ') |
| 120 | + ALL_KNOWN_LOCALES.find { |locale| locale.send(key) == code_or_list } || not_found(code_or_list, key) |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + def not_found(code, key) |
| 125 | + raise "Unknown locale for #{key} code '#{code}'" |
| 126 | + end |
| 127 | + end |
| 128 | + end |
| 129 | +end |
0 commit comments