-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse_rtl_text.rb
66 lines (53 loc) · 1.99 KB
/
reverse_rtl_text.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#! /usr/bin/env ruby
require 'nokogiri'
### Methods
def parse_directory(directory_path)
Dir["#{directory_path.chomp('/')}/**/*.xml"].each { |file_path| parse_file(file_path) }
end
def parse_file(file_path)
return puts "[WARN] #{file_path} is not an XML file; skipping." unless file_path.match(/\.xml$/)
doc = File.open(file_path) { |file| Nokogiri::XML(file) }
# Valid translation files have specific root nodes
return puts "[WARN] #{file_path} contains no expected nodes; skipping." if doc.xpath('//LanguageInfo', '//LanguageData').length <= 0
# Search all leaf nodes since that's where all the translation strings are
doc.xpath('//*[not(*)]').each do |node|
node = reverse_node(node)
end
File.write(file_path, doc.to_xml)
puts "[INFO] #{file_path} parsed and strings reversed!"
end
def reverse_node(node)
if node.content.match(/(\p{Arabic})|(\p{Hebrew})/) then
words = node.content.split
words.each do |word|
# If there are any placeholders (e.g. {0}, {PAWN_FIRST_NAME}) in the word,
# we wanna preserve that as-is. So, we'll build a list of placeholders
# and their index in the word ( e.g. [["{0}", 2], ["{PAWN}", 5]])...
placeholders = []
word.scan(/{.*?}/) { |match| placeholders << [match, $~.offset(0)[0]] }
placeholders.each { |placeholder| word.sub!(placeholder[0], '') } if placeholders
word.reverse! if word.match(/(\p{Arabic})|(\p{Hebrew})/)
# ...then use it to put the placeholders back in place after we reverse the word
placeholders.each { |placeholder| word.insert(placeholder[1], placeholder[0]) } if placeholders
end
words.reverse!
node.content = words.join(' ')
return node
end
end
###
### Run script
if ARGV.length != 1 then
puts "Usage:"\
"reverse_rtl_text.rb [file-path|directory-path]"
exit
end
path = ARGV[0]
if File.directory?(path) then
parse_directory(path)
elsif File.file?(path) then
parse_file(path)
else
puts "[EROR] Unknown file or directory path: #{path}"
exit
end