mul14
10/21/2015 - 4:53 AM

Laravel and JavaScript Localization - Example

Laravel and JavaScript Localization - Example

<?php

Route::get('/js/lang.js', function() {
  
  return response()->view('lang')->header('Content-type', 'application/javascript');
  
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home page</title>
</head>
<body>
    
    <script src="/js/lang.js"></script>
    <script>
      alert(lang.required);
    </script>
</body>
</html>
var lang = {
  required => "{{ trans('required') }}",
  email    => "{{ trans('email') }}",
};