Permalink
Browse files

added better hooks for i18n yaml and tests for the en stuff

  • Loading branch information...
1 parent bb441c1 commit c4234e1ef43045d1004bfd99e270746ce149bfb3 @rsl committed Apr 4, 2013
Showing with 171 additions and 7 deletions.
  1. +1 −1 lib/stringex.rb
  2. +7 −0 lib/stringex/localization.rb
  3. +6 −6 locales/en.yml
  4. +157 −0 test/localization/en_test.rb
View
2 lib/stringex.rb
@@ -14,4 +14,4 @@
if defined?(Rails)
require 'stringex/rails/railtie'
-end
+end
View
7 lib/stringex/localization.rb
@@ -7,6 +7,8 @@ module Stringex
module Localization
include DefaultConversions
+ I18N_LOAD_PATH_BASE = File.join(File.expand_path(File.dirname(__FILE__)), '..', '..', 'locales')
+
class << self
attr_writer :translations, :backend
@@ -94,6 +96,11 @@ def convert(string, options = {}, &block)
converter.string
end
+ def load_i18n_yaml(locale = nil)
+ locale ||= I18n.locale
+ I18n.load_path << Dir[File.join(I18N_LOAD_PATH_BASE, "#{locale}.yml")]
+ end
+
private
def initial_translation(scope, key, locale)
View
12 locales/en.yml
@@ -8,7 +8,7 @@ en:
characters:
and: and
at: at
- divide: divide
+ divide: divided by
degrees: degrees
dot: \1 dot \2
ellipsis: dot dot dot
@@ -28,10 +28,10 @@ en:
yen: \1 yen
html_entities:
amp: and
- cent: cents
+ cent: " cents"
copy: (c)
- deg: degrees
- divide: divide
+ deg: " degrees "
+ divide: " divided by "
double_quote: '"'
ellipsis: "..."
en_dash: "-"
@@ -42,12 +42,12 @@ en:
gt: >
lt: <
nbsp: " "
- pound: pounds
+ pound: " pounds "
reg: (r)
single_quote: "'"
times: x
trade: (tm)
- yen: yen
+ yen: " yen "
vulgar_fractions:
half: half
one_third: one third
View
157 test/localization/en_test.rb
@@ -0,0 +1,157 @@
+# encoding: UTF-8
+
+require 'test_helper'
+require 'i18n'
+require 'stringex'
+
+class EnglishYAMLLocalizationTest < Test::Unit::TestCase
+ def setup
+ Stringex::Localization.reset!
+ Stringex::Localization.backend = :i18n
+ I18n.locale = :en
+ Stringex::Localization.load_i18n_yaml :en
+ end
+
+ {
+ "foo & bar" => "foo and bar",
+ "AT&T" => "AT and T",
+ "99° is normal" => "99 degrees is normal",
+ "4 ÷ 2 is 2" => "4 divided by 2 is 2",
+ "webcrawler.com" => "webcrawler dot com",
+ "Well..." => "Well dot dot dot",
+ "x=1" => "x equals 1",
+ "a #2 pencil" => "a number 2 pencil",
+ "100%" => "100 percent",
+ "cost+tax" => "cost plus tax",
+ "batman/robin fan fiction" => "batman slash robin fan fiction",
+ "dial *69" => "dial star 69",
+ " i leave whitespace on ends unchanged " => " i leave whitespace on ends unchanged "
+ }.each do |original, converted|
+ define_method "test_character_conversion: '#{original}'" do
+ assert_equal converted, original.convert_miscellaneous_characters
+ end
+ end
+
+ {
+ "$100" => "100 dollars",
+ "$19.99" => "19 dollars 99 cents",
+ "£100" => "100 pounds",
+ "£19.99" => "19 pounds 99 pence",
+ "€100" => "100 euros",
+ "€19.99" => "19 euros 99 cents",
+ "¥1000" => "1000 yen"
+ }.each do |original, converted|
+ define_method "test_currency_conversion: '#{original}'" do
+ assert_equal converted, original.convert_miscellaneous_characters
+ end
+ end
+
+ {
+ "Tea &amp; Sympathy" => "Tea and Sympathy",
+ "10&cent;" => "10 cents",
+ "&copy;2000" => "(c)2000",
+ "98&deg; is fine" => "98 degrees is fine",
+ "10&divide;5" => "10 divided by 5",
+ "&quot;quoted&quot;" => '"quoted"',
+ "to be continued&hellip;" => "to be continued...",
+ "2000&ndash;2004" => "2000-2004",
+ "I wish&mdash;oh, never mind" => "I wish--oh, never mind",
+ "&frac12; ounce of gold" => "half ounce of gold",
+ "1 and &frac14; ounces of silver" => "1 and one fourth ounces of silver",
+ "9 and &frac34; ounces of platinum" => "9 and three fourths ounces of platinum",
+ "3&gt;2" => "3>2",
+ "2&lt;3" => "2<3",
+ "two&nbsp;words" => "two words",
+ "&pound;100" => "pounds 100",
+ "Walmart&reg;" => "Walmart(r)",
+ "&apos;single quoted&apos;" => "'single quoted'",
+ "2&times;4" => "2x4",
+ "Programming&trade;" => "Programming(tm)",
+ "&yen;20000" => "yen 20000",
+ " i leave whitespace on ends unchanged " => " i leave whitespace on ends unchanged "
+ }.each do |original, converted|
+ define_method "test_html_entity_conversion: '#{original}'" do
+ assert_equal converted, original.convert_miscellaneous_html_entities
+ end
+ end
+
+ {
+ "&frac12;" => "half",
+ "½" => "half",
+ "&#189;" => "half",
+ "" => "one third",
+ "&#8531;" => "one third",
+ "" => "two thirds",
+ "&#8532;" => "two thirds",
+ "&frac14;" => "one fourth",
+ "¼" => "one fourth",
+ "&#188;" => "one fourth",
+ "&frac34;" => "three fourths",
+ "¾" => "three fourths",
+ "&#190;" => "three fourths",
+ "" => "one fifth",
+ "&#8533;" => "one fifth",
+ "" => "two fifths",
+ "&#8534;" => "two fifths",
+ "" => "three fifths",
+ "&#8535;" => "three fifths",
+ "" => "four fifths",
+ "&#8536;" => "four fifths",
+ "" => "one sixth",
+ "&#8537;" => "one sixth",
+ "" => "five sixths",
+ "&#8538;" => "five sixths",
+ "" => "one eighth",
+ "&#8539;" => "one eighth",
+ "" => "three eighths",
+ "&#8540;" => "three eighths",
+ "" => "five eighths",
+ "&#8541;" => "five eighths",
+ "" => "seven eighths",
+ "&#8542;" => "seven eighths"
+ }.each do |original, converted|
+ define_method "test_vulgar_fractions_conversion: #{original}" do
+ assert_equal converted, original.convert_vulgar_fractions
+ end
+ end
+
+ def test_convert_vulgar_fractions
+ {
+ "&frac14;" => "one fourth",
+ "¼" => "one fourth",
+ "&#188;" => "one fourth",
+ "&frac12;" => "half",
+ "½" => "half",
+ "&#189;" => "half",
+ "&frac34;" => "three fourths",
+ "¾" => "three fourths",
+ "&#190;" => "three fourths",
+ "" => "one third",
+ "&#8531;" => "one third",
+ "" => "two thirds",
+ "&#8532;" => "two thirds",
+ "" => "one fifth",
+ "&#8533;" => "one fifth",
+ "" => "two fifths",
+ "&#8534;" => "two fifths",
+ "" => "three fifths",
+ "&#8535;" => "three fifths",
+ "" => "four fifths",
+ "&#8536;" => "four fifths",
+ "" => "one sixth",
+ "&#8537;" => "one sixth",
+ "" => "five sixths",
+ "&#8538;" => "five sixths",
+ "" => "one eighth",
+ "&#8539;" => "one eighth",
+ "" => "three eighths",
+ "&#8540;" => "three eighths",
+ "" => "five eighths",
+ "&#8541;" => "five eighths",
+ "" => "seven eighths",
+ "&#8542;" => "seven eighths"
+ }.each do |entitied, plain|
+ assert_equal plain, entitied.convert_vulgar_fractions
+ end
+ end
+end

0 comments on commit c4234e1

Please sign in to comment.