-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added multi-language - simpler I18N format - faster pagy_t method
- Loading branch information
Showing
13 changed files
with
218 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# See https://ddnexus.github.io/pagy/api/frontend#i18n | ||
# frozen_string_literal: true | ||
|
||
# this file returns the I18n hash used as default alternative to the i18n gem | ||
|
||
Hash.new{|h,_| h.first[1]}.tap do |i18n| # first loaded locale used as default | ||
i18n.define_singleton_method(:load) do |*args| | ||
# eval: we don't need to keep the loader proc in memory | ||
eval(Pagy.root.join('locales', 'utils', 'loader.rb').read).call(i18n, *args) #rubocop:disable Security/Eval | ||
end | ||
i18n.define_singleton_method(:t) do |locale, path, vars={}| | ||
data, pluralize = self[locale] | ||
translate = data[path] || vars[:count] && data[path+=".#{pluralize.call(vars[:count])}"] or return %([translation missing: "#{path}"]) | ||
translate.call(vars) | ||
end | ||
i18n.load(locale: 'en') | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# 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 | ||
p11n = 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| | ||
arg[:filepath] ||= Pagy.root.join('locales', "#{arg[:locale]}.yml") | ||
arg[:pluralize] ||= p11n[arg[:locale]] | ||
hash = YAML.load(File.read(arg[:filepath], encoding: 'UTF-8')) #rubocop:disable Security/YAMLLoad | ||
hash.key?(arg[:locale]) or raise ArgumentError, %(Pagy::I18n.load: :locale "#{arg[:locale]}" not found in :filepath "#{arg[:filepath].inspect}") | ||
i18n[arg[:locale]] = [flatten.call(hash[arg[:locale]]), arg[:pluralize]] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
# See the Pagy documentation: https://ddnexus.github.io/pagy/extras/i18n | ||
# frozen_string_literal: true | ||
|
||
class Pagy | ||
# Use ::I18n gem | ||
module Frontend | ||
|
||
::I18n.load_path += Dir[Pagy.root.join('locales', '*.yml')] | ||
|
||
# Override the built-in pagy_t | ||
def pagy_t(*args) | ||
::I18n.t(*args) | ||
end | ||
Pagy::I18n.clear.instance_eval { undef :load; undef :t } # unload the pagy default constant for efficiency | ||
|
||
# no :pagy_without_i18n alias with the i18n gem | ||
def pagy_t_with_i18n(*args) ::I18n.t(*args) end | ||
alias :pagy_t :pagy_t_with_i18n | ||
|
||
end | ||
end |
Oops, something went wrong.