Pluralize common variant unit names

This adds the most popular unit names as singular and plural to our
locale for translation. The added Javascript performs a reverse lookup
to find the right singular/plural form of a unit name in that language.
This commit is contained in:
Maikel Linke
2020-01-23 11:27:01 +11:00
parent f797745848
commit 98b55287f1
3 changed files with 137 additions and 3 deletions

View File

@@ -21,9 +21,7 @@ angular.module("admin.products").factory "OptionValueNamer", (VariantUnitManager
else
value = @variant.unit_value
unit_name = @variant.product.variant_unit_name
# TODO needs to add pluralize to line below
# unit_name = unit_name if value > 1
unit_name = @pluralize(@variant.product.variant_unit_name, value)
value = parseInt(value, 10) if value == parseInt(value, 10)
@@ -32,6 +30,21 @@ angular.module("admin.products").factory "OptionValueNamer", (VariantUnitManager
[value, unit_name]
pluralize: (unit_name, count) ->
return unit_name if count == undefined
unit_key = @unit_key(unit_name)
return unit_name unless unit_key
I18n.t(["unit_names", unit_key], {count: count, defaultValue: unit_name})
unit_key: (unit_name) ->
unless I18n.unit_keys
I18n.unit_keys = {}
for key, translations of I18n.t("unit_names")
for quantifier, translation of translations
I18n.unit_keys[translation.toLowerCase()] = key
I18n.unit_keys[unit_name.toLowerCase()]
option_value_value_unit_scaled: ->
[unit_scale, unit_name] = @scale_for_unit_value()

View File

@@ -2721,6 +2721,101 @@ See the %{link} to find out more about %{sitename}'s features and to start using
have_an_account: "Already have an account?"
action_login: "Log in now."
# Most popular names used in variant_unit_name.
# We use these entries to pluralize unit names in every language.
#
# Extracted with the following query:
# Spree::Product.group(:variant_unit_name).order("count_all DESC").count.each { |name, count|
# puts " # Used #{count} times."
# puts " #{name&.parameterize('_')}:"
# puts " one: \"#{name}\""
# puts " other: \"#{name}s\"";
# }
unit_names:
# Used 379 times.
# Used 138 times.
each:
one: "each"
other: "each"
# Used 332 times.
# Used 145 times.
# Used 93 times.
bunch:
one: "bunch"
other: "bunches"
# Used 118 times.
# Used 63 times.
pack:
one: "pack"
other: "packs"
# Used 90 times.
# Used 72 times.
box:
one: "box"
other: "boxes"
# Used 81 times.
# Used 49 times.
bottle:
one: "bottle"
other: "bottles"
# Used 71 times.
# Used 56 times.
jar:
one: "jar"
other: "jars"
# Used 65 times.
head:
one: "head"
other: "heads"
# Used 56 times.
# Used 41 times.
bag:
one: "bag"
other: "bags"
# Used 53 times.
# Used 30 times.
loaf:
one: "loaf"
other: "loaves"
# Used 43 times.
single:
one: "single"
other: "singles"
# Used 41 times.
tub:
one: "tub"
other: "tubs"
# Used 36 times.
punnet:
one: "punnet"
other: "punnets"
# Used 32 times.
packet:
one: "packet"
other: "packets"
# Used 30 times.
# Used 22 times.
item:
one: "item"
other: "items"
# Used 29 times.
# Used 24 times.
dozen:
one: "dozen"
other: "dozens"
# Used 26 times.
unit:
one: "unit"
other: "units"
# Used 24 times.
serve:
one: "serve"
other: "serves"
# Used 20 times.
tray:
one: "tray"
other: "trays"
producers:
signup:
start_free_profile: "Start with a free profile, and expand when you're ready!"

View File

@@ -0,0 +1,26 @@
describe "OptionValueNamer", ->
subject = null
beforeEach ->
module('admin.products')
inject (_OptionValueNamer_) ->
subject = new _OptionValueNamer_
describe "pluralize a variant unit name", ->
it "returns the same word if no plural is known", ->
expect(subject.pluralize("foo", 2)).toEqual "foo"
it "returns the same word if we omit the quantity", ->
expect(subject.pluralize("loaf")).toEqual "loaf"
it "finds the plural of a word", ->
expect(subject.pluralize("loaf", 2)).toEqual "loaves"
it "finds the singular of a word", ->
expect(subject.pluralize("loaves", 1)).toEqual "loaf"
it "finds the zero form of a word", ->
expect(subject.pluralize("loaf", 0)).toEqual "loaves"
it "ignores upper case", ->
expect(subject.pluralize("Loaf", 2)).toEqual "loaves"