From 91f0f27886face3ea78aaae2222198e027bbc329 Mon Sep 17 00:00:00 2001 From: Franta <103316363+Franta1205@users.noreply.github.com> Date: Fri, 17 May 2024 13:46:58 +0200 Subject: [PATCH] feat: add support for danish language (#197) --- .gitignore | 1 + CHANGELOG.md | 3 +- README.rdoc | 20 +++++++ .../i18n/locales/numbers.da.yml | 52 +++++++++++++++++ .../strategies/figures_converter/languages.rb | 1 + .../figures_converter/languages/da.rb | 56 +++++++++++++++++++ .../strategies/figures_converter/options.rb | 1 + .../figures_converter/options/da.rb | 3 + .../figures_converter/options/da/gender.rb | 14 +++++ lib/numbers_and_words/translations.rb | 1 + lib/numbers_and_words/translations/da.rb | 37 ++++++++++++ lib/numbers_and_words/version.rb | 2 +- numbers_and_words.gemspec | 9 ++- .../array/fixture_examples/da.yml | 47 ++++++++++++++++ .../integer/fixture_examples/da.yml | 5 ++ 15 files changed, 248 insertions(+), 4 deletions(-) create mode 100644 lib/numbers_and_words/i18n/locales/numbers.da.yml create mode 100644 lib/numbers_and_words/strategies/figures_converter/languages/da.rb create mode 100644 lib/numbers_and_words/strategies/figures_converter/options/da.rb create mode 100644 lib/numbers_and_words/strategies/figures_converter/options/da/gender.rb create mode 100644 lib/numbers_and_words/translations/da.rb create mode 100644 spec/numbers_and_words/array/fixture_examples/da.yml create mode 100644 spec/numbers_and_words/integer/fixture_examples/da.yml diff --git a/.gitignore b/.gitignore index bf4e104e..2c04d75f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ coverage pkg Gemfile.lock .bundle +.idea diff --git a/CHANGELOG.md b/CHANGELOG.md index e6a50d1c..d0f4aa8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ -## 0.11.13 (Next) +## 0.12.0 (Next) ### Features + * Add support for Danish language. \[[#197](https://github.com/kslazarev/numbers_and_words/pull/197)\] \([@franta](https://github.com/Franta1205)\) * Upgrade RuboCop to 1.63.5. \[[#198](https://github.com/kslazarev/numbers_and_words/pull/198)\] * Your contribution here. diff --git a/README.rdoc b/README.rdoc index e18e75f0..2a5247dd 100644 --- a/README.rdoc +++ b/README.rdoc @@ -42,6 +42,7 @@ Deletrea números en varios idiomas utilizando la gema I18n. * Português Brasileiro [pt-BR] * հայերեն (Armenian) [hy] * Қазақша [kz] +* Dansk [da] ** Experimental @@ -93,6 +94,9 @@ Deletrea números en varios idiomas utilizando la gema I18n. I18n.with_locale(:kz) { 42.to_words } => "қырық екi" + I18n.with_locale(:da) { 42.to_words } + => "toogfyrre" + 21.to_words => "twenty-one" => "veintiuno" @@ -114,6 +118,7 @@ Deletrea números en varios idiomas utilizando la gema I18n. => "hai mươi mốt" => "քսան մեկ" => "жиырма бiр" + => "enogtyve" 231.to_words => "two hundred thirty-one" @@ -136,6 +141,7 @@ Deletrea números en varios idiomas utilizando la gema I18n. => "hai trăm ba mươi mốt" => "երկու հարյուր երեսուն մեկ" => "екi жүз отыз бiр" + => "to hundrede enogtredive" 4030.to_words => "four thousand thirty" @@ -157,6 +163,7 @@ Deletrea números en varios idiomas utilizando la gema I18n. => "bốn nghìn không trăm ba mươi" => "չորս հազար երեսուն" => "төрт мың отыз" + => "fire tusinde tredive" 1000100.to_words => "one million one hundred" @@ -178,6 +185,7 @@ Deletrea números en varios idiomas utilizando la gema I18n. => "một triệu một trăm" => "մեկ միլիոն հարյուր" => "бiр миллион бiр жүз" + => "en million et hundrede" 1000000000000000000000000000000000.to_words => "one decillion" @@ -211,6 +219,7 @@ Deletrea números en varios idiomas utilizando la gema I18n. => ["một", "hai", "ba"] => ["մեկ", "երկու", "երեք"] => ["бiр", "екi", "үш"] + => ["et", "to", "tre"] [11, 22, 133].to_words => ["eleven", "twenty-two", "one hundred thirty-three"] @@ -232,6 +241,7 @@ Deletrea números en varios idiomas utilizando la gema I18n. => ["mười một", "hai mươi hai", "một trăm ba mươi ba"] => ["տասնմեկ", "քսան երկու", "հարյուր երեսուն երեք"] => ["он бiр", "жиырма екi", "бiр жүз отыз үш"] + => ["elleve", "toogtyve", "et hundrede treogtredive"] 21.77.to_words => "twenty-one and seventy-seven hundredths" @@ -353,6 +363,16 @@ Deletrea números en varios idiomas utilizando la gema I18n. I18n.with_locale(:es) { 0.7.to_words remove_zero: true } => "siete décimas" +* Danish + + Change gender from: (gender: [:neuter || :common]) + + I18n.with_locale(:da) { 1.to_words gender: :common } + => "en" + + I18n.with_locale(:da) { 1.to_words gender: :neuter } + => "et" + == Other options / Другие опции * Precision diff --git a/lib/numbers_and_words/i18n/locales/numbers.da.yml b/lib/numbers_and_words/i18n/locales/numbers.da.yml new file mode 100644 index 00000000..6f284091 --- /dev/null +++ b/lib/numbers_and_words/i18n/locales/numbers.da.yml @@ -0,0 +1,52 @@ +da: + numbers: + ones: [nul, et, to, tre, fire, fem, seks, syv, otte, ni] + common: [nul, en, to, tre, fire, fem, seks, syv, otte, ni] + neuter: [nul, et, to, tre, fire, fem, seks, syv, otte, ni] + teens: [_, elleve, tolv, tretten, fjorten, femten, seksten, sytten, atten, nitten] + tens: [_, ti, tyve, tredive, fyrre, halvtreds, tres, halvfjerds, firs, halvfems] + hundreds: [ _, et hundrede, to hundrede, tre hundrede, fire hundrede, fem hundrede, seks hundrede, syv hundrede, otte hundrede, ni hundrede ] + one_hundred: et hundrede + mega: [_, tusinde, million, milliard, billion, billiard, trillion] + tusinde: + one: tusinde + other: tusinde + million: + one: million + other: millioner + milliard: + one: milliard + other: milliarder + billion: + one: billion + other: billioner + billiard: + one: billiard + other: billiarder + trillion: + one: trillion + other: trillioner + ordinal: + ones: [nulte, første, anden, tredje, fjerde, femte, sjette, syvende, ottende, niende] + teens: [tiende, ellevte, tolvte, trettende, fjortende, femtende, sekstende, syttende, attende, nittende] + tens: [_, tiende, tyvende, tredive, fyrre, halvtreds, tres, halvfjerds, firs, halvfems] + hundreds: hundredende + micro: [_, tiendedele, hundrededele, tusindedele, milliontedele] + tenths: + one: tiendedel + other: tiendedele + hundredths: + one: hundrededel + other: hundrededele + thousandths: + one: tusindedel + other: tusindedele + millionths: + one: milliontedel + other: milliontedele + micro_separator: og + micro_prefix: + tens: ti + hundreds: hundrede + union: '-' + union: og diff --git a/lib/numbers_and_words/strategies/figures_converter/languages.rb b/lib/numbers_and_words/strategies/figures_converter/languages.rb index cbcfee8d..be98ea75 100644 --- a/lib/numbers_and_words/strategies/figures_converter/languages.rb +++ b/lib/numbers_and_words/strategies/figures_converter/languages.rb @@ -23,6 +23,7 @@ require 'numbers_and_words/strategies/figures_converter/languages/tr' require 'numbers_and_words/strategies/figures_converter/languages/hy' require 'numbers_and_words/strategies/figures_converter/languages/kz' +require 'numbers_and_words/strategies/figures_converter/languages/da' require 'numbers_and_words/strategies/figures_converter/languages/families/cyrillic' require 'numbers_and_words/strategies/figures_converter/languages/ru' diff --git a/lib/numbers_and_words/strategies/figures_converter/languages/da.rb b/lib/numbers_and_words/strategies/figures_converter/languages/da.rb new file mode 100644 index 00000000..c0304832 --- /dev/null +++ b/lib/numbers_and_words/strategies/figures_converter/languages/da.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +module NumbersAndWords + module Strategies + module FiguresConverter + module Languages + class Da < Base + def print_words + complex_part.count > 1 ? print_megs_words : @strings.flatten.reverse.join(' ') + end + + def ones + super({ gender: gender }) + end + + def hundreds + super({ is_hundred: hundred?, is_one_hundred: one_hundred? }) + end + + def megs + super({ number: @figures.number_in_capacity(@current_capacity) }) + end + + private + + def simple_part + @strings.reject { |f| f.is_a?(Array) } + end + + def complex_part + @strings - simple_part + end + + def hundred? + figures[0].zero? && figures[1].zero? && simple_number_to_words.empty? + end + + def one_hundred? + hundred? && figures[2] == 1 + end + + def gender + @current_capacity ||= 0 + if @current_capacity.zero? + @options.gender.result + elsif @current_capacity == 1 + :neuter + else + :common + end + end + end + end + end + end +end diff --git a/lib/numbers_and_words/strategies/figures_converter/options.rb b/lib/numbers_and_words/strategies/figures_converter/options.rb index 5d54d934..61f15b4d 100644 --- a/lib/numbers_and_words/strategies/figures_converter/options.rb +++ b/lib/numbers_and_words/strategies/figures_converter/options.rb @@ -10,6 +10,7 @@ require 'numbers_and_words/strategies/figures_converter/options/ru' require 'numbers_and_words/strategies/figures_converter/options/ua' require 'numbers_and_words/strategies/figures_converter/options/cs' +require 'numbers_and_words/strategies/figures_converter/options/da' module NumbersAndWords module Strategies diff --git a/lib/numbers_and_words/strategies/figures_converter/options/da.rb b/lib/numbers_and_words/strategies/figures_converter/options/da.rb new file mode 100644 index 00000000..b8310b06 --- /dev/null +++ b/lib/numbers_and_words/strategies/figures_converter/options/da.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +require 'numbers_and_words/strategies/figures_converter/options/da/gender' diff --git a/lib/numbers_and_words/strategies/figures_converter/options/da/gender.rb b/lib/numbers_and_words/strategies/figures_converter/options/da/gender.rb new file mode 100644 index 00000000..5ac154d7 --- /dev/null +++ b/lib/numbers_and_words/strategies/figures_converter/options/da/gender.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module NumbersAndWords + module Strategies + module FiguresConverter + module Options + module Da + class Gender < Base::Gender + end + end + end + end + end +end diff --git a/lib/numbers_and_words/translations.rb b/lib/numbers_and_words/translations.rb index c4c949fa..a567a5d7 100644 --- a/lib/numbers_and_words/translations.rb +++ b/lib/numbers_and_words/translations.rb @@ -27,6 +27,7 @@ require 'numbers_and_words/translations/vi' require 'numbers_and_words/translations/hy' require 'numbers_and_words/translations/kz' +require 'numbers_and_words/translations/da' module NumbersAndWords module Translations diff --git a/lib/numbers_and_words/translations/da.rb b/lib/numbers_and_words/translations/da.rb new file mode 100644 index 00000000..b2f5cf6e --- /dev/null +++ b/lib/numbers_and_words/translations/da.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module NumbersAndWords + module Translations + class Da < Base + include NumbersAndWords::Translations::Families::Base + + DEFAULT_POSTFIX = nil + + def ones(number, options = {}) + return t(:common)[number] if options[:gender] == :common + return t(:neuter)[number] if options[:gender] == :neuter + + t([options[:prefix], :ones, options[:postfix] || DEFAULT_POSTFIX].join('.'))[number] + end + + def tens(number, _options = {}) + t(:tens)[number] + end + + def tens_with_ones(numbers, _options = {}) + [ones(numbers[0], { gender: :common }), tens(numbers[1])].join(union) + end + + def hundreds(number, options = {}) + return t(:one_hundred) if options[:is_one_hundred] + return t(:hundreds)[number] if options[:is_hundred] + + t(:hundreds)[number] + end + + def zero(_options = {}) + ones 0 + end + end + end +end diff --git a/lib/numbers_and_words/version.rb b/lib/numbers_and_words/version.rb index 8de1609c..7a30f1ba 100644 --- a/lib/numbers_and_words/version.rb +++ b/lib/numbers_and_words/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module NumbersAndWords - VERSION = '0.11.13' + VERSION = '0.12.0' end diff --git a/numbers_and_words.gemspec b/numbers_and_words.gemspec index 10553db1..184da180 100644 --- a/numbers_and_words.gemspec +++ b/numbers_and_words.gemspec @@ -2,7 +2,7 @@ # DO NOT EDIT THIS FILE DIRECTLY # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' # -*- encoding: utf-8 -*- -# stub: numbers_and_words 0.11.12 ruby lib +# stub: numbers_and_words 0.12.0 ruby lib Gem::Specification.new do |s| s.name = "numbers_and_words".freeze @@ -11,7 +11,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["Kirill Lazarev".freeze] - s.date = "2023-04-22" + s.date = "2023-11-15" s.description = "This gem spells out numbers in several languages using the I18n gem.".freeze s.email = "k.s.lazarev@gmail.com".freeze s.extra_rdoc_files = [ @@ -32,6 +32,7 @@ Gem::Specification.new do |s| "lib/numbers_and_words/i18n.rb", "lib/numbers_and_words/i18n/initialization.rb", "lib/numbers_and_words/i18n/locales/numbers.cs.yml", + "lib/numbers_and_words/i18n/locales/numbers.da.yml", "lib/numbers_and_words/i18n/locales/numbers.de.yml", "lib/numbers_and_words/i18n/locales/numbers.en-GB.yml", "lib/numbers_and_words/i18n/locales/numbers.en.yml", @@ -121,6 +122,7 @@ Gem::Specification.new do |s| "lib/numbers_and_words/strategies/figures_converter/languages.rb", "lib/numbers_and_words/strategies/figures_converter/languages/base.rb", "lib/numbers_and_words/strategies/figures_converter/languages/cs.rb", + "lib/numbers_and_words/strategies/figures_converter/languages/da.rb", "lib/numbers_and_words/strategies/figures_converter/languages/de.rb", "lib/numbers_and_words/strategies/figures_converter/languages/en-GB.rb", "lib/numbers_and_words/strategies/figures_converter/languages/en.rb", @@ -182,9 +184,12 @@ Gem::Specification.new do |s| "lib/numbers_and_words/strategies/figures_converter/options/ru/gender.rb", "lib/numbers_and_words/strategies/figures_converter/options/ua.rb", "lib/numbers_and_words/strategies/figures_converter/options/ua/gender.rb", + "lib/numbers_and_words/strategies/figures_converter/options/da/gender.rb", + "lib/numbers_and_words/strategies/figures_converter/options/da.rb", "lib/numbers_and_words/translations.rb", "lib/numbers_and_words/translations/base.rb", "lib/numbers_and_words/translations/cs.rb", + "lib/numbers_and_words/translations/da.rb", "lib/numbers_and_words/translations/de.rb", "lib/numbers_and_words/translations/en-GB.rb", "lib/numbers_and_words/translations/en.rb", diff --git a/spec/numbers_and_words/array/fixture_examples/da.yml b/spec/numbers_and_words/array/fixture_examples/da.yml new file mode 100644 index 00000000..fdb1b69c --- /dev/null +++ b/spec/numbers_and_words/array/fixture_examples/da.yml @@ -0,0 +1,47 @@ +to_words: + simple_example: + ? + - 1 + - 2 + - 3 + : + - et + - to + - tre + complex_example: + ? + - 100 + - 21 + - 13 + : + - et hundrede + - enogtyve + - tretten + complex_example2: + ? + - 101 + - 203 + - 209 + : + - et hundrede et + - to hundrede tre + - to hundrede ni + complex_example3: + ? + - 561 + - 422 + - 932 + : + - fem hundrede enogtres + - fire hundrede toogtyve + - ni hundrede toogtredive + complex_example4: + ? + - 1111 + - 2222 + - 6398 + : + - et tusinde et hundrede elleve + - to tusinde to hundrede toogtyve + - seks tusinde tre hundrede otteoghalvfems + diff --git a/spec/numbers_and_words/integer/fixture_examples/da.yml b/spec/numbers_and_words/integer/fixture_examples/da.yml new file mode 100644 index 00000000..c74a85b9 --- /dev/null +++ b/spec/numbers_and_words/integer/fixture_examples/da.yml @@ -0,0 +1,5 @@ +to_words: + ones: + 0: nul + 1: et + 9: ni