Add basic number localization

This commit is contained in:
Wuzzy 2022-09-14 15:09:31 +02:00
parent 390e03898a
commit 672c219ab3
10 changed files with 79 additions and 5 deletions

View File

@ -49,6 +49,7 @@ Mods with documented APIs:
* `rp_item_drop`: Add a function to simulate an item drop
* `rp_itemshow`: Needed when your item needs a custom appearance in the item frame / item showcase
* `rp_jewels`: Register jeweled tools, and more
* `rp_loalize`: Localize numbers
* `rp_locks`: Get info about lockable nodes
* `rp_partialblocks`: Register partial blocks (slabs, stairs)
* `rp_player`: Player model handling, model animation, textures

View File

@ -183,7 +183,7 @@ local function get_stat(format_text, stats_key, parent, stats)
disp_val = disp_val + parent.stats[stats_key]
end
if disp_val ~= 0 then
return S(format_text, plus_power(disp_val))
return S(format_text, loc.num(plus_power(disp_val)))
end
return nil
end

View File

@ -1,3 +1,3 @@
name = rp_jewels
depends = rp_sounds, rp_default, rp_formspec, rp_crafting, rp_achievements, rp_item_drop
depends = rp_sounds, rp_default, rp_formspec, rp_crafting, rp_achievements, rp_item_drop, rp_localize
optional_depends = tt

13
mods/rp_localize/API.md Normal file
View File

@ -0,0 +1,13 @@
# `rp_localize`
Use this mod if you want to print a number respecting the current locale.
## `loc.num(numbr)`
Takes a number `numbr` and return a formatted string representing the number appropirately for the current locale.
For positive integers, the output is the same as for `tostring`.
For convenience, if `numbr` is a string, it will first be attempted to internally convert it to a number.
If it succeeds, it is localized. If it fails, the string is returned unchanged.
This function currently supports the decimal point, the minus sign and the infinity sign. NaNs will be shown as `tostring` would show them.

43
mods/rp_localize/init.lua Normal file
View File

@ -0,0 +1,43 @@
local S = minetest.get_translator("rp_localize")
local INFINITY = 1/0
local NEG_INFINITY = -1/0
loc = {}
loc.num = function(numbr)
if type(numbr) == "string" then
numbr = tonumber(numbr)
if type(numbr) ~= "number" then
return numbr
end
end
if minetest.is_nan(numbr) then
return tostring(numbr)
end
if numbr == INFINITY then
return S("")
elseif numbr == NEG_INFINITY then
return S("@1", S(""))
end
local negative
if numbr < 0 then
negative = true
numbr = math.abs(numbr)
end
local pre = math.floor(numbr)
local post = numbr % 1
local str
if post ~= 0 then
post = string.sub(post, 3)
if negative then
str = S("@1.@2", pre, post)
else
str = S("@1.@2", pre, post)
end
elseif negative then
str = S("@1", numbr)
else
str = tostring(numbr)
end
return str
end

View File

@ -0,0 +1,5 @@
# textdomain:rp_localize
∞=∞
@1.@2=@1,@2
@1.@2=@1,@2
@1=@1

View File

@ -0,0 +1,10 @@
# textdomain:rp_localize
; Symbol for infinity
∞=
; Translation of the decimal point. @1 and @2 are the number parts
; in front and behind the decimal point.
@1.@2=
; Same as above, but with a minus sign in front
@1.@2=
; Negative number
@1=

View File

@ -0,0 +1,2 @@
name = rp_localize
description = Localization tools

View File

@ -64,7 +64,7 @@ tt.register_snippet(function(itemstring)
full_punch_interval = 1
end
desc = newline(desc)
desc = desc .. S("Full punch interval: @1s", string.format("%.2f", full_punch_interval))
desc = desc .. S("Full punch interval: @1s", loc.num(string.format("%.2f", full_punch_interval)))
end
end
if desc == "" then
@ -115,7 +115,7 @@ tt.register_snippet(function(itemstring)
desc = desc .. minetest.colorize(tt.COLOR_GOOD, S("No fall damage"))
elseif tmp < 0 then
desc = newline(desc)
desc = desc .. minetest.colorize(tt.COLOR_DEFAULT, S("Fall damage: @1%", tmp))
desc = desc .. minetest.colorize(tt.COLOR_DEFAULT, S("Fall damage: @1%", loc.num(tmp)))
end
-- Movement-related node facts

View File

@ -1,3 +1,3 @@
name = tt_base
description = Adds generic tooltip extensions to items
depends = tt
depends = tt, rp_localize