Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: rsl/stringex
base: 09a195fff8
...
head fork: rsl/stringex
compare: ca6794d8a4
  • 5 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
4 lib/stringex/configuration/acts_as_url.rb
View
@@ -24,10 +24,6 @@ def self.settings
private
- # def default_settings
- # Stringex::Configuration::StringExtensions.new.default_settings.merge(default_settings)
- # end
-
def default_settings
self.class.default_settings
end
15 lib/stringex/localization.rb
View
@@ -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
81 lib/stringex/localization/default_conversions.rb
View
@@ -0,0 +1,81 @@
+module Stringex
+ module Localization
+ module DefaultConversions
+ CHARACTERS = {
+ :and => "and",
+ :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',
+ :euros => '\1 euros',
+ :euros_cents => '\1 euros \2 cents',
+ :pounds => '\1 pounds',
+ :pounds_pence => '\1 pounds \2 pence',
+ :yen => '\1 yen',
+ }
+
+ HTML_ENTITIES = {
+ :amp => "and",
+ :cent => " cents",
+ :copy => "(c)",
+ :deg => " degrees ",
+ :divide => "divide",
+ :double_quote => "\"",
+ :ellipsis => "...",
+ :en_dash => "-",
+ :em_dash => "--",
+ :frac14 => "one fourth",
+ :frac12 => "half",
+ :frac34 => "three fourths",
+ :gt => ">",
+ :lt => "<",
+ :nbsp => " ",
+ :pound => " pound",
+ :reg => "(r)",
+ :single_quote => "'",
+ :times => "x",
+ :trade => "(tm)",
+ }
+
+ 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 currencies 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
101 lib/stringex/string_extensions.rb
View
@@ -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
@@ -119,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,
@@ -188,7 +131,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 +265,19 @@ 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_currency(key)
+ Localization.translate(:currencies, 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
3  lib/stringex/unidecoder.rb
View
@@ -46,7 +46,7 @@ def in_yaml_file(character)
end
private
-
+
def translate(codepoint)
Localization.translate(:transliterations, codepoint)
end
@@ -60,7 +60,6 @@ def code_group(unpacked_character)
def grouped_point(unpacked_character)
unpacked_character & 255
end
-
end
end
end
1  test/i18n/default_localization_test.rb
View
@@ -1,6 +1,7 @@
# encoding: UTF-8
require 'test_helper'
+require 'stringex'
class DefaultLocalizationTest < Test::Unit::TestCase
def setup

No commit comments for this range

Something went wrong with that request. Please try again.