Disons que vous avez une table:
create table countries (
country_id int primary key,
short_name text not null unique,
official_name text not null unique,
iso_code char(2) not null unique
);
insert into countries values (12, 'Algeria', 'The People''s Democratic Republic of Algeria' 'DZ');
Ensuite, vous créez une table de traduction:
create table countries_t (
country_id int not null references countries(country_id),
short_name text not null,
official_name text not null,
locale varchar(5) not null,
primary key (country_id, locale)
);
insert into countries_t values
(12, 'Algérie', 'la République algérienne démocratique et populaire', 'fr'),
(12, 'Algerien', 'Demokratische Volksrepublik Algerien', 'de-DE');
Créer une vue de renvoyer les données à partir d'une user - variable de session defined. Ci-dessous est spécifique à PostgreSQL, mais votre base de données pourrait soutenir les variables de session personnalisées, autre table temporaire de l'utilisation:
create view countries_i18n as
select
a.country_id,
coalesce(c.short_name, b.short_name, a.short_name) as short_name, --default to countries.name if translation not found
coalesce(c.official_name, b.official_name, a.official_name) as official_name
countries.iso_code
from countries a
left join countries_t b on b.id = a.id and b.locale = current_setting('my_custom_guc.locale')
left join countries_t c on c.id = a.id and c.locale = left(current_setting('my_custom_guc.locale'), 2); --fall-back to 2-letter locale
Interrogation la table en allemand parlé en Allemagne:
SET my_custom_guc.language_code = 'de-DE';
select country_id, iso_code, short_name, official_name from countries_i18n;
country_id iso_code short_name official_name
-----------------------------------------------
12 DZ Algerien Demokratische ...
Interrogez la table canadienne-française (retombe vers le français générique):
SET my_custom_guc.locale= 'fr-CA';
select country_id, iso_code, short_name, official_name from countries_i18n;
country_id iso_code short_name official_name
-----------------------------------------------
12 DZ Algérie la République ...
Interrogation la table en espagnol (il n'y a pas de traduction, retourne l'anglais):
SET my_custom_guc.language_code = 'es';
select country_id, iso_code, short_name, official_name from countries_i18n;
country_id iso_code short_name official_name
-----------------------------------------------
12 DZ Algeria The People's D ...