Skip to content

Commit

Permalink
Added base list formatting class
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron Dutro committed Sep 20, 2012
1 parent 4eb58f6 commit 710288e
Show file tree
Hide file tree
Showing 38 changed files with 300 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -8,7 +8,7 @@ end

group :development do
gem 'nokogiri'
gem 'ruby-cldr', :github => 'KL-7/ruby-cldr', :branch => 'for-twitter-cldr'
gem 'ruby-cldr', :github => 'camertron/ruby-cldr', :branch => 'combined'
end

group :test do
Expand Down
6 changes: 4 additions & 2 deletions lib/twitter_cldr/formatters.rb
Expand Up @@ -18,7 +18,9 @@ module Formatters
autoload :CurrencyFormatter, 'twitter_cldr/formatters/numbers/currency_formatter'
autoload :PercentFormatter, 'twitter_cldr/formatters/numbers/percent_formatter'

autoload :Plurals, 'twitter_cldr/formatters/plurals'
autoload :PluralFormatter, 'twitter_cldr/formatters/plurals/plural_formatter'
autoload :Plurals, 'twitter_cldr/formatters/plurals'
autoload :PluralFormatter, 'twitter_cldr/formatters/plurals/plural_formatter'

autoload :ListFormatter, 'twitter_cldr/formatters/list_formatter'
end
end
56 changes: 56 additions & 0 deletions lib/twitter_cldr/formatters/list_formatter.rb
@@ -0,0 +1,56 @@
# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

module TwitterCldr
module Formatters
class ListFormatter < Base

def initialize(options = {})
@locale = TwitterCldr.convert_locale(extract_locale(options))
end

def format(list)
if resource.include?(list.size)
compose(resource[list.size], list)
else
compose_list(list)
end
end

protected

def compose_list(list)
result = compose(resource[:end], [list[-2], list[-1]])

# Ruby ranges don't support subtraction for some reason (eg. -3..-5).
# Instead, we use a positive counter and negate it on array access.
(3..list.size).each do |i|
format_sym = i == list.size ? :start : :middle
format_sym = :middle unless resource.include?(format_sym)
result = compose(resource[format_sym], [list[-i], result])
end

result
end

def compose(format, elements)
elements.compact!

if elements.size > 1
result = format.dup
elements.size.times { |i| result.gsub!("{#{i}}", elements[i]) }
result
else
elements[0]
end
end

def resource
@resource ||= TwitterCldr.get_locale_resource(@locale, :lists)[@locale][:lists]
end

end
end
end
2 changes: 1 addition & 1 deletion lib/twitter_cldr/resources/locales_resources_importer.rb
Expand Up @@ -12,7 +12,7 @@ module Resources

class LocalesResourcesImporter

COMPONENTS = %w[calendars languages numbers units plurals]
COMPONENTS = %w[calendars languages numbers units plurals lists]

# Arguments:
#
Expand Down
7 changes: 7 additions & 0 deletions resources/locales/af/lists.yml
@@ -0,0 +1,7 @@
---
:af:
:lists:
2: ! '{0} en {1}'
:end: ! '{0} en {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/ar/lists.yml
@@ -0,0 +1,7 @@
---
:ar:
:lists:
2: ! '{0} و {1}'
:end: ! '{0}، و {1}'
:middle: ! '{0}، {1}'
:start: ! '{0}، {1}'
7 changes: 7 additions & 0 deletions resources/locales/ca/lists.yml
@@ -0,0 +1,7 @@
---
:ca:
:lists:
2: ! '{0} i {1}'
:end: ! '{0} i {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/cs/lists.yml
@@ -0,0 +1,7 @@
---
:cs:
:lists:
2: ! '{0} a {1}'
:end: ! '{0} a {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/da/lists.yml
@@ -0,0 +1,7 @@
---
:da:
:lists:
2: ! '{0} og {1}'
:end: ! '{0} og {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/de/lists.yml
@@ -0,0 +1,7 @@
---
:de:
:lists:
2: ! '{0} und {1}'
:end: ! '{0} und {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/el/lists.yml
@@ -0,0 +1,7 @@
---
:el:
:lists:
2: ! '{0} και {1}'
:end: ! '{0} και {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/en/lists.yml
@@ -0,0 +1,7 @@
---
:en:
:lists:
2: ! '{0} and {1}'
:end: ! '{0}, and {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/es/lists.yml
@@ -0,0 +1,7 @@
---
:es:
:lists:
2: ! '{0} y {1}'
:end: ! '{0} y {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/eu/lists.yml
@@ -0,0 +1,7 @@
---
:eu:
:lists:
2: ! '{0} eta {1}'
:end: ! '{0} eta {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/fa/lists.yml
@@ -0,0 +1,7 @@
---
:fa:
:lists:
2: ! '{0} و {1}'
:end: ! '{0}، و {1}'
:middle: ! '{0}،‏ {1}'
:start: ! '{0}،‏ {1}'
7 changes: 7 additions & 0 deletions resources/locales/fi/lists.yml
@@ -0,0 +1,7 @@
---
:fi:
:lists:
2: ! '{0} ja {1}'
:end: ! '{0} ja {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/fil/lists.yml
@@ -0,0 +1,7 @@
---
:fil:
:lists:
2: ! '{0} at {1}'
:end: ! '{0} at {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/fr/lists.yml
@@ -0,0 +1,7 @@
---
:fr:
:lists:
2: ! '{0} et {1}'
:end: ! '{0} et {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/he/lists.yml
@@ -0,0 +1,7 @@
---
:he:
:lists:
2: ! '{0} ו-{1}'
:end: ! '{0} ו-{1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/hi/lists.yml
@@ -0,0 +1,7 @@
---
:hi:
:lists:
2: ! '{0} और {1}'
:end: ! '{0}, और {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/hu/lists.yml
@@ -0,0 +1,7 @@
---
:hu:
:lists:
2: ! '{0} és {1}'
:end: ! '{0} és {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/id/lists.yml
@@ -0,0 +1,7 @@
---
:id:
:lists:
2: ! '{0} dan {1}'
:end: ! '{0}, dan {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/it/lists.yml
@@ -0,0 +1,7 @@
---
:it:
:lists:
2: ! '{0} e {1}'
:end: ! '{0}, e {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/ja/lists.yml
@@ -0,0 +1,7 @@
---
:ja:
:lists:
2: ! '{0}、{1}'
:end: ! '{0}、{1}'
:middle: ! '{0}、{1}'
:start: ! '{0}、{1}'
7 changes: 7 additions & 0 deletions resources/locales/ko/lists.yml
@@ -0,0 +1,7 @@
---
:ko:
:lists:
2: ! '{0} 및 {1}'
:end: ! '{0} 및 {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/ms/lists.yml
@@ -0,0 +1,7 @@
---
:ms:
:lists:
2: ! '{0} dan {1}'
:end: ! '{0}, dan {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/nb/lists.yml
@@ -0,0 +1,7 @@
---
:nb:
:lists:
2: ! '{0} og {1}'
:end: ! '{0} og {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/nl/lists.yml
@@ -0,0 +1,7 @@
---
:nl:
:lists:
2: ! '{0} en {1}'
:end: ! '{0} en {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/pl/lists.yml
@@ -0,0 +1,7 @@
---
:pl:
:lists:
2: ! '{0} i {1}'
:end: ! '{0} i {1}'
:middle: ! '{0}; {1}'
:start: ! '{0}; {1}'
7 changes: 7 additions & 0 deletions resources/locales/pt/lists.yml
@@ -0,0 +1,7 @@
---
:pt:
:lists:
2: ! '{0} e {1}'
:end: ! '{0} e {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/ru/lists.yml
@@ -0,0 +1,7 @@
---
:ru:
:lists:
2: ! '{0} и {1}'
:end: ! '{0} и {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/sv/lists.yml
@@ -0,0 +1,7 @@
---
:sv:
:lists:
2: ! '{0} och {1}'
:end: ! '{0} och {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/th/lists.yml
@@ -0,0 +1,7 @@
---
:th:
:lists:
2: ! '{0}และ{1}'
:end: ! '{0} และ{1}'
:middle: ! '{0} {1}'
:start: ! '{0} {1}'
7 changes: 7 additions & 0 deletions resources/locales/tr/lists.yml
@@ -0,0 +1,7 @@
---
:tr:
:lists:
2: ! '{0} ve {1}'
:end: ! '{0} ve {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/uk/lists.yml
@@ -0,0 +1,7 @@
---
:uk:
:lists:
2: ! '{0} та {1}'
:end: ! '{0} та {1}'
:middle: ! '{0}, {1}'
:start: ! '{0}, {1}'
7 changes: 7 additions & 0 deletions resources/locales/ur/lists.yml
@@ -0,0 +1,7 @@
---
:ur:
:lists:
2: ! '{0} اور {1}'
:end: ! '{0}، اور {1}'
:middle: ! '{0}، {1}'
:start: ! '{0}، {1}'
7 changes: 7 additions & 0 deletions resources/locales/zh-Hant/lists.yml
@@ -0,0 +1,7 @@
---
:zh-Hant:
:lists:
2: ! '{0}和{1}'
:end: ! '{0}和{1}'
:middle: ! '{0}、{1}'
:start: ! '{0}、{1}'
7 changes: 7 additions & 0 deletions resources/locales/zh/lists.yml
@@ -0,0 +1,7 @@
---
:zh:
:lists:
2: ! '{0}和{1}'
:end: ! '{0}和{1}'
:middle: ! '{0}、{1}'
:start: ! '{0}、{1}'

0 comments on commit 710288e

Please sign in to comment.