符号 !{JSON.stringify(t("some.thing"))}; 是什么意思?意思是?

What does the notation !{JSON.stringify(t("some.thing"))}; mean?

我有一个 JS 代码,上面写着

!{JSON.stringify(t("some.thing"))};

它用于 translation/internationalization 和 i18next。但是我不明白 !{...} 部分。

我知道 JSON.stringify 的作用。我知道否定运算符 ! 是什么意思。我不理解它与 t() 函数的结合:当我在没有 !{...} 部分的情况下使用它时,它说

Uncaught ReferenceError: t is not defined

但是对于 !{...} 部分,它正确翻译了 some.thing 部分。

some.thing 是不同 JSON 文件中不同语言的键,例如一个 JSON 英文文件:

{
"some": {"thing": "something"}
}

和德语的 JSON 文件:

{
"some": {"thing": "irgendetwas"}
}

根据您计算机上设置的语言,函数t("some.thing")将return对应的值。如果您的计算机语言设置为英语,它将 return "something"。如果是德语,它将 return "irgendetwas".

JSON.stringify is a Javascript built in function to convert a JSON object into a string. The reverse operation would be JSON.parse().

在您的代码中,t('some.thing') 似乎是一个缩小函数,它 returns 一个对象,它被转换为 string,然后用否定运算符 !.

!not equal to

所以从技术上讲,这将转化为:

If the output of JSON.stringify(t("some.thing")) is empty or null. Return true

更多关于JavaScript Comparison and Logical Operators

大括号与宾语无关,感叹号与否定无关。 JavaScript:

中使用的变量的 jade 语法

http://naltatis.github.io/jade-syntax-docs/