How can gettext variables be stored in a database for internationalization in PHP?
To store gettext variables in a database for internationalization in PHP, you can create a table in your database with columns for the original text (in the default language) and the translated text for each language. When retrieving the text in your PHP code, you can query the database based on the current language and get the corresponding translated text.
// Connect to your database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "your_database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Set the current language
$lang = "en_US"; // for example
// Query the database for the translated text
$sql = "SELECT translated_text FROM translations WHERE original_text = 'Hello' AND language = '$lang'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["translated_text"];
}
} else {
echo "Translation not found";
}
$conn->close();
Keywords
Related Questions
- Are there any best practices for converting times to timestamps in PHP to accurately calculate time differences?
- How can the fetch method in PHP PDO be utilized to retrieve specific column values from a database query?
- What are the advantages of using a foreach loop over a for loop when iterating through PHP arrays?