ruby_native 0.15.2 → 0.15.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 34a06223f80d171207b1eb5e0e56d67f3f02ba2737037fa06b7c16f4f3fbecad
4
- data.tar.gz: 26c59be4e4848abb167cf45d39249609b888167aad3835eb367c70f4fc41f1ed
3
+ metadata.gz: 1be33272a96d8436b0384a2723f6661e349c873c8df954fadba80472bbfe565c
4
+ data.tar.gz: c547853c962c66b1c588185b54ca584ef2d9796fa735fcbe7ad560ba0644f159
5
5
  SHA512:
6
- metadata.gz: b6a051041b02d7d87d2881b4ff279beed81d0f5291746611e601c1f68f8c46eb85539250fb34bb567394c4395b3ed99d44078132d6e0f6d894beb52f91694e25
7
- data.tar.gz: 0cad8e6fac5dce1596ec40009a9e01f9ce1549b6ca3a89b3dee70205d40ff98ef5a6e99d112202e197d25d27ffb37fda4d268681e6137e8e0aa016b07b494414
6
+ metadata.gz: 5dda4cf2375e87449b8e64e81c07fe02cf2d671fdea7182fafd7f34436674bb633ff7823a19c7e2efd7414ff888af17c7d646e9652437e807f0c67458dd97972
7
+ data.tar.gz: ba5e2c1ee978774a81e7292cf6cd7b8e96fba7f80820d81483f0c64721e478205afc4167de8a8bc7064d96fc908a194699f9ebe03a35c1c4bad9e1a9da1c2a8f
@@ -102,6 +102,23 @@ tabs:
102
102
 
103
103
  # Per-tab options (add to any tab above):
104
104
  #
105
+ # # Localize this tab's title. Put the copy in your app's own locale files
106
+ # # under `ruby_native.tabs.<key>.title`, the same way the error screen uses
107
+ # # `ruby_native.errors.*`, and the device's language picks the translation:
108
+ # #
109
+ # # # config/locales/it.yml
110
+ # # it:
111
+ # # ruby_native:
112
+ # # tabs:
113
+ # # home:
114
+ # # title: Casa
115
+ # #
116
+ # # `title:` above stays the fallback for a language you have not
117
+ # # translated. Once every tab has a key you can drop `title:` entirely and
118
+ # # keep all of your copy in the locale files.
119
+ # # https://rubynative.com/docs/tabs#localization
120
+ # key: home
121
+ #
105
122
  # # Load this tab on launch instead of lazily.
106
123
  # # Default: false.
107
124
  # eager: true
@@ -1,3 +1,3 @@
1
1
  module RubyNative
2
- VERSION = "0.15.2"
2
+ VERSION = "0.15.3"
3
3
  end
data/lib/ruby_native.rb CHANGED
@@ -62,6 +62,7 @@ module RubyNative
62
62
  normalize_linked_paths
63
63
  backfill_tab_icons
64
64
  backfill_error_icons
65
+ warn_on_duplicate_tab_keys
65
66
  end
66
67
 
67
68
  # config/ruby_native.yml is rendered as ERB before it is parsed, so a
@@ -95,6 +96,21 @@ module RubyNative
95
96
  end
96
97
  end
97
98
 
99
+ # A tab's `key` names its copy in the host app's locale files, so two tabs
100
+ # sharing one read the same translations and one of them is always
101
+ # mislabeled. Warn rather than raise: both tabs still render, and a typo in
102
+ # the config should never take down boot.
103
+ def self.warn_on_duplicate_tab_keys
104
+ keys = Array(self.config[:tabs]).filter_map { |tab| tab[:key] if tab.is_a?(Hash) }
105
+ duplicates = keys.tally.select { |_, count| count > 1 }.keys
106
+ return if duplicates.empty?
107
+
108
+ Rails.logger.warn(
109
+ "[RubyNative] Duplicate tab key(s) in config/ruby_native.yml " \
110
+ "(#{duplicates.join(", ")}). Each tab needs its own key, or they share a title."
111
+ )
112
+ end
113
+
98
114
  # Mirrors `backfill_tab_icons` for the error screen: fills a state's flat
99
115
  # `icon` from its per-platform `icons` (ios first, then android), so the iOS
100
116
  # app, which reads only the flat `icon`, still renders one. An explicit
@@ -134,6 +150,7 @@ module RubyNative
134
150
  # Shipped app binaries have required the `appearance` key at decode; keep
135
151
  # emitting it so they still boot when the YAML omits the block.
136
152
  payload[:appearance] ||= {}
153
+ localize_tab_titles(payload[:tabs])
137
154
  errors = error_screen_config(payload[:errors])
138
155
  if errors.empty?
139
156
  payload.delete(:errors)
@@ -143,6 +160,51 @@ module RubyNative
143
160
  payload
144
161
  end
145
162
 
163
+ # Fills each tab's `titles` from `ruby_native.tabs.<key>.title` in the host
164
+ # app's locale files, mirroring the `ruby_native.errors.*` namespace. The map
165
+ # is emitted beside the flat `title` rather than replacing it, the same
166
+ # additive shape as `icon`/`icons`: both platforms decode `title` as a plain
167
+ # String, so an app that does not read `titles` yet keeps rendering the YAML
168
+ # copy. Tabs without a `key` are left exactly as authored.
169
+ #
170
+ # `title:` is optional in the YAML so a localized app can keep all of its copy
171
+ # in locale files, but it is never optional on the wire: both platforms decode
172
+ # it as a required, non-optional String, so a tab missing one fails the whole
173
+ # config decode and drops the user on the error screen. Fill it from the
174
+ # default locale, then from any locale that was translated.
175
+ def self.localize_tab_titles(tabs)
176
+ Array(tabs).each do |tab|
177
+ next unless tab.is_a?(Hash)
178
+
179
+ key = tab[:key].to_s
180
+ titles = key.empty? ? {} : translations_for("tabs.#{key}.title")
181
+ tab[:titles] = titles unless titles.empty?
182
+ next unless tab[:title].to_s.empty?
183
+
184
+ tab[:title] = titles[I18n.default_locale] || titles.values.first || fallback_tab_title(key)
185
+ end
186
+ end
187
+
188
+ # Reached only when a tab has no `title:` and no translation in any locale,
189
+ # which would otherwise serve a payload that cannot decode. Names the tab
190
+ # after its key ("order_history" -> "Order history") so the bar still renders
191
+ # something recognizable, and says so, since the copy is missing everywhere.
192
+ def self.fallback_tab_title(key)
193
+ if key.empty?
194
+ Rails.logger.warn(
195
+ "[RubyNative] A tab in config/ruby_native.yml has no `title:`. Give it one, or a `key:` " \
196
+ "with copy under `ruby_native.tabs.<key>.title` in your locale files."
197
+ )
198
+ return "Untitled"
199
+ end
200
+
201
+ Rails.logger.warn(
202
+ "[RubyNative] Tab #{key.inspect} in config/ruby_native.yml has no `title:` and no " \
203
+ "`ruby_native.tabs.#{key}.title` translation in any locale. Falling back to a title from the key."
204
+ )
205
+ key.humanize
206
+ end
207
+
146
208
  # Merges per-state error-screen icons (from YAML) with localized copy (from
147
209
  # I18n) into the shape the native apps decode. Omits any state with neither an
148
210
  # icon nor copy, so an untouched app sends no `errors` block at all.
@@ -155,7 +217,7 @@ module RubyNative
155
217
  entry[:icons] = state_config[:icons] if state_config[:icons]
156
218
  end
157
219
  ERROR_SCREEN_COPY_KEYS.each do |key|
158
- translations = error_screen_translations("#{state}.#{key}")
220
+ translations = translations_for("errors.#{state}.#{key}")
159
221
  entry[key] = translations unless translations.empty?
160
222
  end
161
223
  result[state] = entry unless entry.empty?
@@ -163,17 +225,17 @@ module RubyNative
163
225
 
164
226
  # The Retry button label is shared by both states, so it sits at the top of
165
227
  # the block rather than under a state.
166
- retry_label = error_screen_translations("retry")
228
+ retry_label = translations_for("errors.retry")
167
229
  config[:retry] = retry_label unless retry_label.empty?
168
230
  config
169
231
  end
170
232
 
171
- # Reads `ruby_native.errors.<subkey>` for every available locale, keeping only
172
- # the locales the developer actually translated. Copy lives in the host app's
173
- # own locale files; the gem ships none.
174
- def self.error_screen_translations(subkey)
233
+ # Reads `ruby_native.<subkey>` for every available locale, keeping only the
234
+ # locales the developer actually translated. Copy lives in the host app's own
235
+ # locale files; the gem ships none.
236
+ def self.translations_for(subkey)
175
237
  I18n.available_locales.each_with_object({}) do |locale, result|
176
- value = I18n.t("ruby_native.errors.#{subkey}", locale: locale, default: nil)
238
+ value = I18n.t("ruby_native.#{subkey}", locale: locale, default: nil)
177
239
  result[locale] = value unless value.nil?
178
240
  end
179
241
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_native
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.2
4
+ version: 0.15.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Masilotti