Skip to content

Instantly share code, notes, and snippets.

@igrep
Last active November 30, 2025 06:53
Show Gist options
  • Select an option

  • Save igrep/3618bcdf0294c81bda3481464be96bc8 to your computer and use it in GitHub Desktop.

Select an option

Save igrep/3618bcdf0294c81bda3481464be96bc8 to your computer and use it in GitHub Desktop.
MDNの翻訳を始めるときに便利なVim向け関数群
" # 使用方法
" :call BeginTranslation()
" :call OpenJaPath()
" :let commit = GetLatestSourceCommit()
"
" # 概要
"
" ## BeginTranslation
" MDNの翻訳に取りかかる際、現在開いている英語版のパスを日本語版のパスに変換して英語版をコピーし、開く
" その後Front-matterを編集し、title と slug 以外を削除し、l10n を追加することで、翻訳の準備を整える
" (初めてそのページを翻訳する時に使うので)日本語版のファイルはまだ存在しないことを前提としている
"
" ## OpenJaPath
" 現在開いている英語版のパスを日本語版のパスに変換して開く
"
" ## GetLatestSourceCommit
" 現在開いている英語版のファイルの最新のコミットハッシュを取得して返す
"
" # 使用時の前提
" - translated-contentとcontentが同じディレクトリーにある
" - カレントディレクトリーがその、translated-content・contentがあるディレクトリーである
" index.md があるディレクトリで Vim を起動してもいいが、 関数呼び出し前に :cd でカレントディレクトリーを変更すること
" - (部分的な再利用がしやすいよう)グローバルに各種関数を追加するため、名前が衝突しないこと
"
" # 参考
" https://mozilla-japan.github.io/mdn-translation-guide/translation/5_new-ja-file.html
" https://mozilla-japan.github.io/mdn-translation-guide/translation/6_edit-ja-file.html
function! ToJaPath(enPath) abort
" e.g. /path/to/content/files/en-us/glossary/boolean/html/index.md
return substitute(a:enPath, '/content/files/en-us', '/translated-content/files/ja', '')
endfunction
function! PrepareJaPath() abort
let path_to_index_md = ToJaPath(expand('%:p'))
call mkdir(fnamemodify(path_to_index_md, ':h'), "p")
return path_to_index_md
endfunction
function! OpenJaPath() abort
execute "edit " . PrepareJaPath()
endfunction
function! CopyToJaPath() abort
let path_to_index_md = PrepareJaPath()
execute "w " . path_to_index_md
execute "edit " . path_to_index_md
endfunction
function! BeginMdnTranslation() abort
cd content
let source_commit = s:doGetSourceCommit()
cd ..
call CopyToJaPath()
call InitializeFrontMatter(source_commit)
endfunction
function! GetLatestSourceCommit() abort
cd content
!git pull
let result = s:doGetSourceCommit()
cd ..
return result
endfunction
function! s:doGetSourceCommit() abort
return trim(system('git log -n1 --pretty="format:%H" -- ' . expand('%:p')))
endfunction
function! InitializeFrontMatter(source_commit) abort
" 先頭の行から「---」までを取得
1
silent /\v^---$/
let front_matter_end_line = line('.') - 1
" 先頭の行より次から title: と slug: の行以外を削除
execute 'silent! 2,' . front_matter_end_line . 'v/\v^(title:|slug:)/d'
" 再び「---」に移動し、該当の行より上に l18n を追加
1
silent /\v^---$/
call append(line(".") - 1, ["l10n:", " sourceCommit: " . a:source_commit])
w
endfunction
@hmatrjp
Copy link

hmatrjp commented Oct 22, 2025

少し変更したらLinux 上の Vim で動いたので、フォークしてそちらに変更を反映しました。

@igrep
Copy link
Author

igrep commented Nov 22, 2025

ありがとうございます!念のため OpenJaPath 関数だけ手元のWindowsで軽く動作確認し、ついでに新しい機能も加えつつマージしました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment