Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

move default conversions to a place a little less in your face for my…

… tastes and some related refactors
  • Loading branch information...
commit 989fc9667327a94ed80f65e9cc7e065094827bff 1 parent a99ec71
@rsl authored
View
15 lib/stringex/localization.rb
@@ -1,5 +1,11 @@
+# encoding: UTF-8
+
+require 'stringex/localization/default_conversions'
+
module Stringex
module Localization
+ include DefaultConversions
+
class << self
attr_writer :translations, :backend
@@ -30,9 +36,9 @@ def translate(scope, key, options = {})
return translation unless translation.to_s.empty?
if locale != default_locale
- translate(scope, key, options.merge(:locale => default_locale))
+ translate scope, key, options.merge(:locale => default_locale)
else
- options[:default]
+ default_conversion(scope, key) || options[:default]
end
end
@@ -89,6 +95,11 @@ def initial_translation(scope, key, locale)
translations[locale][scope][key.to_sym]
end
end
+
+ def default_conversion(scope, key)
+ return unless Stringex::Localization::DefaultConversions.respond_to?(scope)
+ Stringex::Localization::DefaultConversions.send(scope)[key]
+ end
end
end
end
View
78 lib/stringex/localization/default_conversions.rb
@@ -0,0 +1,78 @@
+module Stringex
+ module Localization
+ module DefaultConversions
+ CHARACTERS = {
+ :and => "and",
+ :number => "number",
+ :at => "at",
+ :dot => '\1 dot \2',
+ :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',
+ :yen => '\1 yen',
+ :star => "star",
+ :percent => "percent",
+ :equals => "equals",
+ :plus => "plus",
+ :divide => "divide",
+ :degrees => "degrees",
+ :ellipsis => "dot dot dot",
+ :slash => "slash"
+ }
+
+ HTML_ENTITIES = {
+ :double_quote => "\"",
+ :single_quote => "'",
+ :ellipsis => "...",
+ :en_dash => "-",
+ :em_dash => "--",
+ :times => "x",
+ :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 "
+ }
+
+ TRANSLITERATIONS = {}
+
+ VULGAR_FRACTIONS = {
+ :one_fourth => "one fourth",
+ :half => "half",
+ :three_fourths => "three fourths",
+ :one_third => "one third",
+ :two_thirds => "two thirds",
+ :one_fifth => "one fifth",
+ :two_fifths => "two fifths",
+ :three_fifths => "three fifths",
+ :four_fifths => "four fifths",
+ :one_sixth => "one sixth",
+ :five_sixths => "five sixths",
+ :one_eighth => "one eighth",
+ :three_eighths => "three eighths",
+ :five_eighths => "five eighths",
+ :seven_eighths => "seven eighths"
+ }
+
+ class << self
+ %w{characters html_entities transliterations vulgar_fractions}.each do |conversion_type|
+ define_method conversion_type do
+ const_get conversion_type.upcase
+ end
+ end
+ end
+ end
+ end
+end
View
76 lib/stringex/string_extensions.rb
@@ -2,72 +2,6 @@
module Stringex
module StringExtensions
- DEFAULT_CHARACTER_CONVERSIONS =
- {
- :and => "and",
- :number => "number",
- :at => "at",
- :dot => '\1 dot \2',
- :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',
- :yen => '\1 yen',
- :star => "star",
- :percent => "percent",
- :equals => "equals",
- :plus => "plus",
- :divide => "divide",
- :degrees => "degrees",
- :ellipsis => "dot dot dot",
- :slash => "slash"
- }
-
- DEFAULT_HTML_ENTITY_CONVERSIONS =
- {
- :double_quote => "\"",
- :single_quote => "'",
- :ellipsis => "...",
- :en_dash => "-",
- :em_dash => "--",
- :times => "x",
- :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 "
- }
-
- DEFAULT_VULGAR_FRACTION_CONVERSIONS =
- {
- :one_fourth => "one fourth",
- :half => "half",
- :three_fourths => "three fourths",
- :one_third => "one third",
- :two_thirds => "two thirds",
- :one_fifth => "one fifth",
- :two_fifths => "two fifths",
- :three_fifths => "three fifths",
- :four_fifths => "four fifths",
- :one_sixth => "one sixth",
- :five_sixths => "five sixths",
- :one_eighth => "one eighth",
- :three_eighths => "three eighths",
- :five_eighths => "five eighths",
- :seven_eighths => "seven eighths"
- }
-
# These methods are all included into the String class.
module PublicInstanceMethods
# Removes specified character from the beginning and/or end of the string and then performs
@@ -188,7 +122,7 @@ def convert_miscellaneous_html_entities
"(#247|divide)" => :divide,
"(#176|deg)" => :deg
}.each do |textiled, key|
- normal = stringex_translate_html_entitity(key)
+ normal = stringex_translate_html_entity(key)
dummy.gsub!(/&#{textiled};/, normal)
end
dummy.gsub(/&[^;]+;/, "").strip
@@ -322,15 +256,15 @@ def stringex_default_options
end
def stringex_translate_character(key)
- Localization.translate(:characters, key, :default => DEFAULT_CHARACTER_CONVERSIONS[key])
+ Localization.translate(:characters, key)
end
- def stringex_translate_html_entitity(key)
- Localization.translate(:html_entities, key, :default => DEFAULT_HTML_ENTITY_CONVERSIONS[key])
+ def stringex_translate_html_entity(key)
+ Localization.translate(:html_entities, key)
end
def stringex_translate_vulgar_fraction(key)
- Localization.translate(:vulgar_fractions, key, :default => DEFAULT_VULGAR_FRACTION_CONVERSIONS[key])
+ Localization.translate(:vulgar_fractions, key)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.