Skip to content

Instantly share code, notes, and snippets.

@bsa7
Last active March 7, 2016 17:18
Show Gist options
  • Save bsa7/7bffa5e20a359195e349 to your computer and use it in GitHub Desktop.
Save bsa7/7bffa5e20a359195e349 to your computer and use it in GitHub Desktop.
recursive remove unneeded single and double quotes from rails localization *.yml files

Use:

  1. first: for testing purpose - create folder and place replacer.rb to it.

  2. second: create config/locales folder and place example.yml file to it.

  3. initialize git, add files and commit:

    git init
    git add .
    git commit -m 'initial commit before tests'
  1. run script:
    ruby replacer.rb
# for testing - place its file inside config/locales in fake rails folder and run ruby replacer.rb from root of this.
en:
test_yaml:
test_1: ':'
test_2: '{'
test_3: '}'
test_4: '['
test_5: ']'
test_6: ','
test_7: '&'
test_8: '*'
test_9: '#'
test_10: '?'
test_11: '|'
test_12: '-'
test_13: '<'
test_14: '>'
test_15: '='
test_16: '!'
test_17: '%'
test_18: '@'
test_19: '\'
test_20: '`'
test_21: '/'
test_22: 'inside : string'
test_23: 'inside { string'
test_24: 'inside } string'
test_25: 'inside [ string'
test_26: 'inside ] string'
test_27: 'inside , string'
test_28: 'inside & string'
test_29: 'inside * string'
test_30: 'inside # string'
test_31: 'inside ? string'
test_32: 'inside | string'
test_33: 'inside - string'
test_34: 'inside < string'
test_35: 'inside > string'
test_36: 'inside = string'
test_37: 'inside ! string'
test_38: 'inside % string'
test_39: 'inside @ string'
test_40: 'inside \ string'
test_41: 'inside ` string'
test_42: 'inside / string'
test_43: ': at begin of string'
test_44: '{ at begin of string'
test_45: '} at begin of string'
test_46: '[ at begin of string'
test_47: '] at begin of string'
test_48: ', at begin of string'
test_49: '& at begin of string'
test_50: '* at begin of string'
test_51: '# at begin of string'
test_52: '? at begin of string'
test_53: '| at begin of string'
test_54: '- at begin of string'
test_55: '< at begin of string'
test_56: '> at begin of string'
test_57: '= at begin of string'
test_58: '! at begin of string'
test_59: '% at begin of string'
test_60: '@ at begin of string'
test_61: '\ at begin of string'
test_62: '` at begin of string'
test_63: '/ at begin of string'
test_64: 'at end of string :'
test_65: 'at end of string {'
test_66: 'at end of string }'
test_67: 'at end of string ['
test_68: 'at end of string ]'
test_69: 'at end of string ,'
test_70: 'at end of string &'
test_71: 'at end of string *'
test_72: 'at end of string #'
test_73: 'at end of string ?'
test_74: 'at end of string |'
test_75: 'at end of string -'
test_76: 'at end of string <'
test_77: 'at end of string >'
test_78: 'at end of string ='
test_79: 'at end of string !'
test_80: 'at end of string %'
test_81: 'at end of string @'
test_82: 'at end of string \'
test_83: 'at end of string `'
test_84: 'at end of string /'
test_85: '%{param}'
test_86: '%{param} at begin'
test_87: 'at end %{param}'
test_88: 'insinde %{param} string'
#!/usr/bin/env ruby
system 'git add .'
system 'git commit -m "This is safe commit before making all changes."'
regexps = []
fs_1 = ':{}[],&*#?|-<>=!%@\`/'.split(//).map{|s|"\\"+s}.join
# 1. strings containing only those symbols: :{}[],&*#?|-<>=!%@\`/
regexps << "(?<=:)\s+[\"\']([^#{fs_1}])[\"\']$"
# 2. strings, which containing any symbols, within '#:' inside
regexps << "(?<=:)\s+[\"\']([^#{fs_1}][^#:]+)[\"\']$"
file_list = Dir.glob("config/locales/**/*")
file_list.each do |file_name|
next if File.directory?(file_name)
text = File.read(file_name)
new_contents = text
regexps.each do |r|
regexp = Regexp.new r
new_contents = new_contents.gsub(regexp, ' \1')
end
File.open(file_name, "w") {|file| file.puts new_contents }
end
exec 'git diff @~'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment