diff --git a/CHANGELOG.md b/CHANGELOG.md index 01be6ee..aec5744 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # CHANGELOG ### Unreleased - [View Diff](https://github.com/westonganger/rails_i18n_manager/compare/v1.0.0...master) +- [#11](https://github.com/westonganger/rails_i18n_manager/pull/11) - Fix google translate and add specs - [#10](https://github.com/westonganger/rails_i18n_manager/pull/10) - Add missing pagination links to index pages ### v1.0.0 - [View Diff](https://github.com/westonganger/rails_i18n_manager/compare/9c8305c...v1.0.0) diff --git a/app/controllers/rails_i18n_manager/translations_controller.rb b/app/controllers/rails_i18n_manager/translations_controller.rb index 89b0afe..c388c51 100644 --- a/app/controllers/rails_i18n_manager/translations_controller.rb +++ b/app/controllers/rails_i18n_manager/translations_controller.rb @@ -142,7 +142,7 @@ def translate_missing locales = (app_locales || key_record.translation_app.additional_locales_array) ### Filter to just google translate supported languages - locales = locales.intersection(GoogleTranslate.supported_locales) + locales = locales.intersection(GoogleTranslate::SUPPORTED_LOCALES) default_translation_text = key_record.default_translation diff --git a/app/lib/rails_i18n_manager/google_translate.rb b/app/lib/rails_i18n_manager/google_translate.rb index e508a9f..68e2fc4 100644 --- a/app/lib/rails_i18n_manager/google_translate.rb +++ b/app/lib/rails_i18n_manager/google_translate.rb @@ -4,69 +4,31 @@ module GoogleTranslate 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?) + 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 nil + return false end - EasyTranslate.translate(text, from: from, to: to, key: api_key) - - if translation - str = translation.to_s - - if str.present? - ### Replace single quote html entity with single quote character - str = str.gsub("'", "'") + translated_text = EasyTranslate.translate(text, from: from, to: to, key: api_key) - if to.to_s == "es" - str = str.gsub("% {", " %{").strip - end + if translated_text.present? + ### Replace single quote html entity with single quote character + translated_text = translated_text.gsub("'", "'") - return str + if to.to_s == "es" + translated_text = translated_text.gsub("% {", " %{").strip end + + return translated_text end end ### List retrieved from Google Translate (2022) - @@supported_locales = ["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 - mattr_reader :supported_locales - - ### FOR official client - # require "google/cloud/translate/v2" ### Offical Google Translate with API Key - # def self.client - # @@client ||= begin - # api_key = RailsI18nManager.config.google_translate_api_key - # if Rails.env.test? || (api_key.blank? && Rails.env.development?) - # ### Skip Client - # nil - # else - # Google::Cloud::Translate::V2.new(key: api_key) - # end - # end - # end - # def self.supported_locales - # @@suported_locales ||= begin - # if client - # @@supported_locales = client.languages.map{|x| x.code} - # else - # [] - # end - # end - # end - # def self.translate(text, from:, to:) - # if client - # begin - # translation = client.translate(text, from: from, to: to) - # rescue Google::Cloud::InvalidArgumentError - # ### Error usually caused by an unsupported locale - # return nil - # end - # end - # end + SUPPORTED_LOCALES = ["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 end end diff --git a/lib/rails_i18n_manager/engine.rb b/lib/rails_i18n_manager/engine.rb index 65c5f70..a3b264c 100644 --- a/lib/rails_i18n_manager/engine.rb +++ b/lib/rails_i18n_manager/engine.rb @@ -4,6 +4,7 @@ require "zip" require "activerecord-import" require "csv" +require "easy_translate" module RailsI18nManager class Engine < ::Rails::Engine diff --git a/spec/unit/google_translate_spec.rb b/spec/unit/google_translate_spec.rb new file mode 100644 index 0000000..381d280 --- /dev/null +++ b/spec/unit/google_translate_spec.rb @@ -0,0 +1,55 @@ +require 'spec_helper' + +RSpec.describe RailsI18nManager::GoogleTranslate, type: :model do + + before do + allow(Rails.env).to receive(:test?).and_return(false) + allow(RailsI18nManager.config).to receive(:google_translate_api_key).and_return("some-api-key") + end + + context "translate" do + it "returns false for unsupported locales" do + expect(RailsI18nManager::GoogleTranslate.translate("foo", from: "bar", to: "baz")).to eq(false) + end + + it "returns false in test environment" do + allow(Rails.env).to receive(:test?).and_return(true) + expect(RailsI18nManager::GoogleTranslate.translate("foo", from: "en", to: "es")).to eq(false) + end + + it "returns false in development environment if api key is missing" do + allow(RailsI18nManager.config).to receive(:google_translate_api_key).and_return(nil) + allow(Rails.env).to receive(:development?).and_return(true) + expect(RailsI18nManager::GoogleTranslate.translate("foo", from: "en", to: "es")).to eq(false) + end + + it "returns false if HTML string provided" do + expect(RailsI18nManager::GoogleTranslate.translate("", from: "en", to: "es")).to eq(false) + + allow(EasyTranslate).to receive(:translate).and_return("foo") + expect(RailsI18nManager::GoogleTranslate.translate("", from: "en", to: "es")).to eq("foo") + end + + it "replaces single quote HTML entities with actual single quotes" do + allow(EasyTranslate).to receive(:translate).and_return("'foo'") + expect(RailsI18nManager::GoogleTranslate.translate("unrelated", from: "en", to: "es")).to eq("'foo'") + end + + it "replaces '% {' with ' %{' for es locale" do + allow(EasyTranslate).to receive(:translate).and_return("% {foo") + expect(RailsI18nManager::GoogleTranslate.translate("unrelated", from: "en", to: "es")).to eq("%{foo") + end + + it "returns nil if text was not able to be translated" do + allow(EasyTranslate).to receive(:translate).and_return(nil) + expect(RailsI18nManager::GoogleTranslate.translate("unrelated", from: "en", to: "es")).to eq(nil) + end + + it "returns translated text" do + allow(EasyTranslate).to receive(:translate).and_return("bonjour") + expect(RailsI18nManager::GoogleTranslate.translate("hello", from: "en", to: "fr")).to eq("bonjour") + end + end + +end