i18n-tasks
i18n-tasks copied to clipboard
Duplicate translations can cause unexpected behaviours (normalize removing many translations)
I have a project with many duplicate keys in a single locale caused by a misconfiguration (some locale files were not included). After fixing the configuration, running i18n-tasks normalize
dropped many translations (44 files changed, 72 insertions(+), 1872 deletions(-))
I believe that a warning should be printed in such cases. It can be added in the following function:
https://github.com/glebm/i18n-tasks/blob/1d1f4205e66a48b41dda4225f29d7806674e0249/lib/i18n/tasks/data/tree/siblings.rb#L184-L206
using the following patch:
def merge_node!(node, on_leaves_merge: nil) # rubocop:disable Metrics/AbcSize
if key_to_node.key?(node.key)
our = key_to_node[node.key]
return if our == node
+
+ if !our.children? && !node.children? && our.value != node.value && our.data[:path] != node.data[:path]
+ ::I18n::Tasks::Logging.log_warn(
+ 'Duplicate translation: '\
+ "#{our.full_key(root: false)} ⮕ #{our.value} in #{our.data[:path]},"\
+ " but ⮕ #{node.value} in #{node.data[:path]} (preferred)")
+ end
our.value = node.value if node.leaf?
our.data.merge!(node.data) if node.data?
if node.children?
Also, the default configuration file could document that file patterns specified under data.read
have a meaningful order. Files appearing later in the list will be preferred for translations in case of duplicate translations.
Duplicate translations are common when they come from data.external
.
If we want to print a warning, we should print it only for duplicate translations within data.read
, which may be trickier.
Also, the default configuration file could document that file patterns specified under data.read have a meaningful order. Files appearing later in the list will be preferred for translations in case of duplicate translations.
Sounds good