Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

move currency out of character translation into its own space

  • Loading branch information...
commit ca6794d8a485f7cdb4574e80a9361cc59535004d 1 parent 515b5f6
@rsl authored
View
53 lib/stringex/localization/default_conversions.rb
@@ -3,47 +3,50 @@ module Localization
module DefaultConversions
CHARACTERS = {
:and => "and",
- :number => "number",
:at => "at",
+ :degrees => "degrees",
+ :divide => "divide",
:dot => '\1 dot \2',
+ :ellipsis => "dot dot dot",
+ :equals => "equals",
+ :number => "number",
+ :percent => "percent",
+ :plus => "plus",
+ :slash => "slash",
+ :star => "star",
+ }
+
+ CURRENCIES = {
:dollars => '\1 dollars',
:dollars_cents => '\1 dollars \2 cents',
- :pounds => '\1 pounds',
- :pounds_pence => '\1 pounds \2 pence',
:euros => '\1 euros',
:euros_cents => '\1 euros \2 cents',
+ :pounds => '\1 pounds',
+ :pounds_pence => '\1 pounds \2 pence',
:yen => '\1 yen',
- :star => "star",
- :percent => "percent",
- :equals => "equals",
- :plus => "plus",
- :divide => "divide",
- :degrees => "degrees",
- :ellipsis => "dot dot dot",
- :slash => "slash"
}
HTML_ENTITIES = {
+ :amp => "and",
+ :cent => " cents",
+ :copy => "(c)",
+ :deg => " degrees ",
+ :divide => "divide",
:double_quote => "\"",
- :single_quote => "'",
:ellipsis => "...",
:en_dash => "-",
:em_dash => "--",
- :times => "x",
+ :frac14 => "one fourth",
+ :frac12 => "half",
+ :frac34 => "three fourths",
:gt => ">",
:lt => "<",
- :trade => "(tm)",
- :reg => "(r)",
- :copy => "(c)",
- :amp => "and",
:nbsp => " ",
- :cent => " cent",
:pound => " pound",
- :frac14 => "one fourth",
- :frac12 => "half",
- :frac34 => "three fourths",
- :divide => "divide",
- :deg => " degrees "
+ :reg => "(r)",
+ :single_quote => "'",
+ :times => "x",
+ :trade => "(tm)",
}
TRANSLITERATIONS = {}
@@ -63,11 +66,11 @@ module DefaultConversions
:one_eighth => "one eighth",
:three_eighths => "three eighths",
:five_eighths => "five eighths",
- :seven_eighths => "seven eighths"
+ :seven_eighths => "seven eighths",
}
class << self
- %w{characters html_entities transliterations vulgar_fractions}.each do |conversion_type|
+ %w{characters currencies html_entities transliterations vulgar_fractions}.each do |conversion_type|
define_method conversion_type do
const_get conversion_type.upcase
end
View
25 lib/stringex/string_extensions.rb
@@ -53,31 +53,40 @@ def convert_miscellaneous_characters(options = {})
options = stringex_default_options.merge(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_character(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
+
# Special rules for abbreviations
dummy.gsub!(/(\s|^)([[:alpha:]](\.[[:alpha:]])+(\.?)[[:alpha:]]*(\s|$))/) do |x|
x.gsub(".", "")
end
- # Back to normal rules
+ # Back to normal rules
misc_characters =
{
/\s*&\s*/ => :and,
/\s*#/ => :number,
/\s*@\s*/ => :at,
/(\S|^)\.(\S)/ => :dot,
- /(?:\s|^)\$(\d*)(?:\s|$)/ => :dollars,
- /(?:\s|^)£(\d*)(?:\s|$)/u => :pounds,
- /(?:\s|^)(\d*)(?:\s|$)/u => :euros,
- /(?:\s|^)¥(\d*)(?:\s|$)/u => :yen,
/\s*\*\s*/ => :star,
/\s*%\s*/ => :percent,
/\s*=\s*/ => :equals,
@@ -259,6 +268,10 @@ def stringex_translate_character(key)
Localization.translate(:characters, key)
end
+ def stringex_translate_currency(key)
+ Localization.translate(:currencies, key)
+ end
+
def stringex_translate_html_entity(key)
Localization.translate(:html_entities, key)
end
Please sign in to comment.
Something went wrong with that request. Please try again.