-
Notifications
You must be signed in to change notification settings - Fork 0
/
Language toggle.txt
87 lines (73 loc) · 2.81 KB
/
Language toggle.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
https://cloud.google.com/translate/docs/reference/api-overview
https://cloud.google.com/translate/docs/reference/libraries/v2/php
https://cloud.google.com/translate/docs/reference/api-overview#client_libraries
https://youtu.be/lEccoYhzyos
https://www.youtube.com/watch?v=ByraB2uMAiM
ChatGPT copied code:
Language Files: Keep language files in a separate directory for better organization.
Copy code
languages/
├── en.php
└── fr.php
Example content of en.php:
php
Copy code
<?php
return [
'welcome_message' => 'Welcome to our website!',
'about_us' => 'About Us',
'contact_us' => 'Contact Us',
// Add more key-value pairs as needed
];
Language Manager Class: Create a LanguageManager class to handle language loading and switching.
php
Copy code
<?php
class LanguageManager {
private $language;
public function __construct($langCode = 'en') {
$langFilePath = 'languages/' . $langCode . '.php';
if (file_exists($langFilePath)) {
$this->language = include($langFilePath);
} else {
// Fallback to English if language file not found
$this->language = include('languages/en.php');
}
}
public function get($key) {
return isset($this->language[$key]) ? $this->language[$key] : $key;
}
}
Language Switching Logic: Implement language switching using sessions instead of cookies for better security and flexibility.
php
Copy code
<?php
session_start();
if (isset($_POST['language'])) {
$_SESSION['preferred_language'] = $_POST['language'];
}
// Redirect back to the previous page
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
HTML Interface: Create a language selector form.
html
Copy code
<form method="post" action="switch_language.php">
<select name="language" onchange="this.form.submit()">
<option value="en" <?php if($_SESSION['preferred_language'] == 'en') echo 'selected'; ?>>English</option>
<option value="fr" <?php if($_SESSION['preferred_language'] == 'fr') echo 'selected'; ?>>French</option>
<!-- Add more language options as needed -->
</select>
</form>
Usage: Instantiate the LanguageManager and use it to display language-specific content.
php
Copy code
<?php
session_start();
include 'LanguageManager.php';
$langManager = new LanguageManager($_SESSION['preferred_language'] ?? 'en');
// Example usage
echo $langManager->get('welcome_message');
echo $langManager->get('about_us');
echo $langManager->get('contact_us');
This approach provides better separation of concerns and improves maintainability. It's also more secure by using sessions instead of cookies for language preferences. Additionally, it ensures a smoother user experience by avoiding unnecessary page reloads when switching languages.