forked from FusionAuth/fusionauth-localization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.rb
executable file
·129 lines (110 loc) · 3.61 KB
/
update.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env ruby
require 'optparse'
require 'ostruct'
# Parse the options
options = OpenStruct.new
oparser = OptionParser.new do |opts|
opts.banner = 'Usage: update.rb'
opts.separator ''
opts.separator 'Description: Updates the translation files in theme and missing-translations.'
opts.separator ''
opts.separator 'Files in theme/ will be reordered to match the messages.properties (English) ordering of comments and keys.'\
' Files in missing-translations/ will be updated to reflect any missing translations (i.e. keys may be added or removed).'
opts.separator ''
opts.separator 'Options:'
opts.on_tail('-h', '--help', 'Display this message') do
puts opts
exit
end
end
oparser.parse!
# Update theme/ and missing-translations/ files.
WORKING_DIR = File.dirname(__FILE__)
def handleMultiLine(currentLine, lastPropertyName, isMultiLine)
propertyName = ''
propertyValue = ''
comment = ''
wasMultiLine = isMultiLine
if isMultiLine
propertyName = lastPropertyName
propertyValue = currentLine.strip().chomp('\\')
isMultiLine = /.*\\$/.match?(currentLine)
elsif /^[^#][^=]*=.*/.match(currentLine)
currentLine.strip.match(/^([^#][^=]*)=(.*)/) do |data|
propertyName = data[1]
propertyValue = data[2].chomp('\\')
isMultiLine = /.*\\$/.match?(data[2])
end
else
comment = currentLine.chomp
end
return propertyName, propertyValue, comment, isMultiLine, wasMultiLine
end
def loadMessages (file)
properties = {}
lastPropertyName = ''
isMultiLine = false
IO.foreach(file) do |currentLine|
data = handleMultiLine(currentLine, lastPropertyName, isMultiLine)
propertyName = data[0]
propertyValue = data[1]
isMultiLine = data[3]
wasMultiLine = data[4]
if propertyName != ''
if properties.has_key?(propertyName)
if !wasMultiLine
abort('Stopping! Duplicate key ' + propertyName + ' in file ' + file)
end
properties[propertyName] += " " + propertyValue
else
properties[propertyName] = propertyValue
end
properties[propertyName] = properties[propertyName].gsub(/\s+/, ' ')
end
lastPropertyName = propertyName
end
return properties
end
# Load messages.properties
mainFileTemplate = []
isMultiLine = false
IO.foreach("#{WORKING_DIR}/theme/messages.properties") do |currentLine|
data = handleMultiLine(currentLine, '', isMultiLine)
propertyName = data[0]
comment = data[2]
if propertyName != ''
mainFileTemplate.push(propertyName)
elsif !isMultiLine
mainFileTemplate.push(comment)
end
# Update isMultiLine now otherwise we will output blank lines when comment == '', but is
# actually 2nd part of a multiline.
isMultiLine = data[3]
end
englishMessages = loadMessages("#{WORKING_DIR}/theme/messages.properties")
Dir.foreach("#{WORKING_DIR}/theme") do |filename|
next if !filename.match?('properties')
translatedMessages = loadMessages("#{WORKING_DIR}/theme/#{filename}")
newTranslations = []
missingTranslations = []
mainFileTemplate.each do |line|
hasKey = translatedMessages.has_key?(line)
if (/^\s*(#|$)/.match?(line)) || hasKey
line += "=#{translatedMessages[line]}" if hasKey
newTranslations.push(line)
else
missingTranslations.push("#{line}=#{englishMessages[line]}")
end
end
File.open("#{WORKING_DIR}/theme/#{filename}", "w+") do |f|
f.puts(newTranslations)
end
filename = "#{WORKING_DIR}/missing-translations/#{filename}"
if missingTranslations.length == 0
File.delete(filename) if File.exist?(filename)
else
File.open(filename, "w+") do |f|
f.puts(missingTranslations)
end
end
end