Module: DevDoc::I18n::PseudoLocale
- Defined in:
- lib/dev_doc/i18n/pseudo_locale.rb
Overview
Pseudo-locale ('en-PSEUDO') machinery for hardcoded-text detection: wraps every translated string in visible markers so any UNMARKED copy in a rendered page is, by construction, text that bypassed I18n.
Defined Under Namespace
Modules: Backend
Constant Summary collapse
- PSEUDO_LOCALE =
'en-PSEUDO'.freeze
- MARKER =
'⟦'.freeze
- MAP =
Letter map — readable but unmistakably "transformed".
{ 'a' => 'á', 'b' => 'ƀ', 'c' => 'ç', 'd' => 'ð', 'e' => 'é', 'f' => 'ƒ', 'g' => 'ĝ', 'h' => 'ĥ', 'i' => 'í', 'j' => 'ĵ', 'k' => 'ķ', 'l' => 'ļ', 'm' => 'ɱ', 'n' => 'ñ', 'o' => 'ö', 'p' => 'þ', 'q' => 'ɋ', 'r' => 'ŕ', 's' => 'š', 't' => 'ţ', 'u' => 'ü', 'v' => 'ṽ', 'w' => 'ŵ', 'x' => 'ӿ', 'y' => 'ý', 'z' => 'ž', 'A' => 'Á', 'B' => 'Ɓ', 'C' => 'Ç', 'D' => 'Ð', 'E' => 'É', 'F' => 'Ƒ', 'G' => 'Ĝ', 'H' => 'Ĥ', 'I' => 'Í', 'J' => 'Ĵ', 'K' => 'Ķ', 'L' => 'Ļ', 'M' => 'Ṁ', 'N' => 'Ñ', 'O' => 'Ö', 'P' => 'Þ', 'Q' => 'Ɋ', 'R' => 'Ŕ', 'S' => 'Š', 'T' => 'Ţ', 'U' => 'Ü', 'V' => 'Ṽ', 'W' => 'Ŵ', 'X' => 'Ӿ', 'Y' => 'Ý', 'Z' => 'Ž' }.freeze
Class Method Summary collapse
- .active? ⇒ Boolean
-
.force_locale! ⇒ Object
Force every request to render in the pseudo locale.
-
.install! ⇒ Object
Wires the pseudo backend into I18n and forces every request to render in it.
-
.mark(value) ⇒ Object
Marks an externally-managed string — CMS content, assessment-YAML values, or a chosen ActiveRecord display value — so the hardcoded-text scanner treats it as already-handled and skips it.
Class Method Details
.active? ⇒ Boolean
49 50 51 |
# File 'lib/dev_doc/i18n/pseudo_locale.rb', line 49 def self.active? ENV['PSEUDO_I18N'] == '1' end |
.force_locale! ⇒ Object
Force every request to render in the pseudo locale.
Without this, an app's default_url_options typically derives the URL
locale via I18n.locale.to_s.split('-')[0] ("en-PSEUDO" -> "en"), so
every link and redirect the app generates carries locale=en;
set_locale then honours that param and renders plain English — the
pseudo transform never fires on any navigated page. Overriding
set_locale here pins the locale regardless of the incoming param,
which is exactly what a "render everything pseudo" detection mode wants.
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/dev_doc/i18n/pseudo_locale.rb', line 98 def self.force_locale! ::Rails.application.config.to_prepare do ApplicationController.class_eval do private def set_locale ::I18n.locale = DevDoc::I18n::PseudoLocale::PSEUDO_LOCALE.to_sym end end end end |
.install! ⇒ Object
Wires the pseudo backend into I18n and forces every request to render in it. Idempotent — safe to call once from the project initializer.
81 82 83 84 85 86 87 |
# File 'lib/dev_doc/i18n/pseudo_locale.rb', line 81 def self.install! ::I18n::Backend::Simple.prepend(Backend) ::I18n.available_locales |= [PSEUDO_LOCALE.to_sym] force_locale! # &. only because the logger may not be assigned yet this early in boot. ::Rails.logger&.info('[pseudo_locale] en-PSEUDO locale enabled (forced) for hardcoded-text detection') end |
.mark(value) ⇒ Object
Marks an externally-managed string — CMS content, assessment-YAML values, or a chosen ActiveRecord display value — so the hardcoded-text scanner treats it as already-handled and skips it. Returns the string untouched when detection mode is off, so it is safe on production paths (call it through a null-guard; see the header comment).
58 59 60 61 62 63 |
# File 'lib/dev_doc/i18n/pseudo_locale.rb', line 58 def self.mark(value) return value unless active? return value unless value.is_a?(String) && !value.empty? value.include?(MARKER) ? value : "#{MARKER}#{value}⟧" end |