【问题标题】:In Firefox my Text displays perfectly but Chrome says offset in PHP在 Firefox 中,我的文本显示完美,但 Chrome 在 PHP 中显示偏移
【发布时间】: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


    【解决方案1】:

    我的 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 种语言字符。

    请注意,对于给定的浏览器,完全有可能不会发送标头,因此您应该检查默认为一种或其他语言是否未找到匹配项。如果找不到,该函数返回 NULL。显然,您也可以返回另一个国家/地区。

    【讨论】:

    • 谢谢,我现在将 substr($locale, 0, 2) 添加到函数中,它现在可以在两种浏览器中使用:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    相关资源
    最近更新 更多