@@ -1421,7 +1421,7 @@ Mongoid supports localized fields via `i18n <https://github.com/ruby-i18n/i18n>`
1421
1421
1422
1422
class Product
1423
1423
include Mongoid::Document
1424
- field :description, localize: true
1424
+ field :description, type: String, localize: true
1425
1425
end
1426
1426
1427
1427
By telling the field to ``localize``, Mongoid will under the covers store the field
@@ -1447,6 +1447,36 @@ You can get and set all the translations at once by using the corresponding ``_t
1447
1447
product.description_translations =
1448
1448
{ "en" => "Marvelous!", "de" => "Wunderbar!" }
1449
1449
1450
+ Localized fields can be used with any field type. For example, they can be used
1451
+ with float fields for differences with currency:
1452
+
1453
+ .. code:: ruby
1454
+
1455
+ class Product
1456
+ include Mongoid::Document
1457
+
1458
+ field :price, type: Float, localize: true
1459
+ field :currency, type: String, localize: true
1460
+ end
1461
+
1462
+ By creating the model in this way, we can separate the price from the currency
1463
+ type, which allows you to use all of the number-related functionalities on the
1464
+ price when querying or aggregating that field (provided that you index into the
1465
+ stored translations hash). We can create an instance of this model as follows:
1466
+
1467
+ .. code:: ruby
1468
+
1469
+ product = Product.new
1470
+ I18n.locale = :en
1471
+ product.price = 1.00
1472
+ product.currency = "$"
1473
+ I18n.locale = :he
1474
+ product.price = 3.24
1475
+ product.currency = "₪"
1476
+
1477
+ product.attributes
1478
+ # => { "price" => { "en" => 1.0, "he" => 3.24 }, "currency" => { "en" => "$", "he" => "₪" } }
1479
+
1450
1480
1451
1481
.. _present-fields:
1452
1482
0 commit comments