Naev

Module format

Provides string formatting and interpolation facilities.

The benefits of using it are easy internationalization and consistency with the rest of Naev. The number formatters handle plural forms in the user's language, digit separators, and abbreviations. String interpolation (format.f) allows for named parameters ("Fly to {planet} in the {system} system"), whereas string.format is positional ("Fly to %s in the %s system") and locks translators into the English word order.

Functions

number (number) Converts a nonnegative integer into a human readable string, delimiting every third digit with a comma.
credits (credits) Converts a number of credits to a string.
tonnes (tonnes) Converts a number of tonnes to a string, using ngettext.
tonnes_short (tonnes) Like fmt.tonnes, but for abbreviations.
jumps (jumps) Converts a number of jumps to a string, utilizing ngettext.
times (times) Converts a number of times (occurrences) to a string, using ngettext.
list (words) A version of string.gsub returning just the string.
f (str, tab) String interpolation, inspired by f-strings without debug stuff.


Functions

number (number)
Converts a nonnegative integer into a human readable string, delimiting every third digit with a comma. If you pass a more exotic "number" value, you'll get a string back, but you won't attain happiness.

Parameters:

  • number The number to format. Will be rounded to the nearest integer.
credits (credits)
Converts a number of credits to a string.

Should be used everywhere a number of credits is displayed.

Parameters:

  • credits Number of credits.

Returns:

    A string taking the form of "X ¤".

Usage:

    tk.msg("", _("You have been paid %s."):format(fmt.credits(credits)))
tonnes (tonnes)
Converts a number of tonnes to a string, using ngettext.

This adds "tonnes" to the output of fmt.number in a translatable way. Should be used everywhere a number of tonnes is displayed.

Parameters:

  • tonnes Number of tonnes.

Returns:

    A string taking the form of "X tonne" or "X tonnes".

Usage:

    tk.msg("", _("You are carrying %s."):format(fmt.tonnes(tonnes)))
tonnes_short (tonnes)
Like fmt.tonnes, but for abbreviations.

Parameters:

  • tonnes Number of tonnes.

Returns:

    A short string like "22 t" describing the given mass.
jumps (jumps)
Converts a number of jumps to a string, utilizing ngettext.

This adds "jumps" to the output of fmt.number in a translatable way. Should be used everywhere a number of jumps is displayed.

Parameters:

  • jumps Number of jumps.

Returns:

    A string taking the form of "X jump" or "X jumps".

Usage:

    tk.msg("", _("The system is %s away."):format(fmt.jumps(jumps)))
times (times)
Converts a number of times (occurrences) to a string, using ngettext.

This adds "times" to the output of fmt.number in a translatable way. Should be used everywhere a number of occurrences is displayed.

Parameters:

  • times Number of times.

Returns:

    A string taking the form of "X time" or "X times".

Usage:

    tk.msg("", _("Brush your teeth % per day."):format(fmt.times(times)))
list (words)
A version of string.gsub returning just the string. --]]local function _replace(template, index, text) local str = string.gsub(template, index, text) return str end

--[[-- Creates a translatable list of words.

Taken from this post.

Parameters:

  • words table List of words to translate such as {"one","two","three"}.

Returns:

    string String of the list of words such as "one, two, and three"
f (str, tab)
String interpolation, inspired by f-strings without debug stuff.

Prefer this over string.format because it allows translations to change the word order.

Parameters:

  • str string Format string which may include placeholders of the form "{var}" "{var:6.3f}" (where the expression after the colon is any directive string.format understands).
  • tab table Argument table.

Usage:

    fmt.f(_("Deliver the loot to {pntname} in the {sysname} system"),{pntname=returnpnt:name(), sysname=returnsys:name()})
generated by LDoc 1.4.6 Last updated 2021-10-20 12:25:39