Skip to content

Commit 275b1be

Browse files
committed
First release
0 parents  commit 275b1be

File tree

3 files changed

+493
-0
lines changed

3 files changed

+493
-0
lines changed

autoload/i18n_rails.vim

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
" ==============================================================
2+
" Description: Vim plugin for working with Rails I18n
3+
" Author: Alexander Skachko <[email protected]>
4+
" Homepage: https://github.com/lucerion/vim-i18n-rails
5+
" Version: 0.1.0 (2017-01-02)
6+
" Licence: BSD-3-Clause
7+
" ==============================================================
8+
9+
let s:positions = {
10+
\ 'top': 'leftabove split',
11+
\ 'bottom': 'rightbelow split',
12+
\ 'left': 'vertical leftabove split',
13+
\ 'right': 'vertical rightbelow split',
14+
\ 'tab': 'tab split'
15+
\ }
16+
let s:default_position = 'tab'
17+
let s:default_split = s:positions[s:default_position]
18+
19+
let s:errors = {
20+
\ 'dir': "Dir '{variable}' does not exists!",
21+
\ 'file': "File '{variable}' does not exists!",
22+
\ 'translation': "Translation '{variable}' not found!"
23+
\ }
24+
25+
func! i18n_rails#translation(selection_length)
26+
if !isdirectory(s:locale_directory())
27+
call s:error('dir', s:locale_directory()) | return
28+
endif
29+
30+
let l:locale_file = s:locale_file()
31+
if !filereadable(l:locale_file)
32+
call s:error('file', l:locale_file) | return
33+
endif
34+
35+
let l:locale_key = s:locale_key(a:selection_length, l:locale_file)
36+
let l:translation = s:translation(l:locale_file, l:locale_key)
37+
38+
if s:is_empty(l:translation)
39+
call s:error('translation', l:locale_key)
40+
else
41+
echo l:translation
42+
endif
43+
endfunc
44+
45+
func! i18n_rails#all_translations(selection_length)
46+
if !isdirectory(s:locale_directory())
47+
call s:error('dir', s:locale_directory()) | return
48+
endif
49+
50+
let l:locale_key = s:locale_key(a:selection_length)
51+
let l:translations = s:collect_translations(l:locale_key)
52+
53+
if s:is_empty(l:translations)
54+
call s:error('translation', l:locale_key)
55+
else
56+
call setqflist(l:translations)
57+
silent! exec 'copen'
58+
call s:apply_default_mappings()
59+
endif
60+
endfunc
61+
62+
func! i18n_rails#goto_definition(selection_length, ...)
63+
if !isdirectory(s:locale_directory())
64+
call s:error('dir', s:locale_directory()) | return
65+
endif
66+
67+
let l:locale_file = s:locale_file()
68+
if !filereadable(l:locale_file)
69+
call s:error('file', l:locale_file) | return
70+
endif
71+
72+
let l:locale_key = s:locale_key(a:selection_length, l:locale_file)
73+
let l:line_number = s:line_number(l:locale_file, l:locale_key)
74+
75+
if s:is_empty(l:line_number)
76+
call s:error('translation', l:locale_key)
77+
else
78+
call s:open_locale(l:locale_file, a:000)
79+
call s:goto_line(l:line_number)
80+
endif
81+
endfunc
82+
83+
func! s:translation(locale_file, locale_key)
84+
let l:path = split(a:locale_key, '\.')
85+
let l:translation = ''
86+
ruby << EOF
87+
require 'yaml'
88+
89+
locale_hash = YAML.load_file(VIM.evaluate('a:locale_file'))
90+
locale_path = VIM.evaluate('l:path')
91+
translation = locale_path.inject(locale_hash) do |hash, key|
92+
hash = hash.fetch(key, {})
93+
end
94+
if translation.is_a?(String)
95+
VIM.command("let l:translation = '#{translation}'")
96+
end
97+
EOF
98+
return l:translation
99+
endfunc
100+
101+
func! s:line_number(locale_file, locale_key)
102+
let l:path = split(a:locale_key, '\.')
103+
let l:lines = readfile(a:locale_file)
104+
let l:line_number = 0
105+
let l:current_line_number = 0
106+
107+
if s:is_empty(l:path)
108+
return
109+
endif
110+
111+
for l:line in l:lines
112+
let l:current_line_number += 1
113+
114+
if l:line =~ '^\s*' . l:path[0] . ':'
115+
let l:line_number = l:current_line_number
116+
let l:path = l:path[1:]
117+
endif
118+
119+
if s:is_empty(l:path)
120+
break
121+
endif
122+
endfor
123+
124+
if s:is_empty(l:path)
125+
return l:line_number
126+
endif
127+
endfunc
128+
129+
func! s:collect_translations(locale_key)
130+
let l:translations = []
131+
let l:locale_files = split(globpath(s:locale_directory(), '*'), '\n')
132+
133+
for l:locale_file in l:locale_files
134+
let l:locale_key = s:full_locale_key(l:locale_file, a:locale_key)
135+
let l:line_number = s:line_number(l:locale_file, l:locale_key)
136+
let l:translation = s:translation(l:locale_file, l:locale_key)
137+
138+
if !s:is_empty(l:translation)
139+
call add(l:translations, {
140+
\ 'filename': l:locale_file,
141+
\ 'lnum': l:line_number,
142+
\ 'text': l:translation
143+
\ })
144+
endif
145+
endfor
146+
147+
return l:translations
148+
endfunc
149+
150+
func! s:locale_directory()
151+
return split(s:current_path(), '/app')[0] . '/config/locales/'
152+
endfunc
153+
154+
func! s:locale_file()
155+
let l:locale_file = s:default_locale_file()
156+
157+
if s:is_empty(l:locale_file)
158+
let l:locale_file = s:ask_locale_file()
159+
endif
160+
161+
return l:locale_file
162+
endfunc
163+
164+
func! s:default_locale_file()
165+
if !s:is_empty(g:i18n_rails_default_locale_file)
166+
return s:locale_directory() . g:i18n_rails_default_locale_file
167+
endif
168+
endfunc
169+
170+
func! s:ask_locale_file()
171+
let l:locale_directory = s:locale_directory()
172+
let l:current_path = s:current_path()
173+
174+
exec 'lcd ' . l:locale_directory
175+
let l:locale_file = input('File: ', l:locale_directory, 'file')
176+
exec 'lcd ' . l:current_path
177+
redraw
178+
179+
return l:locale_file
180+
endfunc
181+
182+
func! s:locale_key(selection_length, ...)
183+
let l:locale_file = get(a:000, 0, '')
184+
185+
if s:is_empty(a:selection_length)
186+
if s:is_empty(l:locale_file)
187+
return s:ask_locale_key()
188+
else
189+
let l:locale = s:locale(l:locale_file) . '.'
190+
return s:ask_locale_key(l:locale)
191+
endif
192+
else
193+
if s:is_empty(l:locale_file)
194+
return s:selection()
195+
else
196+
return s:full_locale_key(l:locale_file, s:selection())
197+
endif
198+
end
199+
endfunc
200+
201+
func! s:ask_locale_key(...)
202+
let l:text = get(a:000, 0, '')
203+
let l:locale_key = input('Locale key: ', l:text)
204+
redraw
205+
return l:locale_key
206+
endfunc
207+
208+
func! s:full_locale_key(locale_file, locale_key)
209+
return s:locale(a:locale_file) . '.' . a:locale_key
210+
endfunc
211+
212+
func! s:selection()
213+
try
214+
let l:previous_register_value = @z
215+
normal! gv"zy
216+
return @z
217+
finally
218+
let @z = l:previous_register_value
219+
endtry
220+
endfunc
221+
222+
func! s:locale(locale_file)
223+
return fnamemodify(a:locale_file, ':t:r')
224+
endfunc
225+
226+
func! s:goto_line(line_number)
227+
call cursor(a:line_number, 0)
228+
normal! _
229+
silent! normal! zO
230+
endfunc
231+
232+
func! s:open_locale(locale_file, position)
233+
let l:position = empty(a:position) ? s:default_position : a:position[0]
234+
let l:split = get(s:positions, l:position, s:default_split)
235+
silent exec l:split . a:locale_file
236+
endfunc
237+
238+
func! s:current_path()
239+
return expand('%:p:h')
240+
endfunc
241+
242+
func! s:apply_default_mappings()
243+
if g:i18n_rails_use_default_mappings
244+
nnoremap <buffer> <silent> q :cclose<CR>
245+
for mapping in items(g:i18n_rails_mappings)
246+
exec 'nnoremap <buffer> <silent>' . ' ' .
247+
\ get(mapping, 0) . ' ' . get(mapping, 1)
248+
endfor
249+
250+
if g:i18n_rails_translations_autopreview
251+
nnoremap <buffer> <silent> j j<CR><C-W><C-W>
252+
nnoremap <buffer> <silent> k k<CR><C-W><C-W>
253+
endif
254+
end
255+
endfunc
256+
257+
func! s:is_empty(value)
258+
let l:value = a:value
259+
260+
if type(l:value) == type(1)
261+
return l:value <= 0
262+
endif
263+
264+
if type(l:value) == type('string')
265+
let l:value = substitute(l:value, '\s', '', 'g')
266+
let l:value = substitute(l:value, '\t', '', 'g')
267+
endif
268+
269+
return empty(l:value)
270+
endfunc
271+
272+
func! s:error(key, variable)
273+
let l:message = substitute(s:errors[a:key], '{variable}', a:variable, '')
274+
echohl ErrorMsg | echomsg l:message | echohl None
275+
endfunc

0 commit comments

Comments
 (0)