Skip to content

Latest commit

 

History

History
96 lines (69 loc) · 2.33 KB

README.md

File metadata and controls

96 lines (69 loc) · 2.33 KB

vim-i18n

Automated translation of Ruby/Rails projects

Introduction

vim-i18n helps you translate your Ruby/Rails project. It just exposes a single function, I18nTranslateString. This function takes the current visual selection, converts it into a I18n.t() call, and adds the proper key in a specified YAML store.

Examples

Extracting translations in .html.erb files

# app/views/users/show.html.erb
<dt>Name</dt>
    ^^^^
    -> Visual select and `:call I18nTranslateString()`

You will be asked for a key. In keeping with Rails translation syntax, if the key begins with . it will be considered a relative key:

# app/views/users/show.html.erb
<dt><%= t('.name') %>

# config/locales/en.yml

en:
  users:
    show:
      name: Name

Extracting translations in .rb files

Say you have the following line in your codebase:

# app/controllers/static_controller.rb
@some_text = "Hello, %{name}!"
             ^^^^^^^^^^^^^^^^^
             -> Visual select this text and `:call I18nTranslateString()`

The plugin will first ask you for the I18n key to use (ie. homepage.greeting). Then, if still not specified, the plugin will ask you the location of the YAML store (ie. config/locales/en.yml).

At this point, the plugin will replace the selection, and add the string to the YAML store:

# app/controllers/static_controller.rb
@some_text = I18n.t('homepage.greeting', name: '')
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
             -> BOOM!

# config/locales/en.yml
---
en:
  homepage:
    title: "Hello, %{name}!"

Note that the extracted translation included the appropriate interpolation.

Displaying translation for the key

Let say you have the following key within view / model / controller:

# app/controllers/static_controller.rb
@some_text = I18n.t('homepage.greeting', name: '')
                     ^^^^^^^^^^^^^^^^^

After selecting and executing I18nDisplayTranslation(), the plugin will return you value for the translation.

Vim mapping

Add this line or a simliar one to your ~.vimrc:

vmap <Leader>z :call I18nTranslateString()<CR>
vmap <Leader>dt :call I18nDisplayTranslation()<CR>

Installation

Install via pathogen.vim, simply copy and paste:

cd ~/.vim/bundle
git clone git://github.com/stefanoverna/vim-i18n.git