title: Approach for AngularJS template translations with Rails server
body: <p>I need to implement an application with multi-language support using an AngularJS front-end and a Ruby on Rails server.
  I am looking for a reasonable approach to render translated templates in multiple languages. I have come up with an approach I would like feedback on.</p>
  
  <p>In the Angular routes definitions, set the template property to an html partial that just has an ng-include with the src attribute value set by the controller. This approach is needed to dynamically modify the path to the template to be fetched from the server; it is described here: 
  <a href="https://stackoverflow.com/questions/11534710/angularjs-how-to-use-routeparams-in-generating-the-templateurl">AngularJS - How to use $routeParams in generating the templateUrl?</a></p>
  
  <p>So the Angular route config would look like:</p>
  
  <pre><code>angular.module('myApp', []).
      config(function ($routeProvider) {
          $routeProvider.when('/sites/new', {
              template: '&lt;div ng-include src="templateUrl"&gt;&lt;/div&gt;',
              controller: 'RouteController'
          });
      });
  </code></pre>
  
  <p>And the controller would look like:</p>
  
  <pre><code>// Set a prefix on the URL path, something like “es”
  function RouteController($scope, $routeParams) {
      $scope.templateUrl = $routeParams.locale + "/template/sites/site";
  }
  </code></pre>
  
  <p>Here <code>$routeParams.locale</code> is used to set the locale, but could be a variable set by user action. The approach for dynamically modifying the template URL path to add a locale prefix seems a bit convoluted, but I know of no other way.</p>
  
  <p>On the Rails side, in routes.rb, add a route:</p>
  
  <pre><code>match '/:locale/template/*template' =&gt; 'template#get'
  </code></pre>
  
  <p>The route uses route globbing so the <code>params[:template]</code> value can be a multi-level path.
  The <code>TemplateController#get</code> action just renders the partial determined by <code>params[:template]</code>
  The Template controller code is something like:</p>
  
  <pre><code>class TemplateController &lt; ApplicationController
      layout false
      caches_page :get
  
      def get
          render(template: "template/#{params[:template]}")
      end
  end
  </code></pre>
  
  <p>The Rails I18n support for translations is used in the erb templates, translating according to the locale parameter.
  In production, caching would be turned on. This would avoid incurring translation overhead. The locale prefix of the URL path would result in a per language set of translated templates to be cached.</p>
  
  <p>This approach pushes translations processing to the server side as much as possible.</p>
  
  <p>Is there any fundamental problems with this approach?</p>
  
  <p>Could it be done better?</p>