Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix google translate and add specs #11

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
60 changes: 11 additions & 49 deletions app/lib/rails_i18n_manager/google_translate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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("&#39;", "'")
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("&#39;", "'")

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
1 change: 1 addition & 0 deletions lib/rails_i18n_manager/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "zip"
require "activerecord-import"
require "csv"
require "easy_translate"

module RailsI18nManager
class Engine < ::Rails::Engine
Expand Down
55 changes: 55 additions & 0 deletions spec/unit/google_translate_spec.rb
Original file line number Diff line number Diff line change
@@ -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("<foo>", from: "en", to: "es")).to eq(false)

allow(EasyTranslate).to receive(:translate).and_return("foo")
expect(RailsI18nManager::GoogleTranslate.translate("<foo", from: "en", to: "es")).to eq("foo")
expect(RailsI18nManager::GoogleTranslate.translate("foo>", 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("&#39;foo&#39;")
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