Module: DevDoc::I18n::PseudoLocale

Defined in:
lib/dev_doc/i18n/pseudo_locale.rb

Defined Under Namespace

Modules: Backend

Constant Summary collapse

PSEUDO_LOCALE =
'en-PSEUDO'
MARKER =
''
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

Class Method Details

.active?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/dev_doc/i18n/pseudo_locale.rb', line 46

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.



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dev_doc/i18n/pseudo_locale.rb', line 94

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.



78
79
80
81
82
83
# File 'lib/dev_doc/i18n/pseudo_locale.rb', line 78

def self.install!
  ::I18n::Backend::Simple.prepend(Backend)
  ::I18n.available_locales |= [PSEUDO_LOCALE.to_sym]
  force_locale!
  ::Rails.logger.info('[pseudo_locale] en-PSEUDO locale enabled (forced) for hardcoded-text detection') if defined?(::Rails.logger)
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).



55
56
57
58
59
60
# File 'lib/dev_doc/i18n/pseudo_locale.rb', line 55

def self.mark(value)
  return value unless active?
  return value unless value.is_a?(String) && !value.empty?

  value.include?(MARKER) ? value : "#{MARKER}#{value}"
end