Skip to content

Instantly share code, notes, and snippets.

@pavanjoshi914
Last active June 19, 2021 12:32
Show Gist options
  • Save pavanjoshi914/e45ca392b17de947a9154135787b492d to your computer and use it in GitHub Desktop.
Save pavanjoshi914/e45ca392b17de947a9154135787b492d to your computer and use it in GitHub Desktop.
I18n rules

Rules for designing directory structure for I18n

  • we support nested directory structure, all the yml files belonging to views should be stored in config/locales/views folder
  • Further nesting should be done following similar codebase structure.

Some of the cases that occur more frequently

  • case1:

If there is single file in module we dont include erb name for file. here we use view_folder_name: i.e about: as a top level namespace

For views/about/index.html.erb
for single template based modules we use only folder name as a namespace.

Right

en:
  about:
    all_keys present for about page
    
    
Wrong

 about:
  index:
    all_keys present for about page

  • Case 2:

If file with multiple ERB templates present (having lot of static strings). here we use View_folder_name + ERB template name as a top level name space


en:
 logix:
   teachers:
     all keys present for teachers page
     
   contribute:
      all keys present for contribute page
  • Case3:

Module with multiple erb files and most of the templates are route based like update, edit,delete (such modules usually posses less number of strings) Best example of such case is Announcement module

For views/announcements/_announcement.html.erb
we dont introduce a separate namespace for partial. keys present in them shall be named under announcements: namespace

Right

en:
  announcements:
    keys for partials

Wrong
en:
  announcement_partial:
    keys for partial

Some other situations to deal with

  • Keys which are being used throughout the app should be present in general namespace
create directory structure as config/locales/general
en:
  general:
    edit: "EDIT"
    delete: "DELETE"
  • Rails provides helpers for more automated translations for forms
for views/annnouncements/_form.html.erb

create directory structure config/locales/forms/announcements/en.yml

en:
  helpers:
    label:
      all labels translations
    submit:
      translations for submit button

we dont have to use any t() function in forms rails will automatically look up for translations when translations will be put in proper namespaces.

Rules for Naming of keys

  1. headings shall end with _heading suffix
  2. main paragraphs shall be named as main_description and submain para with ```submain_description
  3. eg of I18n in cards
teacher_card:
        heading: "I am a Teacher"
        item1_text: "Introduce the platform to your students"
        item2_text: "Promote the platform within your circles"
        item3_text: "Create exciting educational content using CircuitVerse"
  1. If string posses instance variables in it they shall be translated via string interpolation
t("hii_there",name: @name.name)

en:
  hii_there: "hii there %{name}"
  1. strings containing anchor tags or any html based things can be localized via adding _html suffix to key name Rails will automatically escape html and html based part will be interprated as html and not static string
t("hii_there_html")

en:
  hii_there_html: "some string with html"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment