Question

In config/locales/en.yml I have the following line:

en:
  hello: "Hello world"
  home: "Home"
  about: "About"

In a link I have this code:

<li id="about-link" data-hook><%= link_to Spree.t(:about), spree.root_path%></li>

When I start the server and go to the web page and hover on the about link I see the following alternative text:

translation missing en.spree.about

Why is the translation missing?

Was it helpful?

Solution

Your locale should look like this:

en:
  spree:
    hello: "Hello world"
    home: "Home"
    about: "About"

Since, spree has made their own internationalization setup with Spree scope for all I18n.t calls. So, above defined convention should work.

OTHER TIPS

A good thing to keep in your mind when you start adding locales to your .yml file is that when the app will grow big, you will have hundreds of lines in your locales and you need to organise them better so you don't overwrite them and get translation missing. You should use dictionary listing and take the advantage of Rails Lazy Lookup

Rails Lazy lookup

Imagine having a model product that has an attribute quantity that appears with different locales in several views in your app. So each time you have to write on your views

 t('product.quantity1') => to get: Quantity 
 t('product.quantity2') => where the outputted text might be different.

By using the dot rule and a good dictionairy listing in your locales that use Controller names and actions you dont have to use the depth in your conventions. Therefore, if you write t('.quantity') in a view, Rails will look it up and bring you the appropriate one that corresponds to the view that the t was called.

You locales file should have the following structure:

en:
  products:
    index:
      quantity: 'Items:'   
    show:
      quantity: 'Quantity:'
    edit:
      quantity: 'Number of items:'

By using the above structure on your locales depending on the view that you are editing you can use the exact same t convention call that will bring different translations.

In detail:

t('.quantity') inside app/views/product/index.html.erb => Items:
t('.quantity') inside app/views/product/show.html.erb => Quantity:
t('.quantity') inside app/views/product/edit.html.erb => Number of items:

Therefore, I suggest you start organising better your dictionary listing in your locales so you wont end up calling t with a depth of 2 or more dots as this will end up being very boring. You will also you keep your views with short lines and cleaner. The choice is yours but rails makes it easy for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top