Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jasp/31133 to your computer and use it in GitHub Desktop.
Save jasp/31133 to your computer and use it in GitHub Desktop.
Simple textmate script to convert an inline string to a translation string for use in Rails 2.2 i18n.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>#!/usr/bin/ruby
$LOAD_PATH &lt;&lt; "#{ENV["TM_SUPPORT_PATH"]}/lib"
require 'ui'
require 'escape'
require 'exit_codes'
text = ENV['TM_SELECTED_TEXT'] || ENV['TM_CURRENT_WORD']
suggest = text.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
name = TextMate::UI.request_string(:string =&gt; suggest, :prompt =&gt; "Type variable name")
if name.nil? or name == ""
TextMate.exit_discard
else
translation = "#{name}: \"#{text}\""
IO.popen("pbcopy", "w") { |copier| copier.puts translation }
print "&lt;%=t '#{name}' %&gt;"
end</string>
<key>fallbackInput</key>
<string>word</string>
<key>input</key>
<string>selection</string>
<key>keyEquivalent</key>
<string>^T</string>
<key>name</key>
<string>Convert to translated with translation in clipboard</string>
<key>output</key>
<string>replaceSelectedText</string>
<key>scope</key>
<string>text.html.ruby</string>
<key>uuid</key>
<string>F79A298F-8E4F-4DEC-9D6D-F59F833977B3</string>
</dict>
</plist>
#!/usr/bin/ruby
$LOAD_PATH << "#{ENV["TM_SUPPORT_PATH"]}/lib"
require 'ui'
require 'escape'
require 'exit_codes'
# Work on either selected text or current word
text = ENV['TM_SELECTED_TEXT'] || ENV['TM_CURRENT_WORD']
suggest = text.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
# Get name of translation string
name = TextMate::UI.request_string(:string => suggest, :prompt => "Type variable name")
if name.nil? or name == ""
TextMate.exit_discard
else
translation = "#{name}: \"#{text}\""
# Copy string for en.yml to clipboard
IO.popen("pbcopy", "w") { |copier| copier.puts translation }
# Output with translation fetch
print "<%=t '#{name}' %>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment