Adding a little bit of internationalization/i18n to your RubyMotion app an announcement by colinta
Guess what!? It's easy!
So easy, I'm not even gonna create a demo project for you. Just follow along, and you'll be fine!
Here's what you need to do basic string internationalization:
.lprojfolders in the resources/ folder
Localizable.stringsfiles in each folder
- Translate your strings
- Use SugarCube's
And here's what you need to do internationalization with images:
.lprojfolders in the
- Another version of the image you want i18n'ed
Our imaginary app will have a label that says "Hello".
@label.text = 'Hello'
It's an insanely popular app, and now that it's gone viral, we need to support users in other countries. We'll start with French and Japanese, because I don't have to go look up how to say "Hello" in those languages.
# French > mkdir resources/fr.lproj # Japanese > mkdir resources/ja.lproj
# French > touch resources/fr.lproj/Localizable.strings # Japanese > touch resources/ja.lproj/Localizable.strings
Translate your strings
You may have noticed that I didn't create a version of Localizable.strings for
English. I'm assuming here that you are going to use verbose strings, but if
you want to use short strings and then translate them into more verbose ones,
you can certainly do so - just add an
en.lproj folder and follow the same
principals that we'll do here.
"Hello" = "Bonjour";
"Hello" = "こんにちは";
@label.text = 'Hello'._
Yup! That's enough to have internationalized strings.
And images are easy
If you've got
hello.png / firstname.lastname@example.org in your
resources/ folder, add these
resources/fr.lproj/ and make them "Frenchy". Using
UIImage.imageNamed(filename) (or using SugarCube:
find these internationalized files. No code change necessary there.
And layouts are... uh, not as easy
Translation is the easy part. Much harder is designing layouts that take into
account the expectations of users in other countries. Classic iOS development
.xib files (placed in those
.lproj folders). If you're using
the ib gem, or something similar, you might be able to follow this practice
(I don't know, because I don't like
.xib files) .
If, instead, you're using teacup, you're basically on your own, but the
constraints/autolayout system can help a LOT. For instance, your labels are
going to be all sorts of different widths, and you might want to have some
alignments that are based on that width. Instead of calculating frames in your
viewDidLoad method, consider how you can establish relationships between the
UILabel frame and your other elements.
style :label, text: 'Hello'._ style :button, title: 'Hello'._, constraints: [ constrain(:left).equals(:label, :right).plus(4) constrain(:top).equals(:label, :top) ]
With these constraints, the button will be to the right of the label, no matter how wide the label is!