Sha256: 941f4c5bcba8610caca901ea5d079206af48b6c0c95709b46e8cd55a0eacc0b9

Contents?: true

Size: 1.19 KB

Versions: 54

Compression:

Stored size: 1.19 KB

Contents

# encoding: UTF-8

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

module TwitterCldr
  module Formatters
    module Rbnf

      class Plural
        def self.from_string(locale, str)
          # $(cardinal,one{ tysiąc}few{ tysiące}other{ tysięcy})$
          plural_type, cases = str.gsub(/\$\((.*)\)\$/, '\1').split(',')

          # one{ tysiąc}few{ tysiące}other{ tysięcy}
          cases = cases.scan(/([\w]+)\{([^}]+)\}/).inject({}) do |ret, case_arr|
            ret[case_arr.first.to_sym] = case_arr.last
            ret
          end

          new(locale, plural_type.to_sym, cases)
        end

        attr_reader :locale, :plural_type, :cases

        # plural_type = cardinal, etc
        # cases = hash of form one: "foo", two: "bar"
        def initialize(locale, plural_type, cases)
          @locale = locale
          @plural_type = plural_type
          @cases = cases
        end

        def render(number)
          rule_name = TwitterCldr::Formatters::Plurals::Rules.rule_for(
            number, locale, plural_type
          )

          cases[rule_name] || cases[:other]
        end

        def type
          :plural
        end
      end

    end
  end
end

Version data entries

54 entries across 54 versions & 3 rubygems

Version Path
twitter_cldr-6.13.0 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.12.1 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.12.0 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.11.5 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.11.4 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.11.3 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.11.2 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.11.1 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.11.0 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.10.0 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.9.0 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.8.0 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.7.0 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.6.2 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.6.1 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.6.0 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.5.0 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.4.0 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.3.0 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb
twitter_cldr-6.2.0 lib/twitter_cldr/formatters/numbers/rbnf/plural.rb