I'd like to get i18n working in my OCaml code (translating text messages into the user's preferred language). Most translatable strings are the first non-labelled argument to one of a small group of functions. e.g.
print "Feed '%s':" new_feed
raise_safe "Local feed file '%s' does not exist" path
log_warning ~ex "Error checking signature for %s" feed
In the GUI code, strings in ~label
arguments should be extracted too.
Is there some way I can extract these strings automatically? To make things more interesting, I use some syntax extensions (e.g. Lwt) too.
I see there is an ocaml-gettext package, but:
f_
, s_
, etc).$NAME
first), then failed to uninstall too, which also suggests it's not heavily used.What do people actually use/recommend?
This is an issue I want to address in my Gasoline project, an application toolkit.
In the examples, there is an over-engineered wordcount program, which serves as use-case for the library. The function Component.Message.cannot_open
uses its format string as a key in an internationalised messages database:
let cannot_open name reason =
send sink Error "${FILENAME:s}: cannot open (${REASON:s})"
[ "FILENAME", make String name;
"REASON", make String reason;
]
The sink
is a global state of the software component it belongs to, it is bound to a database of internationalised messages where a translation is looked up and used to prepare the actual message.
Was suggested by the example, this facility supports formatting indications, much like printf
does, we may write ${FILENAME:+20s}
for instance.
For now, there is no actual database lookup implemented. The send
function is defined in the functor Generic_message.Sink.Make
parametrised by a Database
module. The current implementation of wordcount
uses the following implementation of the database:
module Database =
struct
type t = unit
type connection_token = unit
let opendb () = ()
let find () id = id
let close () = ()
end
It is however possible to replace this definition with an actual database lookup — which is in the backlog.
In my opinion, message internationalisation should be accomplished by software components — the largest organisational unity below the whole application — and libraries or similar facilities should use standard constant sum types to report errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With