Skip to content

Commit

Permalink
fixed so that untranslateable strings doesn't kill the script but ins…
Browse files Browse the repository at this point in the history
…tead warns
  • Loading branch information
sunkencity committed Mar 17, 2010
1 parent e008428 commit 2ce0d3b
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions lib/i18n_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,32 @@ def self.google_translate
unless needs_human_eyes?(default_locale_value)
interpolation_arguments= default_locale_value.scan(/\{\{(.*?)\}\}/).flatten

if interpolation_arguments.empty?
translation.value = GoogleLanguage.translate(default_locale_value, locale.code, Locale.default_locale.code)
translation.save!
else
placeholder_value = 990 # at least in :es it seems to leave a 3 digit number in the postion on the string
placeholders = {}

# replace {{interpolation_arguments}} with a numeric place holder
interpolation_arguments.each do |interpolation_argument|
default_locale_value.gsub!("{{#{interpolation_argument}}}", "#{placeholder_value}")
placeholders[placeholder_value] = interpolation_argument
placeholder_value += 1
begin

if interpolation_arguments.empty?
translation.value = GoogleLanguage.translate(default_locale_value, locale.code, Locale.default_locale.code)
translation.save!
else
placeholder_value = 990 # at least in :es it seems to leave a 3 digit number in the postion on the string
placeholders = {}

# replace {{interpolation_arguments}} with a numeric place holder
interpolation_arguments.each do |interpolation_argument|
default_locale_value.gsub!("{{#{interpolation_argument}}}", "#{placeholder_value}")
placeholders[placeholder_value] = interpolation_argument
placeholder_value += 1
end

# translate string
translated_value = GoogleLanguage.translate(default_locale_value, locale.code, Locale.default_locale.code)

# replace numeric place holders with {{interpolation_arguments}}
placeholders.each {|placeholder_value,interpolation_argument| translated_value.gsub!("#{placeholder_value}", "{{#{interpolation_argument}}}") }
translation.value = translated_value
translation.save!
end

# translate string
translated_value = GoogleLanguage.translate(default_locale_value, locale.code, Locale.default_locale.code)

# replace numeric place holders with {{interpolation_arguments}}
placeholders.each {|placeholder_value,interpolation_argument| translated_value.gsub!("#{placeholder_value}", "{{#{interpolation_argument}}}") }
translation.value = translated_value
translation.save!
rescue StandardError
puts "Failed to translate #{translation.inspect}"
end
end
end
Expand Down

0 comments on commit 2ce0d3b

Please sign in to comment.