SQLpage icon indicating copy to clipboard operation
SQLpage copied to clipboard

Feature request: Internationalization

Open maekoos opened this issue 1 year ago • 1 comments

It would be really nice to have sqlpage handle internationalization for us. Maybe using a cookie for switching between languages and fallback to Accept-Language, and a json file in sqlpage directory for adding translations for each locale.

Right now the only option would be to use a (temporary?) table or write a function for handling this, which would require manually reading from the headers and cookies for every single text on the web page.

I'm thinking something like this:

SELECT 'text' AS component, sqlpage.i18n('home_title') as title;
{
  "en": { "home_title": "Welcome!" },
  "sv": { "home_title": "Välkommen!" }
}

Wouldn't be too hard to implement and should be sufficient for pretty much all applications.

maekoos avatar Sep 23 '24 22:09 maekoos

Hi ! Here is a link to a previous discussion on this topic: https://github.com/sqlpage/SQLPage/discussions/248 I am not excluding implementing the solution described in this feature request in the future, but the current recommended solution is to handle internationalization directly in the database, by defining your own custom localized message fetching logic.

An example of what this could look like is:

SET lang = sqlpage.cookie('lang');

select 'hero' as component, t($lang, 'home_title') as title;

with a translation function defined like this (postgresql example):

CREATE OR REPLACE FUNCTION t(language VARCHAR(5), key VARCHAR(255))
RETURNS VARCHAR(255) AS $$
DECLARE
    result VARCHAR(255);
BEGIN
    SELECT translation_value INTO result
    FROM language
    WHERE language_code = language AND translation_key = key;
    RETURN result;
END;
$$ LANGUAGE plpgsql;

lovasoa avatar Nov 13 '24 17:11 lovasoa