Module: RailsI18nManager::GoogleTranslate

Defined in:
app/lib/rails_i18n_manager/google_translate.rb

Constant Summary collapse

SUPPORTED_LOCALES =

List retrieved from Google Translate (2022)

["af", "am", "ar", "az", "be", "bg", "bn", "bs", "ca", "ceb", "co", "cs", "cy", "da", "de", "el", "en", "eo", "es", "et", "eu", "fa", "fi", "fr", "fy", "ga", "gd", "gl", "gu", "ha", "haw", "he", "hi", "hmn", "hr", "ht", "hu", "hy", "id", "ig", "is", "it", "iw", "ja", "jw", "ka", "kk", "km", "kn", "ko", "ku", "ky", "la", "lb", "lo", "lt", "lv", "mg", "mi", "mk", "ml", "mn", "mr", "ms", "mt", "my", "ne", "nl", "no", "ny", "or", "pa", "pl", "ps", "pt", "ro", "ru", "rw", "sd", "si", "sk", "sl", "sm", "sn", "so", "sq", "sr", "st", "su", "sv", "sw", "ta", "te", "tg", "th", "tk", "tl", "tr", "tt", "ug", "uk", "ur", "uz", "vi", "xh", "yi", "yo", "zh", "zh-CN", "zh-TW", "zu"].freeze

Class Method Summary collapse

Class Method Details

.translate(text, from:, to:) ⇒ Object



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
# File 'app/lib/rails_i18n_manager/google_translate.rb', line 4

def self.translate(text, from:, to:)
  api_key = RailsI18nManager.config.google_translate_api_key

  if !SUPPORTED_LOCALES.include?(to.to_s) || Rails.env.test? || (api_key.blank? && Rails.env.development?)
    return false
  end

  if text.include?("<") && text.include?(">")
    ### Dont translate any HTML strings
    return false
  end

  translated_text = EasyTranslate.translate(text, from: from, to: to, key: api_key)

  if translated_text.present?
    ### Replace single quote html entity with single quote character
    translated_text = translated_text.gsub("&#39;", "'")

    if to.to_s == "es"
      translated_text = translated_text.gsub("% {", " %{").strip
    end

    return translated_text
  end
end