Class: Applocale::ParseStringsFile
- Inherits:
-
Object
- Object
- Applocale::ParseStringsFile
- Defined in:
- lib/applocale/Core/ParserStringFile/parse_strings_file.rb
Instance Attribute Summary collapse
-
#convert_file ⇒ Object
readonly
Returns the value of attribute convert_file.
-
#errorlist ⇒ Object
readonly
Returns the value of attribute errorlist.
-
#in_multiline_comments ⇒ Object
readonly
Returns the value of attribute in_multiline_comments.
-
#keys_list ⇒ Object
readonly
Returns the value of attribute keys_list.
-
#platform ⇒ Object
readonly
Returns the value of attribute platform.
-
#strings_keys ⇒ Object
readonly
Returns the value of attribute strings_keys.
Instance Method Summary collapse
-
#initialize(platform, langpathobj_list, convert_file) ⇒ ParseStringsFile
constructor
A new instance of ParseStringsFile.
- #parse_token(linenum, line, sep, lang, file) ⇒ Object
- #remove_escape(lang, key, content) ⇒ Object
- #to_parse_files(langpathobj_list) ⇒ Object
- #to_parse_strings_file(lang, strings_filepath) ⇒ Object
Constructor Details
#initialize(platform, langpathobj_list, convert_file) ⇒ ParseStringsFile
Returns a new instance of ParseStringsFile.
10 11 12 13 14 15 16 17 |
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 10 def initialize(platform, langpathobj_list, convert_file) @strings_keys = {} @keys_list = Array.new @errorlist = Array.new() @platform = platform @convert_file = convert_file self.to_parse_files(langpathobj_list) end |
Instance Attribute Details
#convert_file ⇒ Object (readonly)
Returns the value of attribute convert_file.
8 9 10 |
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 8 def convert_file @convert_file end |
#errorlist ⇒ Object (readonly)
Returns the value of attribute errorlist.
8 9 10 |
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 8 def errorlist @errorlist end |
#in_multiline_comments ⇒ Object (readonly)
Returns the value of attribute in_multiline_comments.
8 9 10 |
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 8 def in_multiline_comments @in_multiline_comments end |
#keys_list ⇒ Object (readonly)
Returns the value of attribute keys_list.
8 9 10 |
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 8 def keys_list @keys_list end |
#platform ⇒ Object (readonly)
Returns the value of attribute platform.
8 9 10 |
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 8 def platform @platform end |
#strings_keys ⇒ Object (readonly)
Returns the value of attribute strings_keys.
8 9 10 |
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 8 def strings_keys @strings_keys end |
Instance Method Details
#parse_token(linenum, line, sep, lang, file) ⇒ Object
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 |
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 99 def parse_token(linenum, line, sep, lang, file) n = 0 in_value = false in_quote = false in_escape = false value = "" for ch in line.chars prech = "" prech = line.chars[n-1] if n > 0 n += 1 if @in_multiline_comments if "#{prech}#{ch}" == "*/" @in_multiline_comments = false in_value = false value = "" end next end if not in_value if ch == "\"" in_quote = true in_value = true elsif ch != " " and ch != "\t" and ch != sep in_value = true value << ch end next end if in_escape value << prech value << ch in_escape = false elsif ch == "\\" in_escape = true elsif in_quote if ch == "\"" break else value << ch end else if ch == " " or ch == "\t" or ch == sep n -= 1 break elsif "#{prech}#{ch}" == "/*" @in_multiline_comments = true elsif "#{prech}#{ch}" == "//" return value, "" elsif ch == "#" return value, "" elsif "#{prech}#{ch}".length > 1 error = ErrorUtil::ParseLocalizedError::WrongFormat.new(file, lang, linenum) @errorlist.push(error) return value, "" else value << ch end end end return value, line[n..-1] end |
#remove_escape(lang, key, content) ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 164 def remove_escape(lang, key, content) value = ContentUtil.remove_escape(@platform, content) if @convert_file.has_parse_from_locale return @convert_file.load_parse_from_locale(lang.to_s, key, content, value) end return value end |
#to_parse_files(langpathobj_list) ⇒ Object
19 20 21 22 23 |
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 19 def to_parse_files(langpathobj_list) langpathobj_list.each do |langpathobj| self.to_parse_strings_file(langpathobj.lang, langpathobj.filepath) end end |
#to_parse_strings_file(lang, strings_filepath) ⇒ Object
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 |
# File 'lib/applocale/Core/ParserStringFile/parse_strings_file.rb', line 25 def to_parse_strings_file(lang, strings_filepath) puts "Start to Parse strings file: \"#{strings_filepath}\" ...".green @in_multiline_comments = false keyrowno = {} linenum = 0 begin IO.foreach(strings_filepath, mode: 'r:bom|utf-8') {|line| linenum += 1 line.strip! if !@in_multiline_comments next if line.start_with?('#') next if line.start_with?('//') end if line.length <= 0 next end while true key, line = parse_token(linenum, line, "=", lang, strings_filepath) line.strip! if not line.start_with?("=") if !@in_multiline_comments && line.length > 0 error = ErrorUtil::ParseLocalizedError::WrongFormat.new(strings_filepath, lang, linenum) @errorlist.push(error) end break end line.slice!(0) value, line = parse_token(linenum, line, ";", lang, strings_filepath) line.strip! if line.start_with?(";") line.slice!(0) else error = ErrorUtil::ParseLocalizedError::WrongFormat.new(strings_filepath, lang, linenum) @errorlist.push(error) key = nil value = nil break end if !ValidKey.is_validkey(@platform, key) error = ErrorUtil::ParseLocalizedError::InvalidKey.new(key, strings_filepath, lang, linenum) @errorlist.push(error) break end if @strings_keys[key].nil? @strings_keys[key] = Hash.new @keys_list.push(key) end if @strings_keys[key][lang.to_s].nil? @strings_keys[key][lang.to_s] = Hash.new @strings_keys[key][lang.to_s][:rowno] = linenum @strings_keys[key][lang.to_s][:value] = self.remove_escape(lang, key, value) keyrowno[key] = linenum else error = ErrorUtil::ParseLocalizedError::DuplicateKey.new(key, keyrowno[key], strings_filepath, lang, linenum) @errorlist.push(error) end if line.length <= 0 break end end } rescue Exception => e puts e. ErrorUtil::ParseLocalizedError::InvalidFile.new(strings_filepath).raise end end |