Code Monkey home page Code Monkey logo

slimkeyfy's Introduction

Slimkeyfy

Extract plain Strings from .slim views and Rails controllers to replace them with I18n's t() method. Keys with it's translations will be streamed to a YAML file.

Read more in this blog post: PhraseApp Blog: Make your Rails App localizable with Slimkeyfy

slimkeyfy app/views/users/show.html.slim en
# users/show.html.slim
h1 Hello World!
# converts to
h1= t('.hello_world')
# config/locales/users.en.yaml
# en:
#   user:
#     show:
#       hello_world: Hello World!

Install

With Ruby Gems

> gem install slimkeyfy

With Bundler

group :development do
  # Github
  gem 'slimkeyfy', github: 'https://github.com/phrase/slimkeyfy.git'
  # or with concrete version
  gem 'slimkeyfy', '~> 0.X'
end

Approach

Most of the time tools like this go for a 80/20 approach stating that 80% can be done right and 20% have to be done manually. Our approach is similar but a bit different. Translating and tagging your Rails app can be error prone. In order to reduce this Slimkeyfy streams in every line that matches the regular expression engine and prompts you to take an action. This guarantees a higher quality but also gets us closer to the 90%. It might take a little more time than full automation. The collected data is then processed into a YAML file. If you don't provide a YAML one will be created. All your processed views and resulting translations will be merged with the existing YAML.

Usage

slimkeyfy INPUT_FILENAME_OR_DIRECTORY LOCALE (e.g. en, fr) [YAML_FILE] [Options]
  • If you do not provide a YAML file - one will be created at configs/locales/view_folder_name.locale.yml
  • If you provide one make sure that the top level locale matches your provided locale
  • The YAML file will be loaded as a hash and deep_merged with the new found translations

Stream - walks through the given file/files and if a regex hits you will be prompted to apply (y)es, discard (n)o, tag (x) (comments suggestions above the processed line) or (a)bort (only aborts the current file).

without YAML file

slimkeyfy path/to/your/file.html.slim en

with given YAML file (has to be valid YAML! with the top level locale matching your provided locale)

slimkeyfy path/to/your/dir/ en path/to/your/en.yml

A Backup (.bak) of the old file will be created e.g. index.html.slim => index.html.slim.bak. Providing ---no-backup creates the backups temporarily and deletes them right after you apply your changes. (be careful!)

Example Usage

your_app_name/
  |- app/
    |- views/
      |- user/
        new.html.slim
        show.html.slim
      |- project/
        index.html.slim
        ...
    ...
  |- config/
    |- locales/
      en.yml
      ...
  ...

> pwd
../your_app_name/

> slimkeyfy app/views/users/ en
... choose your changes here (y/n/x/a)

> ls app/views/users/
    new.html.slim
    show.html.slim
    new.html.slim.bak
    show.html.slim.bak

> ls config/locales/
    user.en.yml
    en.yml

> cat ../users/new.html.slim.bak
  h1 Hello World!

> cat ../users/new.html.slim
  h1= t('.hello_world')

> cat config/locales/en.yml
  --
  en:
    keys..

> cat config/locales/users.en.yml
  ---
  en:
    users:
      new:
        hello_world: Hello World!
      show:
        ...

Suggested Workflow

As HTML is not 100% parsable there will be errors in the conversion. To minimize your error rate we suggest to approach each view or view_folder individually. The i18n-tasks gem helped a lot by finding errors. Always double check your views and make sure that everything went smoothly. Especially double check all your links. Here is an example workflow:

# 1. create a branch for a view folder
> git checkout -b users_localization

# 2. slimkeyfy the view folder you would like to tag
> slimkeyfy app/views/users/ en

# 3. go through all files and verify/add missing translations
# (check against the .bak files (use git diff))
> git diff app/views/users/views.html.slim

# 4. add your translations + keys to your locale file(s)
# 4.1 optional: Use the I18n-tasks gem to find missing translations

# 5. go through all your views and click through everything to actually "see" what changed

# 6. If everything is fine - clean up (remove .baks)
> rm app/views/users/*.bak
# Optional: remove temporary YAML files
> rm config/locales/users.en.yml

Testing

To test the application simply call rspec spec/.. from the root directory of slimkeyfy

bundle install
bundle exec rspec spec/

PhraseApp Integration

Now that you processed your views and moved the generated keys to your localization files it is quite easy to push it to PhraseApp. If you have not set up your account yet take a look here. Make sure that the latest gem of PhraseApp is installed by simply typing:

> gem install phrase

If you are already familiar with the PhraseApp gem you can upload your translation/localization files now. For more details on your setup have a look at our detailed guide.

Todo

  • a dry run option where you can see what will happen if you convert.
  • currently you are prompted for all hits - We would like to do 70-80% automatically and prompt for the 20-30% that cannot be decided upon.
  • adding / generalizing Regular Expressions for better hit rate

Closed Todos

  • YAML placeholders are supported - thx@joshblour

Issues

  • If you choose to take a lot of files at one time make sure to go through with it. It is not an issue to completely rerun everything (already translated strings are ignored) but should be avoided.
  • some hits will not be correct, some things that should be found are not and sometimes the regex engine won't work for mysterious reasons. Slimkeyfy is a helper, it does not provide full automation. It might make your work a little easier.

References

slimkeyfy's People

Contributors

docstun avatar kennell avatar matthieuprat avatar sacry-dyn avatar sbruhns avatar yonahforst avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.