Sha256: c6bf1de9817ecea7f63fae871de0957de03d744e6da4b89512247970fd1cb84a

Contents?: true

Size: 1.51 KB

Versions: 12

Compression:

Stored size: 1.51 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

# the whole file will be eval'ed/executed and gc-collected after returning/executing the loader proc

# eval: no need for the whole file in memory
plurals, _ = eval(Pagy.root.join('locales', 'utils', 'p11n.rb').read)   #rubocop:disable Security/Eval

# flatten the dictionary file nested keys
# convert each value to a simple ruby interpolation proc
flatten = lambda do |hash, key=''|
            hash.each.reduce({}) do |h, (k, v)|
              v.is_a?(Hash) \
                ? h.merge!(flatten.call(v, "#{key}#{k}."))
                : h.merge!(eval %({"#{key}#{k}" => lambda{|vars|"#{v.gsub(/%{[^}]+?}/){|m| "\#{vars[:#{m[2..-2]}]||'#{m}'}" }}"}})) #rubocop:disable Security/Eval
            end
          end

# loader proc
lambda do |i18n, *args|
  i18n.clear
  args.each do |arg|
    if Pagy::DEPRECATED_LOCALES.key?(arg[:locale])
      new_locale = Pagy::DEPRECATED_LOCALES[arg[:locale]]
      $stderr.puts("WARNING: the Pagy locale '#{arg[:locale]}' is deprecated; use '#{new_locale}' instead")
      arg[:locale] = new_locale
    end
    arg[:filepath]  ||= Pagy.root.join('locales', "#{arg[:locale]}.yml")
    arg[:pluralize] ||= plurals[arg[:locale]]
    hash = YAML.load(File.read(arg[:filepath], encoding: 'UTF-8')) #rubocop:disable Security/YAMLLoad
    hash.key?(arg[:locale]) or raise VariableError, %(expected :locale "#{arg[:locale]}" not found in :filepath "#{arg[:filepath].inspect}")
    i18n[arg[:locale]] = [flatten.call(hash[arg[:locale]]), arg[:pluralize]]
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pagy-3.14.0 lib/locales/utils/loader.rb
pagy-3.13.0 lib/locales/utils/loader.rb
pagy-3.12.0 lib/locales/utils/loader.rb
pagy-3.11.0 lib/locales/utils/loader.rb
pagy-3.10.0 lib/locales/utils/loader.rb
pagy-3.9.0 lib/locales/utils/loader.rb
pagy-3.8.3 lib/locales/utils/loader.rb
pagy-3.8.2 lib/locales/utils/loader.rb
pagy-3.8.1 lib/locales/utils/loader.rb
pagy-3.8.0 lib/locales/utils/loader.rb
pagy-3.7.5 lib/locales/utils/loader.rb
pagy-3.7.4 lib/locales/utils/loader.rb