How can PHP developers effectively manage and translate gettext variables stored in a database?

When managing gettext variables stored in a database, PHP developers can effectively retrieve and translate these variables by querying the database for the desired text and using the gettext function to translate it. By setting the appropriate text domain and language, developers can ensure that the translated text is displayed correctly on the website.

// Set text domain and language for translations
$text_domain = 'messages';
$language = 'en_US';

// Query database for gettext variable
$query = "SELECT text FROM translations WHERE id = :id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':id', $id);
$stmt->execute();
$result = $stmt->fetch();

// Translate gettext variable
$translated_text = gettext($result['text']);
echo $translated_text;