Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHBU8-0004PT-E2 for pgsql-general@arkaria.postgresql.org; Tue, 02 Mar 2021 20:19:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lHBU7-0003tD-71 for pgsql-general@arkaria.postgresql.org; Tue, 02 Mar 2021 20:19:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHBU6-0003t6-Vu for pgsql-general@lists.postgresql.org; Tue, 02 Mar 2021 20:19:10 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHBU4-0003vf-Q9 for pgsql-general@postgresql.org; Tue, 02 Mar 2021 20:19:10 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 34DFC3F4; Tue, 2 Mar 2021 15:19:04 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Tue, 02 Mar 2021 15:19:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=reaIPW6R5iydmFf3/39xoA5sLCuCFhasPu2VzJub3Vg=; b=AVOPPnhw EiL3/tGFPh+uRQl8vrKGg4Xxo6EVKYhPcLujIm2bcYMQHUWe6F/+VeqYf3F5RHYN ngBp1i4Hk8UrxnOvhm3J2QdVC8mLJkfn86bxvYudIpsHEerf8fYwwm2FW9U3YILH uX2HkGAQiXqXgiNV75OvG0UVERwlQ39tCW7Zsakd1zH9tI75SnNzUk0H1C9+GLlS 6I8Tju3s+zB3VraDPjJs9E1UHvowfNtYS9mdV4krP4+FbFvG7PQnRR+NrOsYoNJx cwJa2MKT52n1vtzOPr9XNyPjVhVqOes7Z2ceUYaCxg8OGN9S46IRn+LaJxpuCEL2 x/GU/tGF/hmHcg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledruddttddguddvkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvuffkgggtugfgjggfsehtkeertddtredunecuhfhrohhmpeetlhhv rghrohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorh hgqeenucggtffrrghtthgvrhhnpeeufffhjeeiueeuffegvddukeegledtveeivdeiueef ieeivefgteehueefteehvdenucfkphepudeltddrleehrdduledrleehnecuvehluhhsth gvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprghlvhhhvghrrhgvsegr lhhvhhdrnhhoqdhiphdrohhrgh X-ME-Proxy: Received: from perhan.alvh.no-ip.org (unknown [190.95.19.95]) by mail.messagingengine.com (Postfix) with ESMTPA id 64D921080057; Tue, 2 Mar 2021 15:19:03 -0500 (EST) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id C28A52A14F6; Tue, 2 Mar 2021 17:19:00 -0300 (-03) Date: Tue, 2 Mar 2021 17:19:00 -0300 From: Alvaro Herrera To: Alexander Farber Cc: pgsql-general Subject: Re: Localizing stored functions by replacing placeholders in their body Message-ID: <20210302201900.GA14417@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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