Skip to content

Instantly share code, notes, and snippets.

@mlapshin
Created December 6, 2013 12:17
Show Gist options
  • Save mlapshin/7822848 to your computer and use it in GitHub Desktop.
Save mlapshin/7822848 to your computer and use it in GitHub Desktop.
(defun extract-translation (start end)
(interactive "r")
(if (region-active-p)
(let* ((text (buffer-substring start end))
(keyname (read-string "Key name: " (extract-translation-default-key-name text)))
(short-keyname (car (last (split-string keyname "\\.") 1)))
(rails-root (if (fboundp 'ffip-project-root)
(ffip-project-root)
(projectile-project-root)))
(locale-file (format "%sconfig/locales/en.yml" rails-root))
(command (format "yaml-add-key %s %s %s"
(shell-quote-argument keyname)
(shell-quote-argument text)
(shell-quote-argument locale-file))))
(shell-command-to-string command)
(save-excursion
(delete-region start end)
(goto-char start)
(insert (format "t('.%s')" short-keyname))))
(message "extract-translation: region is not active")))
(defun extract-translation-default-key-name (text)
(let ((filename-component (split-string
(replace-regexp-in-string ".*?views/\\(.*\\)?\\.html\\.haml\\'" "\\1" buffer-file-name)
"/"))
(text-component (extract-translation-string-to-keyname text)))
(mapconcat 'identity
(append '("en") filename-component (list text-component))
".")))
(defun extract-translation-string-to-keyname (str)
(replace-regexp-in-string
"\\(\\`_+\\|_+\\'\\)"
""
(replace-regexp-in-string
"_+"
"_"
(replace-regexp-in-string
"[^a-z0-9]"
"_"
(downcase str)))))
(global-set-key (kbd "C-c C-t") 'extract-translation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment