Class: YorikLangHandler

Inherits:
YorikLangHandlerDefault show all
Defined in:
Plugins/yorik_langhandler.rb

Overview

Rewriting of basic Sketchup API class to make locales for plugins.

See Also:

Author:

  • Yurij Kulchevich aka yorik1984

Since:

  • 1.0

Instance Attribute Summary collapse

Attributes inherited from YorikLangHandlerDefault

#strings

Instance Method Summary collapse

Methods inherited from YorikLangHandlerDefault

#[], #check_default_ini_file, self_file_name

Constructor Details

#initialize(lh_data) ⇒ YorikLangHandler

Initializing object of YorikLangHandler class.

See Also:

  • YorikLangHandlerDefault::initialize

Since:

  • 1.0


166
167
168
169
170
171
# File 'Plugins/yorik_langhandler.rb', line 166

def initialize(lh_data)
  @lh_data = lh_data
  super(lh_data)
  dir_locale_list
  make_error_string
end

Instance Attribute Details

#lh_dataObject (readonly)

Since:

  • 1.0


160
161
162
# File 'Plugins/yorik_langhandler.rb', line 160

def lh_data
  @lh_data
end

#localeObject (readonly)

Since:

  • 1.0


160
161
162
# File 'Plugins/yorik_langhandler.rb', line 160

def locale
  @locale
end

Instance Method Details

#dir_locale_listArray

Making list of directories in "Resources" folder.

Since:

  • 1.0


190
191
192
193
194
195
196
197
198
199
# File 'Plugins/yorik_langhandler.rb', line 190

def dir_locale_list
  entries = Dir.entries(@resources_dir)
  entries.delete_if { |elem| elem == "." || elem == ".." }
  sud_dir_locale_list = []
  entries.each do |entrie|
    sub_dir = File.join(@resources_dir, entrie)
    sud_dir_locale_list.push entrie if Dir.exist?(sub_dir)
  end
  @dir_locale_list = sud_dir_locale_list.sort!
end

#find_strings_fileString

Finding strings file using locale data

Since:

  • 1.0


248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'Plugins/yorik_langhandler.rb', line 248

def find_strings_file
  locale = ""
  line = File.open(@lang_ini_path, 'r:BOM|UTF-8').readlines[0]
  locale = line.chomp
  @locale = locale
  strings_file =
      File.join(@resources_dir, locale, @plugin_id + @locale_file_type)
  return strings_file if File.exist?(strings_file)
  @strings_file_missing_error = true
  @locale = @default_locale
  write_locale_to_file(@default_locale)
  strings_file =
      File.join(@resources_dir, @default_locale, @plugin_id + @locale_file_type)
end

#locale_mbox(inputbox_window_name) ⇒ Sketcup::UI

Create Sketchup inputbox with parameters

Since:

  • 1.0


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'Plugins/yorik_langhandler.rb', line 268

def locale_mbox(inputbox_window_name)
  locale_dir_locale_list = make_name_with_locale
  current_locale = @default_locale
  UI.messagebox(@msg_errors[:missing_strings] +
                @msg_errors[:missing_strings_files] +
                @msg_errors[:unusing_string]) if @strings_file_missing_error
  UI.messagebox(@msg_errors[:first_line] +
                @msg_errors[:wrong_strings_files] +
                @msg_errors[:locale_code]) if @strings_error
  @strings_error              = false
  @strings_file_missing_error = false
  prompts = [inputbox_window_name]
  defaults = []
  defaults[0] = @default_lang_name
  list = []
  list[0] = ""
  locale_dir_locale_list.each_value { |value|
    list[0] = list[0] + value.to_s + "|" }
  inputbox = UI.inputbox(prompts, defaults, list, inputbox_window_name)
  current_locale = locale_dir_locale_list.key(inputbox[0]).to_s
  write_locale_to_file(current_locale) unless inputbox[0].nil?
end

#make_error_stringObject

Stack of translated errors messages for service using.

Since:

  • 1.0


176
177
178
179
180
181
182
183
184
# File 'Plugins/yorik_langhandler.rb', line 176

def make_error_string
  @yorik_inner_lib_lh = YorikLangHandlerDefault.new(lh_data)
  @msg_errors = { first_line: @yorik_inner_lib_lh["Empty localization name in 1st line in file(s):"] + "\n",
         wrong_strings_files: "",
                 locale_code: @yorik_inner_lib_lh["It will be using locale code in language selector. "] + @yorik_inner_lib_lh["Please, see more in help."],
             missing_strings: @yorik_inner_lib_lh["Missing .strings files:"] + "\n",
       missing_strings_files: "",
              unusing_string: @yorik_inner_lib_lh["Translate for this language(s) will be not supporting."] }
end

#make_name_with_localeHash

Associate locale code(dir name) with language name which can be finding in .strings. Adding dir only with valid existing .strings.

Since:

  • 1.0


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'Plugins/yorik_langhandler.rb', line 208

def make_name_with_locale
  name_with_locale = {}
  @dir_locale_list.each do |dir|
    strings_file_path = File.join(@resources_dir, dir, @plugin_id + @locale_file_type)
    strings_file_name = strings_file_path.to_s + "\n"
    unless File.exist?(strings_file_path)
      @strings_file_missing_error = true
      missing_strings_files = { missing_strings_files: strings_file_name }
      @msg_errors.merge!(missing_strings_files)
      next
    end
    line = File.open(strings_file_path, 'r:BOM|UTF-8').readlines[0]
    scan_slash = line.scan(/(\/\/)/)
    scan_word  = line.scan(/[[:word:]]/)
    scan_slash = [[""]] if scan_slash[0].nil?
    if scan_slash[0].join == "//" && scan_word.size != 0
      name_with_locale[dir] = scan_word.join
    else
      name_with_locale[dir] = dir
      wrong_strings_files = { wrong_strings_files: strings_file_name }
      @msg_errors.merge!(wrong_strings_files)
      @strings_error = true
    end
  end
  name_with_locale
end

#write_locale_to_file(current_locale) ⇒ Object

Write selected locale to file

Since:

  • 1.0


239
240
241
# File 'Plugins/yorik_langhandler.rb', line 239

def write_locale_to_file(current_locale)
  File.open(@lang_ini_path, 'w'){ |file| file.write current_locale}
end