• Stars
    star
    120
  • Rank 295,983 (Top 6 %)
  • Language
    Objective-C
  • License
    Other
  • Created over 13 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automate the Localization/Translation of your XIB & interface without any additional code nor IBOutlet!

What's this

This class allows you to automate the internationalisation (i18n) of your interface (XIB files) without any additional code in your application. No more using multiple XIBs for various locales or having outlets just to translate the text of a label!

Simply use the keys of your Localizable.strings in the text of any element in your XIB (title of an UIButton, text of a UILabel, …), and it will be automatically translated on the fly at runtime!

  • You don't need to have one XIB file by language/locale any more!
  • You don't need to have an IBOutlet to all of your XIB objects just to translate their text manually by code with NSLocalizedString!

Note: strings starting with a digit won't be translated. This is a feature used to avoid useless translation of static numbers you put in your XIB (especially because you will probably change this "0.00" string in your XIB with some real value by code later)

Installation

To use this, simply add OHAutoNIBi18n.h and OHAutoNIBi18n.m in your project and… that's all! OHAutoNIBi18n will be loaded automatically at runtime and translate your XIB on the fly without any additional line of code.

If you also want to use the _T(), _Tf() and _Tfn() macros, you can also add and #import "OHL10nMacros.h".

Alternatively, you can install all of it using CocoaPods. Simply add pod "OHAutoNIBi18n" to your Podfile.

Debugging your unlocalized strings

You can #define OHAutoNIBi18n_DEBUG 1 if you want OHAutoNIBi18n to warn every time it encounters a string for which it does not find any translation in your Localizable.strings file. In that case, it will log a message in your console and add the $ character around the text in your XIB at runtime so you can easily see it.

Note that you can use strings starting with a "." in your XIB (like ".Lorem ipsum dolor sit amet" to avoid warnings on those strings when OHAutoNIBi18n_DEBUG is set. This is useful for strings you use only as "layout helpers" (dummy strings to help you see your label in your XIB, which is easier than an empty label to help you position it), like for a UILabel in a custom UITableViewCell for which you don't want those warnings, knowing that you will override its text by code anyway.

How it works

This class uses method swizzling to intercept the calls to awakeFromNib and automatically localize any object created/extracted from a XIB file by the runtime. The method swizzling is done automatically when your application is loaded in memory, so you don't even have to add code to install this: the only presence of the OHAutoNIBi18n.m file in your project makes everything magic!

Comparison with Xcode 4.5's "Base Localization"

Starting with Xcode 4.5, you can use the "Base Localisation" (see Apple Tutorial: Internationalize your application).

But this process requires that you create a *.strings file for each of your XIB files, with the same name as the XIB file, and that you rely on Interface Builder's "Object IDs" which are fare from descriptive and human-readable.

This can become quite a pain:

  • If you have a lot of XIB files, you will have to create as many .strings files, resulting in a lot of files
  • With Base Localization, your .strings files will use abstract Object IDs, which are far from convenient to identify objects of your XIB (not even mentioning when you need to provide those Localizable.strings files to the person you pay to translate your application, how will s/he understand how to match those IDs with the actual UI?)
  • This is also problematic for every generic term in your application that is used across multiple XIB, like some vocabulary or strings specific to your application domain and used in all of your XIB files. In that case, Base Localization requires that you repeat this term in every of your .strings files, whereas with OHAutoNIBi18n you can just translate it once in your Localizable.strings.

For all these reasons, Base Localization is just not such a great solution after all, whereas OHAutoNIBi18n does not have those quirks.

Using it in a framework

If you have a framework containing storyboards and XIBs that needs to be auto-localized using OHAutoNIBi18n, you can embed OHAutoNIBi18n in your framework, alongside your storyboards, XIBs, and Localizable.strings files.

In that special case, because your localized strings won't be located in the main bundle of your app using the framework, but will be in the Localizable.strings file inside your framework bundle, you'll have to tell OHAutoNIBi18n to use your framework bundle instead of the main bundle. To do that, you'll have to call the following method as early as possible in your framework's lifecycle, especially before any of your framework's storyboard or XIB is loaded:

// Objective-C
NSBundle* fmkBundle = [NSBundle bundleForClass:SomeClassOfYourFmk.class];
[OHAutoNIBi18n setLocalizationBundle:fmkBundle tableName:nil];
// Swift
let fmkBundle = Bundle(for:SomeClassOfYourFmk.self)
OHAutoNIBi18n.setLocalizationBundle(fmkBundle, tableName:nil)

Using SomeClassOfYourFmk as a class belonging to your framework to easily derive your framework bundle from it.

You can also set a different table to look up for localizations when auto-translating XIBs and Storyboards, providing a specific tableName here. If set to nil, it will use the default name Localizable instead.

License

This code is distributed under the MIT License.

More Repositories

1

OHHTTPStubs

Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!
Objective-C
5,039
star
2

Reusable

A Swift mixin for reusing views easily and in a type-safe way (UITableViewCells, UICollectionViewCells, custom UIViews, ViewControllers, Storyboards…)
Swift
2,999
star
3

OHAttributedLabel

UILabel that supports NSAttributedString
Objective-C
1,511
star
4

Dip

Simple Swift Dependency container. Use protocols to resolve your dependencies and avoid singletons / sharedInstances!
Swift
977
star
5

UIImage-Resize

Category to add some resizing methods to the UIImage class, to resize it to a given CGSize — or fit in a CGSize keeping aspect ratio
Objective-C
352
star
6

OHAttributedStringAdditions

NSAttributedString Additions that add convenience methods to manipulate attributed strings easily
Objective-C
107
star
7

SourceryTemplates

Some templates to use for Code Generation in Swift with http://github.com/krzysztofzablocki/Sourcery
HTML
99
star
8

OHGridView

View that display cells as a grid. Uses quite the same API as UITableView
Objective-C
71
star
9

OHPDFImage

A library to easily load PDF files as UIImages
Objective-C
56
star
10

Dip-UI

UI Extensions for https://github.com/AliSoftware/Dip
Swift
48
star
11

OHSwipeWheel

A widget to choose between multiple values by swiping an horizontal wheel. This is kinda like a UIPickerView but rolling horizontally and that takes much less space
Objective-C
42
star
12

pprof

Ruby Gem to list, filter, search and print Provisioning Profiles files
Ruby
39
star
13

Xcode-Utils

A set of useful templates, macros, etc. to use with the Apple Developer Tools
Objective-C
32
star
14

OpeningHours

A small iOS app to keep the list of Opening Hours for your local shops you're used to go to
Swift
27
star
15

OHActionSheet

UIActionSheet subclass that uses blocks to handle its callback (which make the code much more easier and readable)
Objective-C
27
star
16

OHStackView

This class automatically stack its subviews and relayout them when one of them change its size.
Objective-C
25
star
17

OHAlertView-OHActionSheet

This repo is obsolete. Use OHAlertView and OHActionSheet repos now.
Objective-C
21
star
18

OHURLLoader

Class that uses blocks (new to iOS4/OSX 10.6) to make URL requests/downloads much more easier
Objective-C
19
star
19

OHAlertView

UIAlertView subclass that uses blocks to handle its callback (which make the code much more easier and readable)
Objective-C
16
star
20

AliJSONRPC

A JSON-RPC Framework for Cocoa. Supports JSON-RPC 1.0, 1.1 and 2.0
Objective-C
13
star
21

alisoftware.github.io

AliSoftware's Blog "Crunchy Development": making Swift Magic ✨
SCSS
12
star
22

generate-enum-allvalues

Automatically generate a `static let allValues` for your Swift enums
Swift
8
star
23

SWPromisesDemo

A demo project implementing the StarWars API (swapi.co) in Swift with PromiseKit
Swift
8
star
24

FunctionalVCDemo

Demo for the Functional ViewControllers concept in RxSwift
Swift
8
star
25

MagicSwiftNoStrings

Demo project demonstrate some nice patterns to get rid of String-based API using enums, mixins and SwiftGen
Swift
8
star
26

CodeGen-Workshop

This is the repository used for my SwiftAveiro'19 Workshop about Code Generation
Swift
6
star
27

FormWorkflow

A sample project to demonstrate how to use Promises to describe a workflow of Screens/ViewControllers
Swift
6
star
28

ObjcSwitch

A category to allow you to use the "switch/case"-like syntax with NSObjects (and not just integers/enums!)
Objective-C
6
star
29

swift-syntax-linter-demo

A demo project on how to use SwiftSyntax to implement a simple linter detecting specific call sites of a function in your source code
Swift
6
star
30

KeyPathObserver

Execute blocks when a given property changes (this is KVO, the block-style way)
Objective-C
5
star
31

talks

My talks at conferences
5
star
32

CodeGenDemo

Demo project to show the advantages of Code Generation with SwiftGen & Sourcery
Swift
4
star
33

AliSoftware

3
star
34

SwiftDependencyInjectionTest

A test project to play around and explore some Dependency Injection ideas in Swift — This was the POC that led to Dip
Swift
3
star
35

banana-crumb-muffins

This is the recipe for Banana Crumb Muffins, presented as a GitHub repository (one commit for each step of the recipe). Just for fun.
2
star
36

TVShowOrganizer

Ruby
2
star
37

XCAssetsSample

This sample project try to expose a complex case of using multiple assets with multiple targets. Originally created to test CocoaPods/CocoaPods#3263
Objective-C
2
star
38

xcodeplugin

Tools to generate dvtplugin / xcodeplugins especially for PLIST-files structure definitions
Shell
2
star