You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Swal文本中添加引号而避免被替换为"?

解决模板中转义引号被渲染为"的问题

方案1:使用非转义输出语法

Laravel的{{ }}会自动转义所有HTML实体,导致你的\"被转换成"。如果要输出原始的转义引号,改用{!! !!}语法输出翻译内容,它会跳过自动转义步骤:

const sc = {...swalConfig.confirm}; 
sc.text = "{!! __('Taking the records from the table \"USERS\" to the \"ROLES\" table, without any additional action.') !!}";

⚠️ 注意:使用{!! !!}时必须确保翻译内容是安全的,不存在XSS注入风险,因为它会直接输出原始字符。

方案2:把带引号的内容移到语言文件中

更安全且规范的做法是,将包含引号的文本直接写入语言文件(比如resources/lang/en/xxx.php):

// 语言文件示例
return [
    'transfer_notice' => 'Taking the records from the table "USERS" to the "ROLES" table, without any additional action.'
];

然后在模板中直接调用翻译键,无需手动转义引号:

const sc = {...swalConfig.confirm}; 
sc.text = "{{ __('transfer_notice') }}";

这种方式避免了模板中的转义混乱,同时保持了翻译逻辑的集中管理。

方案3:用单引号包裹翻译字符串

如果不想修改语言文件,可以用单引号包裹__()函数的参数,这样内部的双引号无需转义:

const sc = {...swalConfig.confirm}; 
sc.text = "{{ __('Taking the records from the table "USERS" to the "ROLES" table, without any additional action.') }}";

此方法适用于翻译文本中仅包含双引号的场景,无需额外转义操作。

内容的提问来源于stack exchange,提问作者Marko Panushkovski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 09:25:43