Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to keep the keys order when using remove-unused #147

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ $ i18n-tasks remove-unused
These tasks can infer [dynamic keys](#dynamic-keys) such as `t("category.\#{category.name}")` if you set
`search.strict` to false, or pass `--no-strict` on the command line.

If you want to keep the ordering from the original language file when using remove-unused, pass
`-k` or `--keep-order`.

### Normalize data

Sort the keys:
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ en:
locales implicitly.
confirm: Confirm automatically
data_format: 'Data format: %{valid_text}.'
keep_order: Keep the order of the keys
key_pattern: Filter by key pattern (e.g. 'common.*')
key_pattern_to_rename: Full key (pattern) to rename. Required
locale: :i18n_tasks.common.locale
Expand Down
10 changes: 9 additions & 1 deletion lib/i18n/tasks/command/commands/usages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ module Usages
'--[no-]strict',
t('i18n_tasks.cmd.args.desc.strict')

arg :keep_order,
'-k',
'--keep-order',
t('i18n_tasks.cmd.args.desc.keep_order')

cmd :find,
pos: '[pattern]',
desc: t('i18n_tasks.cmd.desc.find'),
Expand All @@ -35,13 +40,16 @@ def unused(opt = {})
cmd :remove_unused,
pos: '[locale ...]',
desc: t('i18n_tasks.cmd.desc.remove_unused'),
args: %i[locales out_format strict confirm]
args: %i[locales out_format strict confirm keep_order]

def remove_unused(opt = {})
unused_keys = i18n.unused_keys(opt.slice(:locales, :strict))
if unused_keys.present?
terminal_report.unused_keys(unused_keys)
confirm_remove_unused!(unused_keys, opt)
if opt[:'keep-order']
i18n.data.config = i18n.data.config.merge(sort: false)
end
removed = i18n.data.remove_by_key!(unused_keys)
log_stderr t('i18n_tasks.remove_unused.removed', count: unused_keys.leaves.count)
print_forest removed, opt
Expand Down
4 changes: 2 additions & 2 deletions lib/i18n/tasks/data/file_formats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def read_file(path)
::File.read(path, encoding: 'UTF-8')
end

def write_tree(path, tree)
hash = tree.to_hash(true)
def write_tree(path, tree, sort = true)
hash = tree.to_hash(sort)
adapter = self.class.adapter_name_for_path(path)
content = adapter_dump(hash, adapter)
# Ignore unchanged data
Expand Down
1 change: 1 addition & 0 deletions lib/i18n/tasks/data/file_system_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class FileSystemBase # rubocop:disable Metrics/ClassLength

def initialize(config = {})
self.config = config.except(:base_locale, :locales)
self.config[:sort] = !config[:keep_order]
@base_locale = config[:base_locale]
locales = config[:locales].presence
@locales = LocaleList.normalize_locale_list(locales || available_locales, base_locale, true)
Expand Down