php - 拉维尔 5 : Why can't I use integers and floating point numbers in the language files?

标签 php laravel-5

假设我在我正在处理的 Laravel 5 项目中有这个语言文件 resources/lang/en/settings.php。这个文件看起来像这样:

<?php
    return [
        "key_1" => 50,
        "key_2" => "50",
    ];

现在,如果我想像这样获取 key_1 的值:

return trans("settings.key_1"); // returns "settings.key_1"

这将返回 settings.key_1,它不是 50,这是我期望的值。另一方面,如果我尝试获取同样为 50 的 key_2 的值,但这次作为字符串,它将按预期返回 50

return trans("settings.key_2"); // returns 50

那么,为什么我不能在语言文件中使用数字,为什么值必须是字符串?

最佳答案

来自sourcecode: 让我们从您正在调用的 trans 函数开始。

/**
 * Get the translation for a given key.
 */
public function trans($id, array $parameters = [], $domain = 'messages', $locale = null)
{
    return $this->get($id, $parameters, $locale);
}

$this->get()调用的get函数

/**
 * Get the translation for the given key.
 */
public function get($key, array $replace = [], $locale = null, $fallback = true)
{
    list($namespace, $group, $item) = $this->parseKey($key);
    // Here we will get the locale that should be used for the language line. If one
    // was not passed, we will use the default locales which was given to us when
    // the translator was instantiated. Then, we can load the lines and return.
    $locales = $fallback ? $this->parseLocale($locale) : [$locale ?: $this->locale];
    foreach ($locales as $locale) {
        $this->load($namespace, $group, $locale);
        $line = $this->getLine(
            $namespace, $group, $locale, $item, $replace
        );
        if (! is_null($line)) {
            break;
        }
    }
    // If the line doesn't exist, we will return back the key which was requested as
    // that will be quick to spot in the UI if language keys are wrong or missing
    // from the application's language files. Otherwise we can return the line.
    if (! isset($line)) {
        return $key;
    }
    return $line;
}

如你所见:

    // If the line doesn't exist, we will return back the key which was requested as
    // that will be quick to spot in the UI if language keys are wrong or missing
    // from the application's language files. Otherwise we can return the line.
    if (! isset($line)) {
        return $key;
    }

该值不是有效值,因此未传递 isset,因此它将返回 $key 值,这是您请求的键。

为了更进一步,我们可以查看以下在 get 函数中调用的函数。

/**
 * Retrieve a language line out the loaded array.
 */
protected function getLine($namespace, $group, $locale, $item, array $replace)
{
    $line = Arr::get($this->loaded[$namespace][$group][$locale], $item);
    if (is_string($line)) {
        return $this->makeReplacements($line, $replace);
    } elseif (is_array($line) && count($line) > 0) {
        return $line;
    }
}

这里我们看到以下内容:

    if (is_string($line)) {

这是框架实际检查值是否为字符串的地方。

关于php - 拉维尔 5 : Why can't I use integers and floating point numbers in the language files?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37634841/

相关文章:

javascript - 将 Opencart 登录检查/强制插入 JS 函数

php - MySQL事务: queries committed anyway

mysql - 如何使用 laravel 通过查询将 2 个表与组连接

php - Laravel Spark 无法正确引入 css

php - 如果不存在相关模型,Laravel 只会删除模型

php - 以 HTML 表单提交的数组的预期顺序是什么?

php - gzip压缩不适用于xampp

php - 如何使用 CakePhp 克隆/复制一条 sql 记录?

laravel-5 - 升级后的 Laravel 5.6 项目有旧的 Bootstrap

css - Laravel 5 在哪里放置以及如何在 css 中访问图像