Dr Nic

Post-Halloween RadRails trick – all TextMate snippets available

Textmate Snippets to Radrails

HTML snippets
48
Ruby snippets
199

I tease myself with things I can’t have – like watching TextMate demo videos. One wonderful video shows a test-driven design demonstration by James Edward Gray II where he used Textmate snippets to generate the various assert statements. For example:

ase => assert_equal(expected, actual)
asn => assert_nil(instance)
asm => assert_match(/expected_pattern/, actual_string)

I’d been using Corban Brook’s templates but they didn’t include any assert or migration templates. And I wanted them without having to spend $3049 on TextMate [1]

This made me cranky

Solution 1: Learn Emacs. Setting up Emacs with all the modules you’ll need is a non-trival exercise. That is, put aside a day or so and don’t expect to be finished when time runs out. Plus, a version of GNU Emacs for Windows hasn’t been released since 2005. Not a lot of OSS love there.

Solution 2: Offer to build Textmate for Windows. I’m sure I read once that the Textmate author was open to porting of Textmate to Windows. That must be the past, because their website claims very strongly that there will never be a port:

countless requests for both a Windows and Linux port, but TextMate remains exclusive for the Mac, and that is how we like it!

That’s just lazy if you ask me.

Solution 3: Port Textmate’s snippet libraries (bundles) over to RadRails. Textmate has a more powerful syntax for its snippets than RadRails has for its templates (a limitation inherited from Eclipse), but SUCCESS! On the 31st of October – the day when Textmate was lauded around the blogosphere for its Halloween theme, I ported all their snippets into RadRails. OoooOOOOHHH spooooky.

You can now access the latest and greatest in templates/snippets for Radrails, for Ruby/Rails and RHTML (or click on the yellow boxes at the top).

Installation

  1. Download the two template XML files.
  2. Open RadRails
  3. Go to Windows > Preferences > Ruby > Editor > Templates, to see the list of included templates
  4. Click on any template, and press Ctrl-A to select them all.
  5. Click on Remove, to delete them all
  6. Click Import…, select the file ruby-rr-templates.xml
  7. Change to Rails > Editiors > RHTML Editor > RHTML Templates
  8. Remove all the current templates
  9. Click Import…, select the file rhtml-rr-templates.xml
  10. Press Ok to close the dialog and you’re done

Too difficult? The next release of RadRails will include these new templates.

How to use templates/snippets?

You can peruse a textmate-snippets-cheat-sheet, but really, spend 20 minutes looking through the template lists in RadRails. The name of the template is the text you type into the editor to activate the template.

So, the name of assert_equals is ase. So, type ase and press CTRL-SPACE, and a list of options pops up. Press return on the selected option.

Want to see all available assert_* templates? Type as and CTRL-SPACE and there they are. Nifty.

Patching/Updating

The RadRails guys are going to host these templates to allow anonymous access to make submitting patches relatively easy (if you know how to wield SVN). In future perhaps there will be a nice website for collaboratively sharing and downloading updates to templates.

Templates for JavaScript and CSS

I’ve put in a request for adding template support for JS and CSS as I have ported the JavaScript+CSS bundles as well. We just need a UI to import them into RadRails.

[1] TextMate is 39 euros = $49 at the moment. Plus $3000 for a new Mac.

Translation of RSS feeds a failure (part 3)

My path to spying on the Japanese wouldn’t be complete without support for translating RSS feeds. The translation needs to be performed on the feed itself before it hits the feedreader (unless you have non-Ajaxy webreaders that you can translate).

Unfortunately, Google Translate seems to ignore RSS altogether, and just redirects you back to the original RSS feed.

For example, take sample feed: http://d.hatena.ne.jp/hanazukin/rss2

Press the Spy on Japanese bookmarklet (cheat: click here for translation) and you’ll see the original RSS feed in the lower frame. Its not that its been poorly translated – Google just redirects you back to the original page instead of translating it.

Two solutions:

  1. Use a HTML -> RSS feed converter and use the direct translation url (use translate_c? instead of translate? in the url)
  2. Google Translate should add RSS feed translation. Really it cannot be more difficult than HTML translation. Add some caching (which google loves to do anyway) so its not translating each time my feed reader polls the feed, and you’re off and running.
  3. NEW – create a translator proxy that accepts RSS feed urls, converts the feed to a primitive HTML format, translate on google, convert results back to RSS feed

I prefer #2. So, if any Googlers read this blog, or if any friends of Larry & Co. are reading this blog, or if anyone remotely interested in popping over to the Googleplex to pass on a message from me, could ask the Google Translate ppl to add RSS feed support, I’d be a very very happy man.

Foreign tourists to your websites (part 2)

As a follow-up to the recent “Spy on the Japanese” post, you might want a nice set of links for your visitors to translate your page sinto their language.

But, here are your requirements (I can read your mind, I’m a doctor):

  • You know what countries visit your site, so you want to configure it yourself.
  • Since it requires Javascript to be turned on for it to work, you don’t want the links to show if the visitor doesn’t.

Solution: create the links using Javascript.

To allow translations of your blogs/websites via Google Translation, add the following inline Javascript into the sidebar. It was designed for WordPress – you may need to twiddle with the DOM elements created and/or CSS if it looks bad on your site.

The flags can be found at: http://www.famfamfam.com/lab/icons/flags/, and I’ve assumed you’ll store the png images in the /images/flags/ folder. If you store them somewhere else, change the images variable at the top of the script.

So, paste this script into your sidebar and you are saying “Aliens Welcome Here” [1].

      <script>
        // Each lang in an array: [google lang code, flag name, language name]
        // Flags available at: http://www.famfamfam.com/lab/icons/flags/
        var to_langs  = [['ja', 'jp', 'Japanese'],
                         ['de', 'de', 'German'],
                         ['es', 'es', 'Spanish']];
        var from_lang = 'en';
        var images     = '/images/flags/';

        document.write("<li id='translation'></li>");
        var liDiv = document.getElementById('translation');
        var title = document.createElement("h2");
        title.appendChild(document.createTextNode("Translation"));
        liDiv.appendChild(title);
        var ulDiv = document.createElement("ul");
        liDiv.appendChild(ulDiv);
        for (var i=0; i < to_langs.length; i++) {
          var to_lang = to_langs[i][0];
          var flag    = to_langs[i][1];
          var name    = to_langs[i][2];
          liDiv = document.createElement('li');
          ulDiv.appendChild(liDiv);
          liDiv.innerHTML = "<a href='#' onclick=\"location.href=" +
            "'http://translate.google.com/translate?langpair=" +
            from_lang + "&" + to_lang + "&hl=" + to_lang +
            "&ie=UTF-8&oe=UTF-8&u='" +
            " + encodeURIComponent(location.href)\">" +
            "<img border=0 src='" + images + flag + ".png' /> " + name + "</a>";
        }
        var getme = "<a href='http://drnicwilliams.com/2006/08/30/foreign-tourists-to-your-websites-part-2/'>How to translate my site?</a>";
        var getme_li = document.createElement("li");
        getme_li.innerHTML = getme;
        ulDiv.appendChild(getme_li);
      </script>

[1] Americans mightn't know that when foreigners visitor your country, we are presented with paperwork to fillout that refers to us as Aliens. Perhaps your government never watches its own Hollywood movies, but this isn't the best phrase to use, I think. Not very welcoming.

[ANN] Spy on the Japanese Rubists

Their Ruby code is surrounded by a shrouded of Japanese symbols. You know there is gold in there, but its left to the reader to interpret the purpose of the article. Happy Japanese man? Cranky Japanese man?

The creator of Ruby is Japanese, the Rubist magazine is in Japanese, and a great many users of Ruby are Japanese, yet I can’t understand a word they are saying. That’s not their fault. It’s my French teacher’s fault.

Wonder no longer. Thanks to Google Translate’s Japanese to English translation and the Bookmarklet feature of Firefox (and other browsers?)

Drag this Bookmarklet into your links toolbar, and start spying on the Japanese.

Bookmarklet: Spy on Japanese

Want to spy on the Germans? Drag it again, and change the embedded url. Look for the langpair=ja&en and change ja to de. (Cheat: Spy on Germans)

Want to encourage our Japanese friends to spy on you? Perhaps add links on your pages for instant translations from English to Japanese (change ja -> en, and en -> ja), and change the hl=en to hl=ja.

<a href="#" onclick="location.href='http://translate.google.com/translate?langpair=en&ja&hl=ja&ie=UTF-8&oe=UTF-8&u=' + encodeURIComponent(location.href)">Translate into Japanese</a>

Test this here: