在 Firefox 中,我的文本显示完美,但 Chrome 在 PHP 中显示偏移量
In Firefox my Text displays perfectly but Chrome says offset in PHP
我的 PHP 脚本中有一个函数可以根据 header 发送的语言环境加载正确的本地化文件。
在 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 甚至发送语言 header 吗?
谢谢你的帮助!
我的 Chrome 浏览器从英国
为 HTTP_ACCEPT_LANGUAGE 发送“en-GB,en-US;q=0.9,en;q=0.8”
当传入 locale_accept_from_http 时,您会得到“en_GB”.. 而不是“en”。您要么需要允许 GB 并且可能需要 en_US 等,或者可能将结果子串到第一个 2 个语言字符。
请注意,header 完全有可能不会发送给给定的浏览器,因此如果未找到匹配项,您应该检查默认为一种或其他语言。函数 returns 如果找不到则为 NULL。显然你也可以让另一个国家回归。
我的 PHP 脚本中有一个函数可以根据 header 发送的语言环境加载正确的本地化文件。 在 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 甚至发送语言 header 吗?
谢谢你的帮助!
我的 Chrome 浏览器从英国
为 HTTP_ACCEPT_LANGUAGE 发送“en-GB,en-US;q=0.9,en;q=0.8”当传入 locale_accept_from_http 时,您会得到“en_GB”.. 而不是“en”。您要么需要允许 GB 并且可能需要 en_US 等,或者可能将结果子串到第一个 2 个语言字符。
请注意,header 完全有可能不会发送给给定的浏览器,因此如果未找到匹配项,您应该检查默认为一种或其他语言。函数 returns 如果找不到则为 NULL。显然你也可以让另一个国家回归。