Skip to content

Instantly share code, notes, and snippets.

@hmatrjp
Forked from igrep/translate-mdn-ja.vim
Last active November 22, 2025 07:48
Show Gist options
  • Select an option

  • Save hmatrjp/7857dc0ee4d1575d9c876e6a62504dd5 to your computer and use it in GitHub Desktop.

Select an option

Save hmatrjp/7857dc0ee4d1575d9c876e6a62504dd5 to your computer and use it in GitHub Desktop.
MDNの翻訳を始めるときに便利なVim向け関数群(NeoVim用からの移植)
" # 使用方法
" :call BeginTranslation()
" :call OpenJaPath()
"
" # 概要
"
" ## BeginTranslation
" MDNの翻訳に取りかかる際、現在開いている英語版のパスを日本語版のパスに変換して英語版をコピーし、開く
" その後Front-matterを編集し、title と slug 以外を削除し、l10n を追加することで、翻訳の準備を整える
"
" ## OpenJaPath
" 現在開いている英語版のパスを日本語版のパスに変換して開く
"
" # 使用時の前提
" - (初めてそのページを翻訳する時に使うので)日本語版のファイルはまだ存在しない
" - Linux の Vim でも実行できるようにする (TODO: 移植前環境の WindowsのNeovimでも実行できるとなおよい)
" - translated-contentとcontentが同じディレクトリーにあり、フルパスに空白文字などcmd.execのメタキャラクターを含まない
" - カレントディレクトリーがその、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. C:\path\to\content\files\en-us\glossary\boolean\html\index.md (Vim Script は Windows でも / をパス区切りと認識する可能性あり TODO: 要確認)
" 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'))
" TODO: fnamemodify() が Windows の NeoVim でも動くか?
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 = trim(system('git log -n1 --pretty="format:%H" -- ' . expand('%:p')))
cd ..
call CopyToJaPath()
call InitializeFrontMatter(source_commit)
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
Author

hmatrjp commented Nov 22, 2025

フォーク元に取り込まれました。

オリジナルからの変更は ...

フォーク元では、さらに新しい機能の追加も行われているようです。 ← この投稿時点で、Debian/GNU Linux の Vimで動作確認済み

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