module

Rosetta

Constants

VERSION = {{ (`shards version \"/tmp/tmp.GIbnAE/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.

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

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

locale

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

locale=(locale : String | Symbol)

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

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")
localize_time(time : Tuple(Int32, Int32, Int32), format : String)
pluralize(count : Rosetta::CountArg, translation : NamedTuple, rule : Pluralization::Rule = Pluralization::RULES[Rosetta.locale].new)

Pluralizes a given pluralizable translation.

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.

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.

with_locale(locale : String | Symbol, &)

Temporarily use a different locale.

Macros

available_locales

Fetches the available locales from the corresponding annotation.

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)
default_locale

Fetches the default locale from the corresponding annotation.

find(key)

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, a compile error will be raised.

number(format = :default)

Localizes a numeric value, for example:

Rosetta.number.t(123_456.789)
Rosetta.number(:custom).l(123_456.789)
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)

Nested types