Class: Lhj::Trans::Helper
- Inherits:
-
Object
- Object
- Lhj::Trans::Helper
- Defined in:
- lib/lhj/helper/trans_helper.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#contain_zh_hk(input) ⇒ Object
contain hk char.
- #down_load_path ⇒ Object
- #down_load_yaml ⇒ Object
- #load_trans_map ⇒ Object
- #trans_zh_cn_str(input) ⇒ Object
- #trans_zh_hk_str(input) ⇒ Object
- #yaml_file ⇒ Object
Class Method Details
.instance ⇒ Object
7 8 9 |
# File 'lib/lhj/helper/trans_helper.rb', line 7 def self.instance @instance ||= new end |
Instance Method Details
#contain_zh_hk(input) ⇒ Object
contain hk char
36 37 38 39 40 41 42 |
# File 'lib/lhj/helper/trans_helper.rb', line 36 def contain_zh_hk(input) @trans_map ||= load_trans_map input.each_char do |c| return true if @trans_map[c] end false end |
#down_load_path ⇒ Object
22 23 24 |
# File 'lib/lhj/helper/trans_helper.rb', line 22 def down_load_path "http://#{Lhj::OSSConfig.oss_bucket}.#{Lhj::OSSConfig.oss_endpoint}/zh2hant.yml" end |
#down_load_yaml ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/lhj/helper/trans_helper.rb', line 26 def down_load_yaml require 'open-uri' URI.open(down_load_path) do |i| File.open(yaml_file, 'w+') do |f| f.write(i.read) end end end |
#load_trans_map ⇒ Object
15 16 17 18 19 20 |
# File 'lib/lhj/helper/trans_helper.rb', line 15 def load_trans_map require 'yaml' down_load_yaml unless File.exist?(yaml_file) contents = YAML.safe_load(File.open(yaml_file)) contents.to_hash end |
#trans_zh_cn_str(input) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/lhj/helper/trans_helper.rb', line 44 def trans_zh_cn_str(input) @trans_map_invert ||= load_trans_map.invert out = [] input.each_char do |c| out << (@trans_map_invert[c] || c) end out.join('') end |
#trans_zh_hk_str(input) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/lhj/helper/trans_helper.rb', line 53 def trans_zh_hk_str(input) @trans_map ||= load_trans_map out = [] input.each_char do |c| out << (@trans_map[c] || c) end out.join('') end |