Browse files

try to not regex all the currency changes if there's no currency symbols

  • Loading branch information...
1 parent 301ebc2 commit 48683a9bc32fa1066d6f607b9aceed1ebd87a645 @rsl committed Mar 20, 2013
View
8 lib/stringex/localization.rb
@@ -86,6 +86,10 @@ def reset!
@backend = @translations = @locale = @default_locale = nil
end
+ def currencies_supported_regex
+ Regexp.new DefaultConversions::CURRENCIES_SUPPORTED.join('|')
+ end
+
private
def initial_translation(scope, key, locale)
@@ -97,8 +101,8 @@ def initial_translation(scope, key, locale)
end
def default_conversion(scope, key)
- return unless Stringex::Localization::DefaultConversions.respond_to?(scope)
- Stringex::Localization::DefaultConversions.send(scope)[key]
+ return unless DefaultConversions.respond_to?(scope)
+ DefaultConversions.send(scope)[key]
end
end
end
View
4 lib/stringex/localization/default_conversions.rb
@@ -1,3 +1,5 @@
+# encoding: UTF-8
+
module Stringex
module Localization
module DefaultConversions
@@ -26,6 +28,8 @@ module DefaultConversions
:yen => '\1 yen',
}
+ CURRENCIES_SUPPORTED = %w{$ £ € ¥}
+
HTML_ENTITIES = {
:amp => "and",
:cent => " cents",
View
40 lib/stringex/string_extensions.rb
@@ -54,25 +54,27 @@ def convert_miscellaneous_characters(options = {})
dummy = dup.gsub(/\s*\.{3,}\s*/, " #{stringex_translate_character(:ellipsis)} ") # Catch ellipses before single dot rule!
- # Special rules for money
- # Complex currency first
- {
- /(?:\s|^)\$(\d+)\.(\d+)(?:\s|$)/ => :dollars_cents,
- /(?:\s|^)£(\d+)\.(\d+)(?:\s|$)/u => :pounds_pence,
- /(?:\s|^)(\d+)\.(\d+)(?:\s|$)/u => :euros_cents,
- }.each do |found, key|
- replaced = stringex_translate_currency(key)
- dummy.gsub!(found, " #{replaced} ")
- end
- # Simple currency last
- {
- /(?:\s|^)\$(\d*)(?:\s|$)/ => :dollars,
- /(?:\s|^)£(\d*)(?:\s|$)/u => :pounds,
- /(?:\s|^)(\d*)(?:\s|$)/u => :euros,
- /(?:\s|^)¥(\d*)(?:\s|$)/u => :yen,
- }.each do |found, key|
- replaced = stringex_translate_currency(key)
- dummy.gsub!(found, " #{replaced} ")
+ if dummy =~ Localization.currencies_supported_regex
+ # Special rules for money
+ # Complex currency first
+ {
+ /(?:\s|^)\$(\d+)\.(\d+)(?:\s|$)/ => :dollars_cents,
+ /(?:\s|^)£(\d+)\.(\d+)(?:\s|$)/u => :pounds_pence,
+ /(?:\s|^)(\d+)\.(\d+)(?:\s|$)/u => :euros_cents,
+ }.each do |found, key|
+ replaced = stringex_translate_currency(key)
+ dummy.gsub!(found, " #{replaced} ")
+ end
+ # Simple currency last
+ {
+ /(?:\s|^)\$(\d*)(?:\s|$)/ => :dollars,
+ /(?:\s|^)£(\d*)(?:\s|$)/u => :pounds,
+ /(?:\s|^)(\d*)(?:\s|$)/u => :euros,
+ /(?:\s|^)¥(\d*)(?:\s|$)/u => :yen,
+ }.each do |found, key|
+ replaced = stringex_translate_currency(key)
+ dummy.gsub!(found, " #{replaced} ")
+ end
end
# Special rules for abbreviations

0 comments on commit 48683a9

Please sign in to comment.