Cri*_*ian 2 html php jquery laravel laravel-blade
我的 laravel 上有两个独立的文件,一个是 index.blade.php ,一个是 .js 文件。刀片上有一根绳子,像这样:
<p id="currentMessage" class="bold-700"></p>
Run Code Online (Sandbox Code Playgroud)
在我的 JS 中我有:
$(document).ready(function() {
$('#currentMessage').text("@lang('hello'));
});
Run Code Online (Sandbox Code Playgroud)
似乎它不会在运行时翻译,我无法注入文本并等待它被翻译,但是有什么方法可以在发送之前在 jquery 上翻译字符串或其他方法来解决这个问题?
您可以通过这种方式将blade 的翻译注入到javascript:
<script>
var translations = {
hello: "@lang('hello')",
goodbye: "@lang('goodbye')",
...
};
</script>
Run Code Online (Sandbox Code Playgroud)
然后在你的js文件中,
$(document).ready(function() {
$('#currentMessage').text(translations.hello);
});
Run Code Online (Sandbox Code Playgroud)