Last active
January 3, 2017 02:58
-
-
Save johncarney/e05aaa9d7cec5d934ce0524f6613cc6e to your computer and use it in GitHub Desktop.
Lists conflicting translations from your Rails locale files. Note that it will only detect conflicts between different locale files. Conflicts within a single locale file will not be detected.
This file contains hidden or 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
| #!/usr/bin/env ruby | |
| # Usage: ruby rails-locales-conflicts.rb [<rails root>] | |
| require "pathname" | |
| require "yaml" | |
| class Translations | |
| def initialize | |
| @translations = Hash.new do |h, k| | |
| h[k] = {} | |
| end | |
| end | |
| def merge_locale!(locale, prefix: [], source:) | |
| locale.each do |key, value| | |
| prefixed_key = [ *prefix, key ] | |
| case value | |
| when Hash | |
| merge_locale!(value, prefix: prefixed_key, source: source) | |
| else | |
| add_translation(prefixed_key, value, source: source) | |
| end | |
| end | |
| end | |
| def conflicts | |
| @translations.select { |_, v| v.size > 1 } | |
| end | |
| private | |
| def add_translation(key, value, source:) | |
| @translations[key].merge!(source => value) | |
| end | |
| end | |
| locales_dir = Pathname(ARGV.first || ".").join("config", "locales") | |
| locale_files = Pathname.glob(locales_dir.join("**", "*.yml")) | |
| translations = locale_files.each_with_object(Translations.new) do |locale_file, txns| | |
| source = locale_file.relative_path_from(locales_dir).to_s | |
| txns.merge_locale! YAML.load(locale_file.read), source: source | |
| end | |
| puts translations.conflicts.map { |k, v| "#{k.inspect} => #{v.inspect}" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment