Created
September 14, 2021 20:10
-
-
Save dorianmariecom/2a1b5fafe48b02fac91ceb382bcf35f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def easy_translate(original, from:, to:) | |
| capitalized = original[0] == original[0].upcase | |
| interpolations_in_original = original.scan(/%{[^}]+}/) | |
| spaces_before = original.scan(/\A */).first | |
| spaces_after = original.scan(/ *\z/).first | |
| translated_text = | |
| EasyTranslate.translate(original, from: from, to: to).strip | |
| translated_text = translated_text.gsub(/(.)% {/, '\1 %{') | |
| translated_text = translated_text.gsub("% {", "%{") | |
| translated_text = | |
| if capitalized | |
| translated_text[0].upcase + translated_text[1..] | |
| else | |
| translated_text[0].downcase + translated_text[1..] | |
| end | |
| bad_interpolations = translated_text.scan(/%{[^}]+}/) | |
| interpolations_in_original.size.times do |index| | |
| next if !bad_interpolations[index] || !interpolations_in_original[index] | |
| translated_text.gsub!( | |
| bad_interpolations[index], | |
| interpolations_in_original[index] | |
| ) | |
| end | |
| "#{spaces_before}#{translated_text}#{spaces_after}" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment