Skip to content

Instantly share code, notes, and snippets.

@maxhuk
Created April 9, 2013 10:22
Show Gist options
  • Save maxhuk/5344669 to your computer and use it in GitHub Desktop.
Save maxhuk/5344669 to your computer and use it in GitHub Desktop.
Useful localization macros. Fast localize for IB views and objects (text is replaced with NSLocalizedString(text, nil)).
//
// Localize.h
//
// Created by Maksym Huk on 3/31/13.
// Copyright (c) 2013 Maksym Huk. All rights reserved.
//
#import <Foundation/Foundation.h>
#define LS(...) NSLocalizedString((__VA_ARGS__), nil)
void LocalizeObject(id obj);
void LocalizeView(UIView *view);
void LocalizeViews(NSArray *views);
//
// Localize.m
//
// Created by Maksym Huk on 3/31/13.
// Copyright (c) 2013 Maksym Huk. All rights reserved.
//
#import "Localize.h"
void LocalizeObject(id obj)
{
if ([obj isKindOfClass:[UIView class]]) {
LocalizeView(obj);
}
else if ([obj isKindOfClass:[UINavigationItem class]]) {
UINavigationItem *item = (UINavigationItem *)obj;
item.title = LS(item.title);
}
}
void LocalizeView(UIView *genericView)
{
if ([genericView isKindOfClass:[UILabel class]]) {
UILabel *view = (UILabel *)genericView;
view.text = LS(view.text);
}
else if ([genericView isKindOfClass:[UIButton class]]) {
UIButton *view = (UIButton *)genericView;
[view setTitle:LS([view titleForState:UIControlStateNormal]) forState:UIControlStateNormal];
}
else if ([genericView isKindOfClass:[UISegmentedControl class]]) {
UISegmentedControl *view = (UISegmentedControl *)genericView;
for (NSUInteger i = 0; i < view.numberOfSegments; ++i) {
[view setTitle:LS([view titleForSegmentAtIndex:i]) forSegmentAtIndex:i];
}
}
}
void LocalizeViews(NSArray *views)
{
for (UIView *view in views) {
LocalizeView(view);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment