-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBuildLocale
executable file
·202 lines (164 loc) · 7.64 KB
/
BuildLocale
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env ruby
require 'fileutils'
# The source root.
SRCROOT = ENV['SRCROOT'] || '.'
# Create the compiled locales directory.
# You can specify it as COMPILED_LOCALES_DIR in the environment,
# or it will be $SRCROOT/Compiled Locales.
compiledDir = ENV['COMPILED_LOCALES_DIR'] || File.join(SRCROOT, 'Compiled Locales')
Dir.mkdir(compiledDir) unless File.directory? compiledDir
# Get the locales source dir.
# You can specify it as LOCALES_DIR, but by default it's
# $SRCROOT/Locales
localesDir = ENV['LOCALES_DIR'] || File.join(SRCROOT, 'Locales')
# This is an addenda for the localization packs.
# If '$PRODUCE_APPLIED' == YES, we also make inspectable .nib
# files in the folder specified by '$APPLIED_DIR' or SRCROOT/Applied.
PRODUCE_APPLIED = (ENV['PRODUCE_APPLIED'] == 'YES')
if PRODUCE_APPLIED
APPLIED_DIR = ENV['APPLIED_DIR'] || File.join(SRCROOT, 'Applied')
Dir.mkdir APPLIED_DIR unless File.directory? APPLIED_DIR
end
# Each directory inside localesDir is a locale. It must
# be a Mac OS X-understood locale code (eg Italian or it),
# without .lproj.
locales = []
Dir.open(localesDir) do |d|
# non-hidden (non-.) directories, and dirs not starting with '_' are locales
locales = d.entries.select {
|x| x.match(/^[^\._]/) and File.directory?(File.join(localesDir, x))
}
end
# Get the base locale dir. It's either in the env as
# BASE_LOCALE_DIR; or it's en.lproj; or it's English.lproj.
ISO_NAMED_DIR = File.join(SRCROOT, 'en.lproj')
NEXT_NAMED_DIR = File.join(SRCROOT, 'English.lproj')
dirPath =
(ENV['BASE_LOCALE_DIR'] if ENV['BASE_LOCALE_DIR']) ||
(NEXT_NAMED_DIR if File.directory? NEXT_NAMED_DIR) ||
(ISO_NAMED_DIR if File.directory? ISO_NAMED_DIR)
raise "error: #{dirPath} is not a directory or doesn't exist" unless File.directory? dirPath
# compiledLocaleDirs contains the .lproj folders in the
# compiledDir for each locale (locale name -> lproj path)
compiledLocaleDirs = {}
appliedDirs = {}
locales.each do |locale|
compiledLocaleDir = File.join(compiledDir, locale + '.lproj')
Dir.mkdir(compiledLocaleDir) unless File.directory? compiledLocaleDir
compiledLocaleDirs[locale] = compiledLocaleDir
if PRODUCE_APPLIED
appliedDir = File.join(APPLIED_DIR, locale)
Dir.mkdir(appliedDir) unless File.directory? appliedDir
appliedDirs[locale] = appliedDir
end
end
# The meat of the thing -- iterate the source files and for
# each do some work with the localization.
Dir.open(dirPath).each do |file|
fullFilePath = File.join(dirPath, file)
case file
when /\.nib$/, /\.xib$/
# This is slightly tricky. We need ibtool to run and
# make us a very, very nice compiled .nib file out of
# the nib or xib.
compiledNibName = file[0, file.length - 3] + 'nib';
compiledLocaleDirs.each do |locale, compiledLocaleDir|
stringsFile = File.join(localesDir, locale, file + '.strings')
# This is hackish -- ideally, we'd fail if there's no
# .strings file, but some NIB files have no localizable content
# so we mark them by removing their .strings file from
# the locale dir.
if File.exists? stringsFile
compiledNibFile = File.join(compiledLocaleDir, compiledNibName)
appliedNibFile = File.join(appliedDirs[locale], file) if PRODUCE_APPLIED
# Rebuild applied and compiled if the original xib has changed.
if File.exist? compiledNibFile and File.stat(compiledNibFile).mtime < File.stat(fullFilePath).mtime
$stderr.puts "note: Removing file #{compiledNibFile} because the source file #{fullFilePath} is updated."
File.delete compiledNibFile
end
if PRODUCE_APPLIED
if File.exist? appliedNibFile and File.stat(appliedNibFile).mtime < File.stat(fullFilePath).mtime
$stderr.puts "note: Removing applied file #{appliedNibFile} because the source file #{fullFilePath} is updated."
File.delete appliedNibFile
end
end
# Also: rebuild if the .strings file has changed.
if File.exist? compiledNibFile and File.stat(compiledNibFile).mtime < File.stat(stringsFile).mtime
$stderr.puts "note: Removing file #{compiledNibFile} because the strings file #{stringsFile} is updated."
File.delete compiledNibFile
end
if PRODUCE_APPLIED
if File.exist? appliedNibFile and File.stat(appliedNibFile).mtime < File.stat(stringsFile).mtime
$stderr.puts "note: Removing applied file #{appliedNibFile} because the strings file #{stringsFile} is updated."
File.delete appliedNibFile
end
end
unless File.exist? compiledNibFile
$stderr.puts "note: #{compiledNibFile} does not exist, regenerating."
commandLine = ['/usr/bin/ibtool',
'--agent-name', 'net_infinite_labs_BuildLocaleIBToolAgent',
'--agent-timeout', '30',
'--strings-file', stringsFile,
'--compile', compiledNibFile,
fullFilePath]
$stderr.puts "Calling: #{commandLine.join ' '}"
fork do
exec(*commandLine)
end
Process.wait
end
if PRODUCE_APPLIED
unless File.exist? appliedNibFile
$stderr.puts "note: applied file #{appliedNibFile} does not exist, regenerating."
commandLine = ['/usr/bin/ibtool',
'--agent-name', 'net_infinite_labs_BuildLocaleIBToolAgent',
'--agent-timeout', '30',
'--strings-file', stringsFile,
'--write', appliedNibFile,
fullFilePath]
$stderr.puts "Calling: #{commandLine.join ' '}"
fork do
exec(*commandLine)
end
Process.wait
end
end
end
end
when /\.strings$/
# ignore xxx.nib.strings or yyy.xib.strings -- generated by CollectLocale.
unless file =~ /\.nib\.strings$/ or file =~ /\.xib\.strings$/
# Copy the localized file from the localeDir to the compiledLocaleDir
compiledLocaleDirs.each do |locale, compiledLocaleDir|
compiledStringsFile = File.join(compiledLocaleDir, file)
if File.exist? compiledStringsFile and File.stat(compiledStringsFile).mtime < File.stat(fullFilePath).mtime
$stderr.puts "note: Removing file #{compiledStringsFile} because the source file #{fullFilePath} is updated."
File.delete compiledStringsFile
end
unless File.exist? compiledStringsFile
$stderr.puts "note: #{compiledStringsFile} does not exist, regenerating."
FileUtils.cp File.join(localesDir, locale, file),
compiledStringsFile
end
end
end
when /^[^.]/ # another kind of file not starting with a dot. we don't work with hidden files!
compiledLocaleDirs.each do |locale, compiledLocaleDir|
localizedArbitraryFile = File.join(localesDir, locale, file)
# So: if BASE/xyz exists, and Locales/ab/xyz exists,
# we want to copy the latter into target/ab.lproj/xyz IF
# the compiled one does not exist OR is newer.
if File.exist? localizedArbitraryFile
compiledFile = File.join(compiledLocaleDir, file)
if File.exist? compiledFile and File.stat(compiledFile).mtime < File.stat(fullFilePath).mtime
$stderr.puts "note: Removing file #{compiledFile} because the source file #{fullFilePath} is updated."
File.delete compiledFile
end
unless File.exist? compiledFile
$stderr.puts "note: #{compiledFile} does not exist, copying from source."
FileUtils.cp localizedArbitraryFile, compiledFile
end
end
end
end
end