blog comments 0 del.icio.us bookmarks 0 diggs 0 Google results 0

6.0
PostRank

Ya Talkin' Gibberish

Err the Blog - Home From Err the Blog - Home, 1 year ago, 6 views

Oh man! Just tonight Beast Edge was graced with localization, courtesy of Gibberish. Relevant commits are here and here. There’s already some heated debate on the Mephisto mailing list regarding this change and possible future changes, if you’re into that sort of thing. Dig in.

Gibba-what-now?

Yeah, what? Gibberish is a simple Rails localization plugin we developed for FamUpdate and have been slipping into other projects. We’ve got Globalize and we’ve got Localize, but come on. They’re great, and useful, but sometimes you just want something light weight. I know I do.

I also know keying my translations by a string is not fun. What if I want to change a comma? Or add an exclamation point! Oh, use a symbol. But symbols make it hard to read my views. There must be a middle ground.

Of course: Gibberish.

Keyin’
<%= 'Description'[:description_title] %>

The above will return Description by default. If you’ve another language selected (besides English), it will find the description_title key in that language’s translation file and return the corresponding string instead. Simple.

You may leave the brackets blank, which will force Gibberish to assume the key is a lowercase, underscore’d version of the string.

So this:
"Header description"[]
Is the same as this:
"Header description"[:header_description]

Real smooth (thanks to technoweenie). Just, be careful. If you change the string too much you could land yourself in the same place we’re trying to escape from. (Trouble.)

klingon.yml

Translation files are simple YAML. For instance, es.yml:

welcome_friend: ¡Recepción, amigo!
welcome_user: ¡Recepción, {user}!
love_rails: Amo los carriles.

Something like “Welcome friend”[] will, when the language is set to :es, return ¡Recepción, amigo!. You can change the language quite easily:

>> Gibberish.current_language = :es
=> :es

It’s even got one of those trendy around_filters:

class ApplicationController < ActionController::Base
  around_filter :set_language

private
  def set_language
    Gibberish.use_language(session[:language]) { yield }
  end
end

No big deal.

Check the languages you’ve loaded with languages:

>> Gibberish.languages
=> [:es, :fr, :de, :kl]

In dev mode languages are automatically reloaded. From where? RAILS_ROOT + ’/langs’. Gibberish’ll look for .yml files in that directory.

The above example, we can safely assume, is run within a Rails app with lang/es.yml, lang/fr.yml, lang/de.yml, and lang/kl.yml. Add another language file and it’ll get picked up on the fly.

Intersomethinglation

The other cool thing Gibberish provides is interpolation. You may have noticed the {user} in welcome_user way above. Yeah, that’s gonna change.

Like this:
>> "Welcome, {user}"[:welcome_user, current_user.name]
=> "Welcome, Chris" 

The {user} bit will be replaced with current_user.name when being rendered. Works for translations, too. Naturally.

Curly brace’d strings are interpolated in the order arguments are passed. The fact that it says user is irrelevant. Really, it’s only to make things easy to remember. The important part is that it’s the first interpolation bullseye in the string.

Another example (ostensibly to drill home the point but realistically because me and PJ are on a Sandman kick):

>> "{name} is from {place}"[:hey_place, 'Chris', 'the Dreamworld']
=> "Chris is from the Dreamworld" 

Okay, you got the idea.

Grab it!

From the svn:

$ cd vendor/plugins
$ piston import svn://errtheblog.com/svn/plugins/gibberish

Follow trac: http://require.errtheblog.com/plugins/browser/gibberish

View the README: http://require.errtheblog.com/plugins/browser/gibberish

And, as always, report bugs to Lighthouse: http://err.lighthouseapp.com/projects/466-plugins

Enjoy ya jive talkin’ Gibberish. Patches gladly accepted.

comments

No comments yet.

You must be logged in to add your own comment.