module

Rosetta

Constants

LOCALE_REGEX = /\A([a-z]{2,3})(?:[_-]([A-Z0-9]{2,4}))?/
NESTED_KEY_REGEX = /%r\{\s*([^}]+)\s*\}/
VERSION = {{ (`shards version \"/tmp/tmp.EKHPdj/src/src/rosetta\"`).chomp.stringify }}

Class methods

distance_of_time_in_words(from : Time, to : Time) : String

Returns a String with approximate distance in time between from and to. For example:

Rosetta.distance_of_time_in_words(
  Time.utc(2021, 10, 15, 8, 0, 0),
  Time.utc(2021, 10, 15, 8, 0, 5))
)
# => "5 seconds"

Most of the code for this method is borrowed from Lucky.

Source
interpolate(translation : String, values : Hash | NamedTuple)

Interpolates a given string with the values from the given hash or named tuple.

Source
locale

Gets the current locale at runtime using the config instance stored in the current fiber.

Source
locale=(locale : String | Symbol)

Sets the current locale at runtime using the config instance stored in the current fiber.

Source
localize_time(time : Time, format : String)

Uses a given format to localize a given Time object, for example:

Rosetta.localize_time(Time.local, "%d %b %Y %H:%M:%S")
Source
localize_time(time : Tuple(Int32, Int32, Int32), format : String)
Source
pluralize(count : Rosetta::CountArg, translation : NamedTuple, rule : Pluralization::Rule = Pluralization::RULES[Rosetta.locale].new)

Pluralizes a given pluralizable translation.

Source
time_ago_in_words(from : Time) : String

Returns a String with approximate distance in time between from and current moment.

time_ago_in_words(Time.utc(2019, 8, 30))
# => "about a month"
# gives the same result as:
distance_of_time_in_words(Time.utc(2019, 8, 30), Time.utc)
# => "about a month"

See more examples in #distance_of_time_in_words.

Source
time_from_now_in_words(to : Time) : String

Returns a String with approximate distance in time between current moment and future date.

time_from_now_in_words(Time.utc(2022, 8, 30))
# => "about a year"
# gives the same result as:
distance_of_time_in_words(Time.utc, Time.utc(2022, 8, 30))
# => "about a year"

See more examples in #distance_of_time_in_words.

Source
with_locale(locale : String | Symbol, &)

Temporarily use a different locale.

Source

Macros

available_locales

Fetches the available locales from the corresponding annotation.

Source
date(format = :default)

Localizes a date, for example:

Rosetta.date.t(Time.local)
Rosetta.date.t({2021, 8, 20})
Rosetta.date(:long).l(Time.local)
Rosetta.date("%a, %d %b %Y").l(Time.local.date)
Source
default_locale

Fetches the default locale from the corresponding annotation.

Source
find(key, default = nil)

Finds the translations for the given key as a dedicated class instance for the translation, which inherits from Rosetta::Translation.

If the key does not exist, adn no default value is given, a compile error will be raised.

Source
number(format = :default)

Localizes a numeric value, for example:

Rosetta.number.t(123_456.789)
Rosetta.number(:custom).l(123_456.789)
Source
time(format = :default)

Localizes time, for example:

Rosetta.time.t(Time.local)
Rosetta.time(:short).l(Time.local)
Rosetta.time("%d %b %Y %H:%M:%S").l(Time.local)
Source

Nested types