Permalink
Browse files

init. implement opengoogletranslate#url()

  • Loading branch information...
0 parents commit 9b9bf79aa96f7c1eb87765d5d6af337a3ee8e2bc @haya14busa committed Nov 12, 2016
@@ -0,0 +1,7 @@
+let g:root = fnamemodify(expand('<sfile>'), ':h')
+let g:__vital_power_assert_config = {
+\ '__debug__': 1,
+\ '__pseudo_throw__': 0,
+\ '__max_length__': -1
+\ }
+
@@ -0,0 +1,35 @@
+let s:URI = vital#opengoogletranslate#import('Web.URI')
+
+" opengoogletranslate#url() returns URL of Google Translate with given input.
+" target_lang and from_lang args can be empty string.
+function! opengoogletranslate#url(input, target_lang, from_lang) abort
+ let to = a:target_lang ==# '' ? s:infer_target_lang() : a:target_lang
+ return s:gtl_url(a:input, to, a:from_lang)
+endfunction
+
+" s:gtl_url() returns URL of Google Translate with given input. from_lang arg
+" can be empty and converted to 'auto'.
+"
+" https://translate.google.com/#auto/{lang}/{input}
+" https://translate.google.com/#en/ja/input
+" e.g. https://translate.google.com/#auto/ja/input
+function! s:gtl_url(input, target_lang, from_lang) abort
+ let from = a:from_lang ==# '' ? 'auto' : a:from_lang
+ let i = s:URI.encode(a:input)
+ return printf('https://translate.google.com/#%s/%s/%s', from, a:target_lang, i)
+endfunction
+
+function! s:infer_target_lang() abort
+ return s:_infer_target_lang(v:lang)
+endfunction
+
+function! s:_infer_target_lang(lang) abort
+ if a:lang ==# 'C'
+ return 'en'
+ elseif len(a:lang) < 2
+ return 'en'
+ else
+ return a:lang[:1]
+ endif
+endfunction
+
@@ -0,0 +1,83 @@
+Describe opengoogletranslate
+ Before all
+ let file = g:root . '/autoload/opengoogletranslate.vim'
+ let S = vital#vital#import('Vim.ScriptLocal').sfuncs(file)
+ exe vital#vital#import('Vim.PowerAssert').define('PowerAssert')
+ End
+
+ Describe opengoogletranslate#url
+ It ok
+ let tests = [
+ \ {
+ \ 'from': 'en',
+ \ 'to': 'ja',
+ \ 'input': 'input',
+ \ 'want': 'https://translate.google.com/#en/ja/input',
+ \ },
+ \ {
+ \ 'from': '',
+ \ 'to': 'ja',
+ \ 'input': 'input',
+ \ 'want': 'https://translate.google.com/#auto/ja/input',
+ \ },
+ \ {
+ \ 'from': '',
+ \ 'to': '',
+ \ 'input': 'input',
+ \ 'want': printf('https://translate.google.com/#auto/%s/input', S.infer_target_lang()),
+ \ },
+ \ {
+ \ 'from': 'en',
+ \ 'to': 'ja',
+ \ 'input': "in put\n2nd",
+ \ 'want': 'https://translate.google.com/#en/ja/in%20put%0A2nd',
+ \ },
+ \ ]
+ for tt in tests
+ let [from, to, input] = [tt.from, tt.to, tt.input]
+ PowerAssert opengoogletranslate#url(input, to, from) ==# tt.want
+ endfor
+ End
+ End
+
+
+ Describe s:gtl_url
+ It ok
+ let tests = [
+ \ {
+ \ 'from': 'en',
+ \ 'to': 'ja',
+ \ 'input': 'input',
+ \ 'want': 'https://translate.google.com/#en/ja/input',
+ \ },
+ \ {
+ \ 'from': '',
+ \ 'to': 'ja',
+ \ 'input': 'input',
+ \ 'want': 'https://translate.google.com/#auto/ja/input',
+ \ },
+ \ ]
+ let l:S_gtl_url = S.gtl_url
+ for tt in tests
+ let [from, to, input] = [tt.from, tt.to, tt.input]
+ PowerAssert S_gtl_url(input, to, from) ==# tt.want
+ endfor
+ End
+ End
+
+ Describe s:_infer_target_lang
+ It ok
+ let tests = [
+ \ {'in': 'C', 'want': 'en'},
+ \ {'in': 'x', 'want': 'en'},
+ \ {'in': 'en_US.utf8', 'want': 'en'},
+ \ {'in': 'ja_JP.UTF-8', 'want': 'ja'},
+ \ ]
+ let l:S_infer_target_lang = S._infer_target_lang
+ for tt in tests
+ let [in, want] = [tt.in, tt.want]
+ PowerAssert l:S_infer_target_lang(in) ==# want
+ endfor
+ End
+ End
+End
@@ -0,0 +1,12 @@
+function! vital#of(name) abort
+ let files = globpath(&runtimepath, 'autoload/vital/' . a:name . '.vital', 1)
+ let file = split(files, "\n")
+ if empty(file)
+ throw 'vital: version file not found: ' . a:name
+ endif
+ let ver = readfile(file[0], 'b')
+ if empty(ver)
+ throw 'vital: invalid version file: ' . a:name
+ endif
+ return vital#_{substitute(ver[0], '\W', '', 'g')}#new()
+endfunction
@@ -0,0 +1,5 @@
+let s:_plugin_name = expand('<sfile>:t:r')
+
+function! vital#{s:_plugin_name}#new() abort
+ return vital#{s:_plugin_name[1:]}#new()
+endfunction
Oops, something went wrong.

0 comments on commit 9b9bf79

Please sign in to comment.