Skip to content

Instantly share code, notes, and snippets.

@jbgutierrez
Created May 6, 2014 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbgutierrez/49125d1874494be66393 to your computer and use it in GitHub Desktop.
Save jbgutierrez/49125d1874494be66393 to your computer and use it in GitHub Desktop.
Shortcut to send translation tokens to an external properties file
function! ExtractProperty()
let a_save = @a
try
let properties_file = expand('%:r').'.properties'
if filereadable(properties_file)
normal! gv"ay
let value = @a
let key = substitute(value, ".*", "\\U\\0", "")
let key = substitute(key, '\W\+', "_", "g")
if len(key) > 20
let hash = system('md5 -qs '.key)
let key = key[0:20-5]."_".hash[0:5]
endif
let key = input("Key name: ", key)
let replacement = "<%- t('" . key . "') %>"
let language = expand('%:r:e')
let new_line = [ key.'='.value ]
let @a = replacement
normal gv
normal "_x"aP
let lines=readfile(properties_file, 'b')
if !empty(lines) && empty(lines[-1])
call remove(lines, -1)
endif
call writefile(lines+new_line, properties_file, 'b')
endif
finally
let @a = a_save
endtry
endfunction
vnoremap <leader>t :call ExtractProperty()<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment