Created
July 12, 2016 10:43
-
-
Save chirauki/30956425f26a90e5d268094da0887ab1 to your computer and use it in GitHub Desktop.
Replace words on Abiquo language files
This file contains 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 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
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!).