Posted by
| Nick Gammon
Australia (23,122 posts) Bio
Forum Administrator |
Message
| How to detect which messages are used the most
Reading the gettext documentation gave me another idea. There are something like 750 messages to be localized. It would be nice to initially translate the common ones, and then work back to the less common ones.
We can do this fairly readily, because the translation file is in fact a Lua script, and we can make use of metatables to catch accesses to the translation items.
As an example, I appended the following to my en.lua file:
-- make copy of original tables
orig_messages = messages
orig_formatted = formatted
orig_times = times
orig_headings = headings
-- empty them out so __index is triggered
-- save original tables so we can look them up eventually
messages = { _orig = orig_messages }
formatted = { _orig = orig_formatted }
times = { _orig = orig_times }
headings = { _orig = orig_headings }
counts = {} -- keep counts here
-- metatable for messages, titles, headings
mt_static = {
-- called to access an entry
__index=
function (t, name)
local s = rawget (t._orig, name)
if s == nil or #s == 0 then
counts [name] = (counts [name] or 0) + 1
end -- not translated yet
return s
end;
}
-- metatable for formatted messages
mt_formatted = {
-- called to access an entry
__index=
function (t, name)
local f = rawget (t._orig, name)
-- no function? not translated then
if f == nil then
counts [name] = (counts [name] or 0) + 1
return nil
end
assert (type (f) == "function")
-- return a function, that will count if the original function
-- returns an empty string
return function (...)
local s = f (...) -- call original function
if type (s) ~= "string" or #s == 0 then
counts [name] = (counts [name] or 0) + 1
end -- not translated
return s -- return translated value
end -- function
end;
}
-- apply the metatables
setmetatable (messages, mt_static)
setmetatable (times, mt_static)
setmetatable (headings, mt_static)
setmetatable (formatted, mt_formatted)
-- the user will call world.TranslateDebug to invoke this
function Debug ()
-- for sorting
local t = {}
-- build into table which can be sorted
for k, v in pairs (counts) do
table.insert (t, k)
end -- for
-- clear out notepad, make heading
utils.appendtonotepad ("translation", "Translation counts\n\n", true)
-- sort into descending order
table.sort (t, function (a, b)
return counts [a] > counts [b]
end)
-- display results
for k, v in ipairs (t) do
utils.appendtonotepad ("translation", string.format ("%4i: %q \n", counts [v], v))
end -- for
end -- Debug
[EDIT] Amended 20th June 2007 - to fix problem where it didn't correctly look up the original message.
What this does is replace the translation tables by empty ones (after making a copy of them). Then by adding a metatable to the new, empty, tables, we will get a trigger to the __index function each time we attempt to locate a message to be translated. The __index function simply counts the number of times this item has been accessed, and then returns the saved, original, value.
Finally we make a Debug function that can be called by the (new) script function TranslateDebug. Now if we type into the command window:
/TranslateDebug ()
We see something like this:
Translation counts
12: "Ready"
2: "Alias: %s"
2: "Clipboard converted for use with the Forum, %i change%s made"
2: "&Flip To World Ctrl+Alt+Space"
2: "&Send To World Shift+Ctrl+S"
2: "The connection to %s is not open. Attempt to reconnect?"
2: "Connecting to %s, port %d"
2: "--- Connected on %A, %B %d, %Y, %#I:%M %p ---"
1: "Trigger: %s"
1: "For information and assistance about MUSHclient visit our forum at:"
1: "Go to forum"
1: "Execution of line %i column %i"
1: "--- Disconnected on %A, %B %d, %Y, %#I:%M %p ---"
1: "Opening world \"%s\""
1: "World: %s"
1: "No active world"
1: "Script error"
1: "--- Connected for %i day%s, %i hour%s, %i minute%s, %i second%s. ---"
1: "The \"%s\" server has closed the connection"
1: "Immediate execution"
1: "Written by Nick Gammon."
Thus we see that in this (short) session, the above messages needed to be translated. In particular "Ready" was the most often used, thus we might translate that first.
The script shown above has enough complexity so that it doesn't count messages that are already translated. Thus, as you translate some, they will drop off the list, and the next most frequent ones can be translated. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|