Created
May 6, 2014 22:50
-
-
Save jbgutierrez/49125d1874494be66393 to your computer and use it in GitHub Desktop.
Shortcut to send translation tokens to an external properties file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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