【发布时间】:2021-07-14 22:50:12
【问题描述】:
我的 PHP 脚本中有一个函数,它根据标头发送的区域设置加载正确的本地化文件。 在 Firefox 中,我的文本完美显示,但是当我切换到 Chrome 时,它只显示“注意:尝试访问第 46 行 [PATH]/index.php 中类型为 null 的值的数组偏移”
我加载语言的功能是这个:
<?php
/**
* This function loads the specified language by their locale identifier
*
* @param string $locale The locale identifier
* @param string $page Page to load data for
* @return array Returns array with data
*/
// Example Code snippet on how to use it:
/*
$locale = locale_accept_from_http($_SERVER["HTTP_ACCEPT_LANGUAGE"]); // Locale retrieved from Header
$language = loadLanguage($locale, "panel"); // Loads the language for the "panel" Page
*/
function loadLanguage($locale, $page){
$langlist = [
"de",
"en"
];
$lang_values = array_values($langlist);
if(in_array($locale, $lang_values)){
if(file_get_contents(dirname(__FILE__) . "/{$page}" . "/snippets_" . strtoupper($locale) . ".json")){
return json_decode(file_get_contents(dirname(__FILE__) . "/{$page}" . "/snippets_" . strtoupper($locale) . ".json"), true);
} else {
return json_decode(file_get_contents(dirname(__FILE__) . "/{$page}" . "/snippets_EN.json"), true);
}
}
}
?>
德语 (de) 和英语 (en) 的翻译文件存在于他们的文件夹中,所以我不太明白,为什么它不起作用。 Chrome 甚至会发送语言标头吗?
感谢您的帮助!
【问题讨论】:
标签: php google-chrome firefox localization