Skip to content

Commit

Permalink
move default conversions to a place a little less in your face for my…
Browse files Browse the repository at this point in the history
… tastes and some related refactors
  • Loading branch information
rsl committed Mar 18, 2013
1 parent a99ec71 commit 989fc96
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 73 deletions.
15 changes: 13 additions & 2 deletions 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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
78 changes: 78 additions & 0 deletions 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
76 changes: 5 additions & 71 deletions lib/stringex/string_extensions.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 989fc96

Please sign in to comment.