module Emoninja::I18n
Utility to translate emoji names to different languages
Constants
- FAIL_AFTER
- LOCAL_I18N
- YT_CONFIG
Public Class Methods
key(term, lang)
click to toggle source
# File lib/emoninja/i18n.rb, line 46 def key term, lang !lang.nil? && lang.to_s != 'en' && reversed_translations(lang)[term.downcase] || term end
Also aliased as: []
reversed_translations(lang)
click to toggle source
# File lib/emoninja/i18n.rb, line 35 def reversed_translations lang (@reversed_translations ||= {})[lang] ||= translations(lang).invert.map { |k, v| [k.downcase, v.downcase] }.to_h end
translations(lang)
click to toggle source
@param [String] lang the language to build a dictionary @param [Array | Hash] the array of words
# File lib/emoninja/i18n.rb, line 13 def translations lang file = LOCAL_I18N % { lang: lang } (@translations ||= {})[lang] ||= preload file keys = [Data.vocabulary, Data.argo].map(&:keys).reduce(:|) - @translations[lang].keys return @translations[lang] if keys.empty? require 'yandex-api' Yandex::API::Translate.load YT_CONFIG errors_count = 0 @translations[lang].merge!( keys.each_with_object({}) do |k, memo| begin result = Yandex::API::Translate.do(k, lang) memo[k] = result['text'].is_a?(Array) ? result['text'].join(' ') : result['text'] if result['code'] == 200 rescue break memo if (errors_count += 1) >= FAIL_AFTER end end ).tap { |data| File.write(file, data.to_yaml) } end
Private Class Methods
preload(file)
click to toggle source
# File lib/emoninja/i18n.rb, line 53 def preload file File.exist?(file) ? YAML.load(File.read(file)) : {} end