diff --git a/config/locales/en.yml b/config/locales/en.yml index c82fce8c..baa6a92c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -11,6 +11,7 @@ en: desc: all_locales: Do not expect key patterns to start with a locale, instead apply them to all locales implicitly. + config: Override config location confirm: Confirm automatically data_format: 'Data format: %{valid_text}.' keep_order: Keep the order of the keys diff --git a/lib/i18n/tasks/base_task.rb b/lib/i18n/tasks/base_task.rb index 5388d1f3..6ebb1bdd 100644 --- a/lib/i18n/tasks/base_task.rb +++ b/lib/i18n/tasks/base_task.rb @@ -39,7 +39,8 @@ class BaseTask include Data include Stats - def initialize(config = {}) + def initialize(config = {}, config_file: nil) + @config_override = config_file self.config = config || {} end diff --git a/lib/i18n/tasks/cli.rb b/lib/i18n/tasks/cli.rb index 0f09cd77..ba32a8b0 100644 --- a/lib/i18n/tasks/cli.rb +++ b/lib/i18n/tasks/cli.rb @@ -34,7 +34,18 @@ def start(argv) end def run(argv) - I18n.with_locale(base_task.internal_locale) do + argv.each_with_index do |arg, i| + if ["--config", "-c"].include?(arg) + if File.exist?(argv[i+1]) + @config_file = argv[i+1] + break + else + error "Config file doesn't exist: #{argv[i+1]}", 128 + end + end + end + + I18n.with_locale(base_task(config_file: @config_file).internal_locale) do name, *options = parse!(argv.dup) context.run(name, *options) end @@ -52,8 +63,8 @@ def commands private - def base_task - @base_task ||= I18n::Tasks::BaseTask.new + def base_task(config_file: nil) + @base_task ||= I18n::Tasks::BaseTask.new(config_file: config_file) end def parse!(argv) diff --git a/lib/i18n/tasks/command/commands/health.rb b/lib/i18n/tasks/command/commands/health.rb index 2b5aa258..b6ecd3bd 100644 --- a/lib/i18n/tasks/command/commands/health.rb +++ b/lib/i18n/tasks/command/commands/health.rb @@ -9,7 +9,7 @@ module Health cmd :health, pos: '[locale ...]', desc: t('i18n_tasks.cmd.desc.health'), - args: %i[locales out_format] + args: %i[locales out_format config] def health(opt = {}) forest = i18n.data_forest(opt[:locales]) diff --git a/lib/i18n/tasks/command/options/common.rb b/lib/i18n/tasks/command/options/common.rb index 341cbf05..49775372 100644 --- a/lib/i18n/tasks/command/options/common.rb +++ b/lib/i18n/tasks/command/options/common.rb @@ -28,6 +28,12 @@ module Common '--value VALUE', t('i18n_tasks.cmd.args.desc.value') + arg :config, + '-c', + '--config FILE', + t('i18n_tasks.cmd.args.desc.config') + + def arg_or_pos!(key, opts) opts[key] ||= opts[:arguments].try(:shift) end diff --git a/lib/i18n/tasks/configuration.rb b/lib/i18n/tasks/configuration.rb index 47026048..b816146d 100644 --- a/lib/i18n/tasks/configuration.rb +++ b/lib/i18n/tasks/configuration.rb @@ -20,7 +20,7 @@ def config ].freeze def file_config - file = CONFIG_FILES.detect { |f| File.exist?(f) } + file = @config_override || CONFIG_FILES.detect { |f| File.exist?(f) } # rubocop:disable Security/Eval config = file && YAML.load(eval(Erubi::Engine.new(File.read(file, encoding: 'UTF-8')).src)) # rubocop:enable Security/Eval