i18n_proofreading 1.1.1 → 1.2.0

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: 9a73bed80d485e35617f3ea214165c0ffcfc296721ac896f09d389cbf3a1b6e8
4
- data.tar.gz: 4cc2e32bf3420c01ea3af58c33582ffaa37c9fd26cf6e7425db1b554c4652c84
3
+ metadata.gz: b064bdf9957104685057c004265c500cc66817f1e80ec826c8d384596570a864
4
+ data.tar.gz: 7188a0820bee5027d18f65fc234bfc55bb96a96c4066755896759e1f75bb1680
5
5
  SHA512:
6
- metadata.gz: 1c884b9b47f7e5712ab618fc663f5160ed7d7eabd571b295818efe445f11111f2bbb81e7110336594c7885d79c032479241623a33bb81d170309165ccb522f40
7
- data.tar.gz: 7c939c1e63d708c6772dcd4e703816967942b1ca2400a05027cbd8fa200ec1c446de390daf2bca70cf5313eccaa4615dc132464ffbe395a3a8cfb19d84ba04f0
6
+ metadata.gz: 0f7c3fc2dee542a89d4a6f594d057fbbf6c02559a5bae2b04fd5cfa7a5a297bc76bb2c3bfc1705addf36bad1cd488208e76ccf17647c7613a266bd080b8a7843
7
+ data.tar.gz: f9dda98042e0a0ffc27a41eceecb7c6c6693a3e7e53373dc4c7bddb6dcb1eb3116ed97c3dac587a5a4b9bb07956438a3b52731d2c0dc6a7a5dd3d5470b5fe673
data/AGENTS.md CHANGED
@@ -83,7 +83,7 @@ The pill is one way; a link is another, and the choice is remembered in a cookie
83
83
  ?i18n_proofreading=false # off
84
84
  ```
85
85
 
86
- `config.show_pill = false` hides the pill, `config.pill_label` overrides its text (nil = the localized `i18n_proofreading.pill` key), and `config.toggle_param` renames the parameter.
86
+ `config.show_pill = false` hides the pill, `config.show_pill = :auto` hides it only on pages that carry a toggle link (use it when the menu holding your link is not on every layout), `config.pill_label` overrides its text (nil = the localized `i18n_proofreading.pill` key), and `config.toggle_param` renames the parameter.
87
87
 
88
88
  ### Do not
89
89
 
@@ -108,7 +108,7 @@ Everything is optional; a fresh install works with zero config in development. F
108
108
  | `author_label` | email, else `to_s` | Receives the user |
109
109
  | `available_locales` | `I18n.available_locales` | Callable; validates what a suggestion may target |
110
110
  | `auto_inject` | `true` | `false` = place `i18n_proofreading_tag` yourself |
111
- | `show_pill`, `pill_label` | `true`, localized | Hide the pill and use `?i18n_proofreading=true` instead |
111
+ | `show_pill`, `pill_label` | `true`, localized | Hide the pill and use `?i18n_proofreading=true` instead; `:auto` hides it only where the page has such a link |
112
112
  | `toggle_param` | `"i18n_proofreading"` | Rename the query parameter |
113
113
  | `rate_limit` | `{ to: 30, within: 60 }` | Rails 7.2+; ignored on 7.1. `nil` disables |
114
114
  | `mount_path` | `"/i18n_proofreading"` | Keep in sync with the `mount` line |
@@ -121,7 +121,7 @@ The tool's own UI ships in 26 languages, RTL mirrored, and follows system light/
121
121
  | Symptom | Cause |
122
122
  | --- | --- |
123
123
  | `NameError` for one of your own helpers in the dashboard | `isolate_namespace` scopes `helper` to the engine. Use `config.base_controller_class` so the dashboard inherits your helpers, rather than `admin_layout` alone. |
124
- | No pill, no outlines | The environment is not in `enabled_environments` (this is the usual one), or `config.enabled` returned false, or `show_pill = false` |
124
+ | No pill, no outlines | The environment is not in `enabled_environments` (this is the usual one), or `config.enabled` returned false, or `show_pill = false` (or `:auto` on a page with a toggle link) |
125
125
  | Pill appears but no strings are outlined | The I18n backend patch is only prepended in an enabled environment at boot — check you are actually in development/staging, and restart after changing the setting |
126
126
  | `/i18n_proofreading` returns a 403 | `authorize_admin` still at its development-only default |
127
127
  | Suggestions rejected with an invalid-token error | The layout is missing `csrf_meta_tags` |
data/CHANGELOG.md CHANGED
@@ -2,7 +2,38 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
- ## [1.1.1] - 2026-10-30
5
+ ## [1.2.0] - 2026-10-30
6
+
7
+ ### Fixed
8
+
9
+ - **Suggest mode no longer renders elements a page does not have.** A blank
10
+ translation (an empty value, or a `default: ""` for a missing key) used to get
11
+ a marker, which made it present: a view gating markup on `t(...).presence`
12
+ rendered it in suggest mode only. Bullet Train resolves every form field's
13
+ help and placeholder that way, so each field grew an empty help paragraph.
14
+ Blank results are now left unmarked.
15
+ - **No more stray `⟦⟧` on the page.** Rails' `t` helper resolves a String
16
+ `default:` for a missing key with `I18n.translate(nil, ...)`; the backend
17
+ marked that keyless result with an empty `⟦⟧`, which the widget could not
18
+ strip, so it showed beside the text and in placeholders. Keyless fallbacks
19
+ are now left unmarked, and the widget strips any empty marker that still
20
+ reaches the page.
21
+ - **Hovering a host toggle link no longer toggles suggest mode.** The link is a
22
+ GET that flips the mode, and Turbo 8 prefetches a link when the pointer rests
23
+ on it, so a hover alone turned suggest mode on or off. The widget now cancels
24
+ Turbo's prefetch (`turbo:before-prefetch`) for any link carrying the toggle
25
+ parameter. Other prefetchers (Speculation Rules) are not covered.
26
+ - Costs: a `default:` fallback for a missing key is no longer clickable in
27
+ suggest mode. It had no key to attribute a suggestion to, so it could not be
28
+ submitted usefully anyway. Blank strings are no longer clickable either; they
29
+ rendered nothing to click.
30
+
31
+ ### Added
32
+
33
+ - **`config.show_pill = :auto`** shows the pill only on pages without a toggle
34
+ link of the host's own, decided by the widget on every render. For a host
35
+ whose menu item is not on every layout: pages with the menu get only the menu
36
+ item, the rest keep the pill. The default stays `true`.
6
37
 
7
38
  ### Fixed
8
39
 
data/README.md CHANGED
@@ -87,7 +87,9 @@ works from any app that bundles it.
87
87
 
88
88
  1. In an enabled environment, the I18n backend appends a hidden `⟦some.key⟧`
89
89
  marker to each translated string — only while a reviewer has the tool on
90
- (a cookie), so pages are clean by default.
90
+ (a cookie), so pages are clean by default. Blank strings and Rails'
91
+ keyless `default:` fallbacks stay unmarked, so a view's `t(...).presence`
92
+ check behaves the same in suggest mode.
91
93
  2. The widget strips every marker out of the DOM on load and remembers which
92
94
  key produced each piece of text.
93
95
  3. Clicking a string opens a popover: current text, pending suggestions, and a
@@ -110,7 +112,7 @@ Everything is optional — the defaults work out of the box in development. In
110
112
  | `author_label` | the user's `email` | Label shown for the author |
111
113
  | `available_locales` | `I18n.available_locales` | Which locales a suggestion may target |
112
114
  | `auto_inject` | `true` | Inject the widget into HTML responses |
113
- | `show_pill` | `true` | The floating "Suggest edits" pill |
115
+ | `show_pill` | `true` | The floating "Suggest edits" pill. `:auto` shows it only on pages without a toggle link of your own |
114
116
  | `pill_label` | `nil` | Fixed pill text. `nil` uses the localized default |
115
117
  | `toggle_param` | `"i18n_proofreading"` | Query param that toggles suggest mode |
116
118
  | `mount_path` | `"/i18n_proofreading"` | Keep in sync with `mount` in `routes.rb` |
@@ -145,7 +147,9 @@ config.on_submit = ->(s) { SuggestionMailer.with(suggestion: s).created.deliver_
145
147
 
146
148
  Prefer a menu item over the floating pill? Drive suggest mode from any link —
147
149
  and optionally hide the pill with `config.show_pill = false` (the two can also
148
- coexist).
150
+ coexist). If the menu is not on every page, `config.show_pill = :auto` hides
151
+ the pill only where the page has a toggle link, so a layout without your menu
152
+ keeps the pill.
149
153
 
150
154
  A one-way "turn it on" link is just the toggle parameter:
151
155
 
@@ -165,7 +169,7 @@ language:
165
169
  <% end %>
166
170
  ```
167
171
 
168
- Three things worth knowing:
172
+ Four things worth knowing:
169
173
 
170
174
  - `?i18n_proofreading=true` turns it on, `false` off. State lives in the
171
175
  `i18n_proofreading` cookie; the middleware then redirects to the same URL
@@ -173,6 +177,11 @@ Three things worth knowing:
173
177
  - These links keep working **while suggest mode is active**. The widget freezes
174
178
  ordinary navigation during proofreading so a stray click can't leave the page
175
179
  mid-edit — but any link carrying the toggle parameter is exempt.
180
+ - **A hover never toggles.** The link is a GET that flips the mode, and Turbo 8
181
+ prefetches a link when the pointer rests on it; the widget cancels Turbo's
182
+ prefetch for any link carrying the toggle parameter. If the app also uses
183
+ another prefetcher (Speculation Rules, `rel="prefetch"`), exclude these links
184
+ there.
176
185
  - The `i18n_proofreading.*` scope is **exempt from key-marking**, so the tool
177
186
  never flags its own controls as editable. No plain-literal workaround needed.
178
187
 
@@ -55,7 +55,8 @@ I18nProofreading.configure do |config|
55
55
  # Show the floating "Suggest edits" pill. Set false to hide it and toggle
56
56
  # suggest mode from your own link instead, e.g.
57
57
  # `<%= link_to "Proofread", "?i18n_proofreading=true" %>`. "?i18n_proofreading=false"
58
- # exits; the choice is remembered in a cookie.
58
+ # exits; the choice is remembered in a cookie. Set :auto to hide the pill only
59
+ # on pages that carry such a link (a menu that is not on every layout).
59
60
  #
60
61
  # config.show_pill = true
61
62
  # config.toggle_param = "i18n_proofreading"
@@ -67,7 +67,9 @@ module I18nProofreading
67
67
  attr_accessor :mount_path
68
68
 
69
69
  # Show the floating toggle pill. Set false to hide it and drive suggest mode
70
- # from your own link instead (see #toggle_param).
70
+ # from your own link instead (see #toggle_param). Set :auto to hide it only
71
+ # on pages that carry such a link, for a host whose menu is not on every
72
+ # layout; the widget decides on each render.
71
73
  attr_accessor :show_pill
72
74
 
73
75
  # Text on the floating toggle pill. Leave nil to use the localized default
@@ -32,6 +32,14 @@ module I18nProofreading
32
32
  def translate(locale, key, options = {})
33
33
  result = super
34
34
  return result unless Marking.enabled? && result.is_a?(String)
35
+ # Blank text has nothing to click, and a marker would make it present:
36
+ # hosts gate markup on `t(...).presence` (Bullet Train renders every form
37
+ # field's help and placeholder that way), so suggest mode would render
38
+ # elements the page does not have.
39
+ return result if result.blank?
40
+ # Rails' `t` helper resolves a String `default:` for a missing key with
41
+ # `I18n.translate(nil, default: ...)`: no key to attribute a suggestion to.
42
+ return result if key.to_s.empty?
35
43
 
36
44
  full = [*options[:scope], key].join('.')
37
45
  return result if full.match?(SKIP)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module I18nProofreading
4
- VERSION = '1.1.1'
4
+ VERSION = '1.2.0'
5
5
  end
@@ -29,8 +29,14 @@
29
29
  var LEFT = "⟦"; // ⟦
30
30
  var RIGHT = "⟧"; // ⟧
31
31
  var TOKEN = new RegExp(LEFT + "([^" + RIGHT + "]+)" + RIGHT);
32
- var TOKENS = new RegExp("\\s*" + LEFT + "[^" + RIGHT + "]+" + RIGHT, "g");
32
+ // Stripping also takes an empty "⟦⟧": it names no key to attribute, but it
33
+ // must never reach the screen either.
34
+ var ANY_TOKEN = new RegExp(LEFT + "[^" + RIGHT + "]*" + RIGHT);
35
+ var TOKENS = new RegExp("\\s*" + LEFT + "[^" + RIGHT + "]*" + RIGHT, "g");
33
36
  var COOKIE = "i18n_proofreading";
37
+ // A host's own on/off link ("?i18n_proofreading=true"). The parameter name
38
+ // is server configuration, the same on every page.
39
+ var TOGGLE_LINK = 'a[href*="' + (config.toggleParam || "i18n_proofreading") + '="]';
34
40
  var MARKED_ATTRS = ["placeholder", "title", "aria-label", "value"];
35
41
  var Z = 2147483000;
36
42
 
@@ -59,6 +65,11 @@
59
65
  // toggling suggest mode across a Turbo visit needs no re-registration.
60
66
  document.addEventListener("keydown", handleKeydown);
61
67
  document.addEventListener("click", handleClick, true);
68
+ // A toggle link is a GET that flips suggest mode, and Turbo 8 prefetches a
69
+ // link once the pointer rests on it: without this, a hover toggles the mode.
70
+ document.addEventListener("turbo:before-prefetch", function (event) {
71
+ if (event.target.closest && event.target.closest(TOGGLE_LINK)) event.preventDefault();
72
+ });
62
73
 
63
74
  // Everything else lives in <body>, which Turbo replaces on every visit —
64
75
  // taking the pill and the active-mode highlighting with it. Re-run the
@@ -114,10 +125,17 @@
114
125
  if (config === UNAVAILABLE) return;
115
126
 
116
127
  injectStyles();
117
- if (config.showPill !== false) buildPill();
128
+ if (showPill()) buildPill();
118
129
  if (config.active) strip();
119
130
  }
120
131
 
132
+ // "auto" leaves the pill to pages without a host toggle link: where the host
133
+ // offers its own on/off control, a second one would only cover the page.
134
+ function showPill() {
135
+ if (config.showPill === "auto") return !document.querySelector(TOGGLE_LINK);
136
+ return config.showPill !== false;
137
+ }
138
+
121
139
  // --- suggest-mode toggle --------------------------------------------------
122
140
 
123
141
  function toggle() {
@@ -141,7 +159,7 @@
141
159
  acceptNode: function (node) {
142
160
  var tag = node.parentElement && node.parentElement.tagName;
143
161
  if (tag === "SCRIPT" || tag === "STYLE") return NodeFilter.FILTER_REJECT;
144
- return TOKEN.test(node.nodeValue) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
162
+ return ANY_TOKEN.test(node.nodeValue) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
145
163
  },
146
164
  });
147
165
 
@@ -150,12 +168,10 @@
150
168
 
151
169
  nodes.forEach(function (node) {
152
170
  var match = node.nodeValue.match(TOKEN);
153
- if (!match) return;
154
- var key = match[1];
155
171
  node.nodeValue = node.nodeValue.replace(TOKENS, "");
156
172
  var element = node.parentElement;
157
- if (element) {
158
- element.dataset.i18nKey = key;
173
+ if (match && element) {
174
+ element.dataset.i18nKey = match[1];
159
175
  element.dataset.i18nValue = node.nodeValue.trim();
160
176
  }
161
177
  });
@@ -177,7 +193,7 @@
177
193
  // Let a host's own suggest-mode toggle link through (e.g. "?i18n_proofreading=false"
178
194
  // in a nav menu); otherwise navigation-freezing would trap the user in suggest
179
195
  // mode with no way out but the pill.
180
- if (event.target.closest && event.target.closest('a[href*="' + config.toggleParam + '="]')) return;
196
+ if (event.target.closest && event.target.closest(TOGGLE_LINK)) return;
181
197
 
182
198
  // Freeze navigation so a stray click can't leave the page mid-proofread.
183
199
  event.preventDefault();
@@ -62,7 +62,7 @@ module I18nProofreading
62
62
  endpoint: endpoint,
63
63
  locale: locale.to_s,
64
64
  active: active ? true : false,
65
- showPill: I18nProofreading.config.show_pill ? true : false,
65
+ showPill: show_pill,
66
66
  pillLabel: I18nProofreading.config.pill_label,
67
67
  toggleParam: I18nProofreading.config.toggle_param,
68
68
  labels: labels(locale),
@@ -88,6 +88,15 @@ module I18nProofreading
88
88
 
89
89
  private
90
90
 
91
+ # true, false, or "auto": the widget then shows the pill only on a page
92
+ # without a host toggle link, decided in the browser on every render.
93
+ def show_pill
94
+ setting = I18nProofreading.config.show_pill
95
+ return 'auto' if setting.to_s == 'auto'
96
+
97
+ setting ? true : false
98
+ end
99
+
91
100
  # Every user-facing string in the widget, resolved through Rails I18n so the
92
101
  # popover matches the locale the page was rendered in. The locale is passed
93
102
  # explicitly (not read from I18n.locale) because auto-injection runs in
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n_proofreading
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Shmarov