我想從PHP文件中分離js代碼,並且我將轉換變量從php轉換爲js時出現問題。 CakePhp使用__('text {VAR}', [VAR])
進行翻譯;CakePhp將變量作爲JS變量
這裏是一個PHP文件
$orders = [1=>...,2=>...., 3=>..., 4=>...];
<script>
var allOrders = <?= json_encode($orders ?? null) ?>;
var text_ok = '<?= __('OK') ?>';
.
.
.
var text_doYouWantToDeleteOrder = '<?= __('Do you really want to delete house No. {0}?', [json_encode($order->id)]); ?>';
</script>
結束代碼和我的外部文件(僅JS):
<script type="text/javascript">
var i = 0;
$.each(allOrders, function (index, order) {
$('#delete-order-' + order.id).click(function() {
swal({
title: text_doYouWantToDeleteOrder,
...
closeOnCancel: true
}, function (isConfirm) {
if (isConfirm) {
...
}
});
});
i++;
});
所以問題是如何從第一個文件匹配翻譯到二得到:
你真的想刪除1號房嗎?
你真的想刪除5號房嗎?
你真的想刪除8號房嗎?
舊工作的代碼(PHP和JS混合)
<script type="text/javascript">
<?php
$i = 0;
foreach ($orders as $order) {
?>
$('#delete-order-<?= $order->id ?>').click(function() {
swal({
title: "<?= __('Do you really want to delete house No. {0}?', [$order->id]); ?>",
...
closeOnCancel: true
}, function (isConfirm) {
if (isConfirm) {
...
}
});
});
<?php
$i++;
}
?>