2014-07-02 22 views
-1

是否有工具或任務運行器可以採用一種語言的HTML文檔,解析出一般/特定HTML標籤中的內容,通過Google翻譯運行該內容,然後將其放回新標記中的正確位置文件?基本上,消化一個源文件並輸出多種不同(非計算機)語言的變體。在HTML代碼中翻譯(人類)語言有哪些工具可用?

什麼,我希望的是:

index.html

<!DOCTYPE html> 
<html> 
<body> 
    привет мир! 
</body> 
</html> 

被編譯到:

en/index.html

<!DOCTYPE html> 
<html> 
<body> 
    Hello World! 
</body> 
</html> 

ru/index.html

<!DOCTYPE html> 
<html> 
<body> 
    привет мир! 
</body> 
</html> 

ch/index.html

<!DOCTYPE html> 
<html> 
<body> 
    你好世界! 
</body> 
</html> 

我當然不介意建立某種Gruntfile或不管這決定了語言,目的地等

回答

0

我能用Ruby和Grunt拼湊出一個解決方案。這可以被重構爲更強大,但它是一個工作的解決方案,所以我跑它。請記住,翻譯腳本會覆蓋的源文件。只有Grunt開始在新目的地重複複製。

使用easy_translate擦寶石,我寫這個劇本:

#!/usr/bin/ruby 

require 'easy_translate' 

EasyTranslate.api_key = '' # Get from google 

target_path = '' # path to translate 

Dir.glob(target_path) do |item| 
    next if item == '.' or item == '..' or item == '.svn' 

    contents = "" 
    update = "" 

    File.open(item, "r") do |file| 
    contents += file.read 
    update += EasyTranslate.translate(contents, :from => :russian, :to => :en) 
    end 

    File.open(item, "w"){ } 

    File.open(item, "w") do |file| 
    file.write(update) 
    end 
end 

通過這次行走,在target_path每一個文件,如果它是我們做出的變量contents值得的項目和update我們將使用放置在檢查文件內容的舊版本和新版本。然後我們打開文件並填寫這些變量。在第20行,我們清空文件,然後在第22-24行的最後一個塊中,我們將更新字符串寫入文件中。我使用File.open(item, "w") {}而不是.truncate(0),因爲我使用後一個選項將隨機Unicode字符添加到文件的內容中。再一次地,在翻譯過程中覆蓋文件並不理想。在新的目的地製作一個副本會更好,但我沒有那樣做。

Ruby腳本返回了一行縮小的HTML並打破了Smarty模板,因此我使用grunt-prettifygrunt-text-replace來優化HTML以便於使用,並使Smarty可以再次使用。 Gruntfile看起來像這樣:

module.exports = function(grunt) { 

    grunt.initConfig({ 
    pkg: grunt.file.readJSON('package.json'), 

    'prettify': { 
     options: { 
     indent: 4, 
     indent_char: ' ', 
     wrap_line_length: 0, 
     brace_style: 'expand', 
     "unformatted": [ 
      "a", 
      "code", 
      "pre" 
     ] 
     }, 
     all: { 
     expand: true, 
     cwd: 'static_ugly', 
     ext: '.html', 
     src: ['*.html'], 
     dest: 'static_pretty' 
     } 
    }, 

    'replace': { 
     fix_smarty: { 
     src: ['static_pretty/*.html'], 
     overwrite: true, 
     replacements: [{ 
      from: '{/ Strip', 
      to: '{/strip' 
     },{ 
      from: '{$ This-> setLayout ', 
      to: '{$this->setLayout' 
     },{ 
      from: '{$ this- > setPageTitle ', 
      to: '{$this->setPageTitle' 
     },{ 
      from: '$ this-> setPageTitle ', 
      to: '$this->setPageTitle' 
     },{ 
      from: '{$ smarty.block.footer ', 
      to: '{$smarty.block.footer' 
     }] 
     } 
    } 

    }); 

    grunt.loadNpmTasks('grunt-prettify'); 
    grunt.loadNpmTasks('grunt-text-replace'); 
    grunt.registerTask('default', ['prettify', 'replace']); 
}; 

有很多發現/替換的東西,我遺漏了,因爲它是項目特定的。這個解決方案几乎可以滿足原始問題的需求。爲了完成這個任務,我做了一些手動移動的文件,但我相信我會用這種前進的變化,並將其視爲一項正在進行的工作。