0

I am working on localizing my app and adding NSLocalizedString() all over my code where the string will be shown on the screen. The question is i have a UIPickerview that shows 12 items and i would like to localize it and not add different language strings in core data. This is what i have now that returns the titles. How can i localize it?

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.items objectAtIndex:row];
}

Is this going to work?

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *localizedTitle = [NSString stringWithFormat:NSLocalizedString(@"%@", @"Item Name"),[self.items objectAtIndex:row]];
    return localizedTitle;
}

I should just have a list of all the items in the Localizable.strings file?

Thanks in advance!

Yan
  • 3,533
  • 4
  • 24
  • 45

2 Answers2

0

NSLocalizedString does not work with Core Data. NSLocalizedString is macro for getting key-value from Localized File.

So if you want to get correct word from Core Data, first you need to get current language that is being used in iOS. Like this:

NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];

Then you need to make NSFetchRequest that will return correct word for selected language. If your UI has a lot of labels, buttons, etc you will make a lot of fetches. Since NSFetchRequest should be done asynchronous it can look strange if there is no text in labels when view is first shown. So my question is are you sure you want to use Core Data for localization? You can always try to make fetch on main thread, but surely your UI will freeze.

Josip B.
  • 2,434
  • 1
  • 25
  • 30
  • Thanks for the reply! I don't want to use core data for localization. I would like to use only Localizable.strings to localize. These words will be used in a lot of different places in the app and would like to have it set once and not have different tables in core data for each language – Yan Jul 15 '13 at 20:52
  • Then the easiest way you can achieve it is to make keys in Localizable.strings to have order like this: picker_1_0 = "some_value"; picker_2_0 = "some_value", etc. Then when UIPickerView dataSource calls - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component method you use localization in this way NSLocalizedString("", [NSString stringWithFormat:@"picker_%d_%d", row, component]); Using it this way you will have keys ordered in way UIPickerView needs it. – Josip B. Jul 16 '13 at 04:58
  • Would that mean that all the pickers will have the same values which are taken from the strings file? – Yan Jul 18 '13 at 00:33
  • It depends on your implementation. You can "assign" some name to picker in Localizable.strings so if you have two picker in your UIViewController make strings for first picker with prefix "first" and second to have prefix "second". Then when your dataSource calls method that needs to return some string for row in component you first check is it called for first picker or second picker and you add prefix you need to add. – Josip B. Jul 18 '13 at 10:40
0

When you localize, any string the user sees, no matter what UI component its a part of, should be in Localizablestrings.

Every possible item in your picker should have an entry in your Localizable.strings. NSLocalizedString is just a macro for loading strings form your Localizable.strings.

When you are done, you just submit your .strings to SmoothLocalize.com or similar service and drag the result they give you back into your app.

jvnbt
  • 2,465
  • 4
  • 20
  • 23
  • How would i do that inside the strings file. If i just add "item1"="item language" in strings file that doesn't show in the picker – Yan Jul 18 '13 at 00:33
  • Are you loading the picker with an array somewhere? If so, are you loading with strings such as NSLocalizedString("string",nil)? – jvnbt Jul 19 '13 at 03:17