Implement dynamic language system with automatic detection
- Add getAvailableLanguages() method to scan lang directory automatically
- Add getNativeLanguageName() method for proper language display names
- Enhance SimpleTemplate engine to support array iteration with {{#array}} syntax
- Update header template to use dynamic language dropdown with native names
- Add German (de.php) and French (fr.php) language files as examples
- Fix search input text color to use black text for better visibility
- Languages now appear automatically when added to engine/lang/ without code changes
This commit is contained in:
@@ -73,6 +73,65 @@ class CodePressCMS {
|
||||
return file_exists($defaultLangFile) ? include $defaultLangFile : [];
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get all available languages from lang directory
|
||||
*
|
||||
* @return array Available languages with their codes and names
|
||||
*/
|
||||
private function getAvailableLanguages() {
|
||||
$langDir = __DIR__ . '/../../lang/';
|
||||
$languages = [];
|
||||
|
||||
if (!is_dir($langDir)) {
|
||||
return $languages;
|
||||
}
|
||||
|
||||
$files = scandir($langDir);
|
||||
foreach ($files as $file) {
|
||||
if (preg_match('/^([a-z]{2})\.php$/', $file, $matches)) {
|
||||
$langCode = $matches[1];
|
||||
$langFile = $langDir . $file;
|
||||
|
||||
if (file_exists($langFile)) {
|
||||
$translations = include $langFile;
|
||||
$languages[$langCode] = [
|
||||
'code' => $langCode,
|
||||
'name' => $translations['site_title'] ?? strtoupper($langCode),
|
||||
'native_name' => $this->getNativeLanguageName($langCode)
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $languages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get native language name for language code
|
||||
*
|
||||
* @param string $langCode Language code
|
||||
* @return string Native language name
|
||||
*/
|
||||
private function getNativeLanguageName($langCode) {
|
||||
$names = [
|
||||
'nl' => 'Nederlands',
|
||||
'en' => 'English',
|
||||
'fr' => 'Français',
|
||||
'de' => 'Deutsch',
|
||||
'es' => 'Español',
|
||||
'it' => 'Italiano',
|
||||
'pt' => 'Português',
|
||||
'ru' => 'Русский',
|
||||
'zh' => '中文',
|
||||
'ja' => '日本語',
|
||||
'ar' => 'العربية'
|
||||
];
|
||||
|
||||
return $names[$langCode] ?? strtoupper($langCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get translated text
|
||||
*
|
||||
@@ -885,7 +944,10 @@ class CodePressCMS {
|
||||
// Language
|
||||
'current_lang' => $this->currentLanguage,
|
||||
'current_lang_upper' => strtoupper($this->currentLanguage),
|
||||
'available_langs' => $this->config['language']['available'] ?? ['nl', 'en'],
|
||||
'available_langs' => array_map(function($lang) {
|
||||
$lang['is_current'] = $lang['code'] === $this->currentLanguage;
|
||||
return $lang;
|
||||
}, $this->getAvailableLanguages()),
|
||||
// Translations
|
||||
't_home' => $this->t('home'),
|
||||
't_search' => $this->t('search'),
|
||||
|
||||
@@ -39,8 +39,42 @@ class SimpleTemplate {
|
||||
|
||||
// Handle conditional blocks
|
||||
foreach ($this->data as $key => $value) {
|
||||
if (is_array($value) || (is_string($value) && !empty($value))) {
|
||||
// Handle {{#key}}...{{/key}} blocks
|
||||
if (is_array($value)) {
|
||||
// Handle {{#key}}...{{/key}} blocks for arrays (iteration)
|
||||
$pattern = '/{{#' . preg_quote($key, '/') . '}}(.*?){{\/' . preg_quote($key, '/') . '}}/s';
|
||||
if (preg_match($pattern, $template, $matches)) {
|
||||
$blockTemplate = $matches[1];
|
||||
$replacement = '';
|
||||
|
||||
foreach ($value as $item) {
|
||||
if (is_array($item)) {
|
||||
// Create a temporary template instance for nested data
|
||||
$tempTemplate = new self();
|
||||
$tempTemplate->data = $item;
|
||||
$replacement .= $tempTemplate->renderTemplate($blockTemplate);
|
||||
} else {
|
||||
// Simple array, replace {{.}} with the item value
|
||||
$itemBlock = str_replace('{{.}}', htmlspecialchars($item, ENT_QUOTES, 'UTF-8'), $blockTemplate);
|
||||
$replacement .= $itemBlock;
|
||||
}
|
||||
}
|
||||
|
||||
$template = preg_replace($pattern, $replacement, $template);
|
||||
}
|
||||
|
||||
// Handle {{^key}}...{{/key}} blocks (negative condition for empty arrays)
|
||||
$pattern = '/{{\^' . preg_quote($key, '/') . '}}(.*?){{\/' . preg_quote($key, '/') . '}}/s';
|
||||
if (empty($value)) {
|
||||
// Show the content if array is empty
|
||||
if (preg_match($pattern, $template, $matches)) {
|
||||
$template = preg_replace($pattern, $matches[1], $template);
|
||||
}
|
||||
} else {
|
||||
// Remove the content if array is not empty
|
||||
$template = preg_replace($pattern, '', $template);
|
||||
}
|
||||
} elseif ((is_string($value) && !empty($value)) || (is_bool($value) && $value === true)) {
|
||||
// Handle {{#key}}...{{/key}} blocks for truthy values
|
||||
$pattern = '/{{#' . preg_quote($key, '/') . '}}(.*?){{\/' . preg_quote($key, '/') . '}}/s';
|
||||
if (preg_match($pattern, $template, $matches)) {
|
||||
$replacement = $matches[1];
|
||||
|
||||
Reference in New Issue
Block a user