From 8128b310a6c6afc367e20f729fc4aea459ad1ad4 Mon Sep 17 00:00:00 2001 From: Brendan Maclean Date: Thu, 13 Jun 2024 13:01:56 -0600 Subject: [PATCH] Generate locale translations for ledger entries, add default for missing translations --- .../ledger_accountable/ledger_accountable_generator.rb | 4 ++++ .../ledger_accountable/templates/ledger_entry.rb | 10 ++++++---- lib/locale/en.yml | 7 +++++++ 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 lib/locale/en.yml diff --git a/lib/generators/ledger_accountable/ledger_accountable_generator.rb b/lib/generators/ledger_accountable/ledger_accountable_generator.rb index 7e4da8a..7675a3d 100644 --- a/lib/generators/ledger_accountable/ledger_accountable_generator.rb +++ b/lib/generators/ledger_accountable/ledger_accountable_generator.rb @@ -18,6 +18,10 @@ def create_initializer_file template 'initializer.rb', 'config/initializers/ledger_accountable.rb' end + def copy_localization_file + copy_file '../../../locale/en.yml', 'config/locales/ledger.en.yml' + end + private def self.next_migration_number(path) diff --git a/lib/generators/ledger_accountable/templates/ledger_entry.rb b/lib/generators/ledger_accountable/templates/ledger_entry.rb index 3314221..8e1f365 100644 --- a/lib/generators/ledger_accountable/templates/ledger_entry.rb +++ b/lib/generators/ledger_accountable/templates/ledger_entry.rb @@ -6,16 +6,18 @@ class LedgerEntry < ApplicationRecord belongs_to :ledger_item, polymorphic: true, optional: false enum entry_type: { addition: 0, deletion: 1, modification: 2 } - + store :metadata, coder: JSON - + validates :amount, presence: true validates :entry_type, presence: true def to_itemized_s(line_type = :line) I18n.t! "#{TRANSLATION_PREFIX}.#{ledger_item_type.constantize.model_name.param_key}.#{line_type}", - metadata.symbolize_keys + metadata.symbolize_keys rescue I18n::MissingTranslationData, I18n::MissingInterpolationArgument - ledger_item_type + I18n.t "#{TRANSLATION_PREFIX}.default.#{line_type}", + default: ledger_item_type, + model_name: ledger_item_type.humanize end end diff --git a/lib/locale/en.yml b/lib/locale/en.yml new file mode 100644 index 0000000..8193d5a --- /dev/null +++ b/lib/locale/en.yml @@ -0,0 +1,7 @@ +en: + ledger: + default: + line: "%{model_name}" + addition: "%{model_name} added" + deletion: "%{model_name} removed" + modification: "%{model_name} modified"