Sha256: 466c397dfa30bc8f343bc71c01fc59d87a449afee7dc7a04c211a9ea4c2f443c

Contents?: true

Size: 1.3 KB

Versions: 18

Compression:

Stored size: 1.3 KB

Contents

# 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)|
              if v.is_a?(Hash)
                h.merge! flatten.call(v, "#{key}#{k}.")
              else
                code = %({"#{key}#{k}" => lambda{|vars|"#{v.gsub(/%{[^}]+?}/){|m| "\#{vars[:#{m[2..-2]}]||'#{m}'}" }}"}})
                h.merge! eval(code) # rubocop:disable Security/Eval
              end
            end
          end

# loader proc
lambda do |i18n, *args|
  i18n.clear
  args.each do |arg|
    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

18 entries across 18 versions & 2 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/pagy-4.11.0/lib/locales/utils/loader.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/pagy-4.11.0/lib/locales/utils/loader.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/pagy-4.11.0/lib/locales/utils/loader.rb
pagy-4.11.0 lib/locales/utils/loader.rb
pagy-4.10.2 lib/locales/utils/loader.rb
pagy-4.10.1 lib/locales/utils/loader.rb
pagy-4.10.0 lib/locales/utils/loader.rb
pagy-4.9.0 lib/locales/utils/loader.rb
pagy-4.8.1 lib/locales/utils/loader.rb
pagy-4.8.0 lib/locales/utils/loader.rb
pagy-4.7.1 lib/locales/utils/loader.rb
pagy-4.7.0 lib/locales/utils/loader.rb
pagy-4.6.0 lib/locales/utils/loader.rb
pagy-4.5.1 lib/locales/utils/loader.rb
pagy-4.5.0 lib/locales/utils/loader.rb
pagy-4.4.0 lib/locales/utils/loader.rb
pagy-4.3.0 lib/locales/utils/loader.rb
pagy-4.2.0 lib/locales/utils/loader.rb