From 2170cb8586aeeb26920b4010fa2be604e293c6a0 Mon Sep 17 00:00:00 2001 From: Diego Pino Date: Wed, 4 Jul 2012 03:49:01 +0200 Subject: [PATCH] Script for replacing strings in Java files Uses a file in keys.pot format: - msgid: String to substitute - msgstr: New string FEA: ItEr76S04BugFixing --- scripts/replace-keys.rb | 84 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100755 scripts/replace-keys.rb diff --git a/scripts/replace-keys.rb b/scripts/replace-keys.rb new file mode 100755 index 000000000..c882fd4a7 --- /dev/null +++ b/scripts/replace-keys.rb @@ -0,0 +1,84 @@ +#!/usr/bin/ruby -w +# encoding: UTF-8 + +# Copyright (C) 2012 Diego Pino GarcĂ­a +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301, USA. + +# Reads a files in keys.pot format and replaces msgid for msgstr in +# list of files marked with '#: ' +# +# It doesn't work if msgid contains escaped characters +# Strings with several lines should be rewritten as one line only + + +require 'pp' + +def readfile(filename) + entries = Array.new + entry = Hash.new + files = Hash.new + + file = File.new(filename, "r") + while (line = file.gets) + line = line.strip + if (/^msgstr "(.*?)"/.match(line)) + if (!$1.empty?) + entry["msgstr"] = $1 + entry["files"] = files.keys + entries.push(entry) + + entry = Hash.new + files = Hash.new + end + elsif (/^msgid "(.*?)"/.match(line)) + if (!$1.empty?) + entry["msgid"] = $1 + end + elsif (/^#: (.*)/.match(line)) + if (!$1.empty?) + parts = $1.split(":") + key = parts[0] + files[key] = "" + end + end + end + file.close + + return entries +end + +def print_entries(entries) + entries.each { |entry| + puts "msgid: #{entry['msgid']}" + puts "msgstr: #{entry['msgstr']}" + puts "---\n" + } +end + +filename = "keys.pot"; + +entries = readfile(filename) +# print_entries(entries) + +entries.each { |entry| + entry["files"].each { |filename| + filename = "../#{filename}" + content = File.read(filename) + content = content.gsub(entry["msgid"], entry["msgstr"]) + File.open(filename, "w") { |file| file.puts content} + } +}