-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlocal_storage.rb
More file actions
57 lines (43 loc) · 962 Bytes
/
local_storage.rb
File metadata and controls
57 lines (43 loc) · 962 Bytes
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
# encoding: utf-8
require './xml_helper.rb'
require './lib/nicer_nil.rb'
class LocalStorage
DIR_NAME = 'tmp-local-storage'
def initialize()
system("mkdir -p #{DIR_NAME}")
end
def get_langs()
langs = []
in_dir do
Dir.glob('*_*').to_a.each do |filename|
langs << filename.split('_').first
end
end
langs.uniq - ['en']
end
def get_strings(lang)
xml_str = ''
in_dir do
filename = Dir.glob("#{lang}_*").sort.last
File.open(filename) {|f| xml_str = f.read()}
end
XMLHelper.xml_to_str(xml_str)
end
def put_strings(lang, strings)
xml_str = XMLHelper.str_to_xml(strings)
in_dir do
filename = Dir.glob("#{lang}_*").sort.last.next || lang + '_000001'
File.open(filename, 'w') {|f| f.write(xml_str)}
end
end
private
def in_dir
oldwd = Dir.getwd()
Dir.chdir(DIR_NAME)
begin
yield
rescue
end
Dir.chdir(oldwd)
end
end