Skip to content

Instantly share code, notes, and snippets.

@chirauki
Created July 12, 2016 10:43
Show Gist options
  • Save chirauki/30956425f26a90e5d268094da0887ab1 to your computer and use it in GitHub Desktop.
Save chirauki/30956425f26a90e5d268094da0887ab1 to your computer and use it in GitHub Desktop.
Replace words on Abiquo language files
#!/usr/bin/env ruby
require 'trollop'
require 'json'
require 'highline'
@opts = Trollop::options do
opt :unattended, "Do not ask for permission", :type => :boolean
opt :replace, "Replace some word in lang file", :type => :boolean
opt :source, "Word to replace", :type => :string
opt :target, "Replacement word", :type => :string
opt :lang_file, "UI Lang file to mangle", :type => :string, :required => true
end
Trollop::die "Need word to replace and something to replace it with!" if @opts[:replace] and (@opts[:replace] == nil or @opts[:target] == nil)
# Load lang file
file = File.read(@opts[:lang_file])
@data_hash = JSON.parse(file)
def replace
@data_hash.keys.each do |k|
if @data_hash[k].include? @opts[:source]
replaced = @data_hash[k].gsub(@opts[:source], @opts[:target])
puts "Changing message"
puts " '#{@data_hash[k]}' "
puts "with"
puts " '#{replaced}' "
@data_hash[k] = replaced
end
end
end
def ask_replace
cli = HighLine.new
@data_hash.keys.each do |k|
if @data_hash[k].include? @opts[:source]
replaced = @data_hash[k].gsub(@opts[:source], @opts[:target])
puts "Changing message"
puts " '#{@data_hash[k]}' "
puts "with"
puts " '#{replaced}' "
answer = cli.agree("Are you sure?")
@data_hash[k] = replaced if answer
end
end
end
def replace_message(msg)
msg.gsub!(@opts[:source], @opts[:target])
end
if @opts[:replace]
if @opts[:unattended]
replace
else
ask_replace
end
f = File.open(@opts[:lang_file], 'w')
f.write JSON.pretty_generate(@data_hash)
f.close
end
@chirauki
Copy link
Author

Usage:

./lang-mangle.rb -u -r -s Enterprise -t Tenant -l lang_en_US-labels.json

That would replace all occurrences of the word Enterprise with Tenant in language file lang_en_US-labels.json.

If you remove the -u parameter it will ask for confirmation in every match (and the file is only written at the end!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment