Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## init
- Merb::BootLoader.after_app_loads do
- Translation.init("pl") if defined?(Translation)
- ## gugl
- require "net/http"
- require "iconv"
- class GoogleTranslate
- def initialize(from = nil, to = nil, enc = nil)
- @from, @to, @enc = from, to, enc
- @http = Net::HTTP.new('translate.google.com')
- $KCODE = 'utf8'
- end
- def translate(text)
- body = @http.get("/translate_a/t?client=t&text=#{text.gsub(" ", "%20").gsub(/.+?\|/, "")}&sl=#{@from}&tl=#{@to}").body
- str = Iconv.iconv("utf8", @enc, body).first[/"(.+)"/, 1].gsub("% s", " %s")
- Merb.logger.my_debug "#{text} => #{str}"
- str
- end
- end
- ## trans
- class Translation
- include DataMapper::Resource
- @@cache = {}
- @@language = "pl"
- property :id, String, :key => true
- property :value, String
- property :language, String, :key => true
- def hash
- Merb::Request.escape([Marshal.dump(id)].pack("m"))
- end
- after :save do
- Translation.update_cache
- end
- after :destroy do
- Translation.update_cache
- end
- def related
- @related ||= begin
- parts = id.split
- cond = ["(" + (["id ILIKE ?"] * parts.size).join(" OR ") + ")"] + parts.map{|e| "%#{e}%"}
- Translation.all(:conditions => cond, :language => language, :id.not => self.key)
- end
- end
- def related=(list)
- list.each_pair do |hash, params|
- Translation.get_by_hash_and_language(hash, self.language).update_attributes(params)
- end
- end
- class << self
- def blank
- Translation.all(:value => "")
- end
- def languages
- ["pl", "de"]
- end
- def get_by_hash_and_language(hash, language)
- Translation.get(Marshal.load(Merb::Request.unescape(hash).unpack("m").first), language)
- end
- def init(language)
- @@language = language
- update_cache
- end
- def translate(id)
- '{%s}' % if @@cache.has_key?(id)
- @@cache[id].blank? ? id : @@cache[id]
- else
- Translation.create(:id => id, :value => "", :language => @@language)
- @@cache[id] = ""
- id
- end
- end
- def update_cache
- Translation.all(:language => @@language).each {|t| @@cache[t.id] = t.value }
- end
- end
- end
- ## la la
- class Translations < Application
- # provides :xml, :yaml, :js
- def gtranslate(language)
- g = GoogleTranslate.new("en", "pl", "latin2")
- trans = Translation.all(:language => language, :value => "")
- trans.each do |t|
- t.value = g.translate(t.id)
- t.save
- end
- redirect resource(:translations, :language => language), :message => "%s phrases translated".t(trans.size)
- end
- def index
- opts = {}
- opts[:language] = params[:language] unless params[:language].blank?
- @translations = Translation.all(opts)
- display @translations
- end
- def edit(hash, language)
- only_provides :html
- @translation = Translation.get_by_hash_and_language(hash, language)
- raise NotFound unless @translation
- display @translation
- end
- def update(translation)
- @translation = Translation.get_by_hash_and_language(params[:hash], translation[:language])
- raise NotFound unless @translation
- if @translation.update_attributes(params[:translation])
- redirect resource(:translations), :message => "Translation(s) was successfully updated".t
- else
- display @translation, :edit
- end
- end
- def destroy(hash, language)
- @translation = Translation.get_by_hash_and_language(hash, language)
- raise NotFound unless @translation
- if @translation.destroy
- redirect resource(:translations), :message => "Translation deleted".t
- else
- raise InternalServerError
- end
- end
- end # Translations
Add Comment
Please, Sign In to add comment