From: Alvaro Herrera <alvherre@alvh.no-ip.org>
To: Alexander Farber <alexander.farber@gmail.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Localizing stored functions by replacing placeholders in their body
Date: Tue, 2 Mar 2021 17:19:00 -0300
Message-ID: <20210302201900.GA14417@alvherre.pgsql> (raw)
In-Reply-To: <CAADeyWiVL-N0XPipT--ypL5BzCnpSe6+c8RdHaKnx6u6PoHqJA@mail.gmail.com>
On 2021-Mar-02, Alexander Farber wrote:
> CREATE OR REPLACE FUNCTION localize_hello()
> RETURNS text AS
> $func$
> SELECT '$(hello)';
> $func$ LANGUAGE sql IMMUTABLE;
I'm not sure this is a great approach to in-database translations: you
have one function per string, which is cumbersome, bloated and probably
slow. I would suggest having a function that takes a string and returns
its translation, which is obtained from a couple of tables: one where
the original strings are stored and another which stores the
translations for each string into each language.
(You can have the target language be a property of the database, or a
custom GUC setting that the application sets at the start, or just
passed as an argument to the translate() function from somewhere. Or
maybe each database always has exactly one language. Whatever suits you
best.)
So the functions that your application calls return strings by doing
stuff like
SELECT translate('one UFO came and stole one bike');
and they'll get whatever is right for them. The functions only need to
worry about calling translate() in all the right places; they don't need
to individually worry about fetching the translation etc.
Note that in that design, the original string appears in two places: the
function source code, and the original-strings table. You could go one
step further and have the function store a code (UUID?) for the string;
then if a message has a typo, you're just one UPDATE away from fixing it
instead of an ALTER FUNCTION. And also, it's easy to change all
translations together if your UFOs are actually ordinary burglars.
Exercise for the reader: what if your strings have format specifiers?
"%d UFOs came and stole %d bikes"
--
Álvaro Herrera 39°49'30"S 73°17'W
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: alvherre@alvh.no-ip.org, alexander.farber@gmail.com
Subject: Re: Localizing stored functions by replacing placeholders in their body
In-Reply-To: <20210302201900.GA14417@alvherre.pgsql>
References: <CAADeyWiVL-N0XPipT--ypL5BzCnpSe6+c8RdHaKnx6u6PoHqJA@mail.gmail.com> <20210302201900.GA14417@alvherre.pgsql>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox