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