Search code examples
objective-cxcodecore-datanslocalizedstring

How to translate the default CoreData value


I set a default value of an attribute. This attribute is of type string. So when you create a new object and the content will auto fill the text appears: group name. I would be able to translate that "group name" in different languages​​. How can I do?


Solution

  • Instead of setting the default value for the string inside of the graphical data model, create a subclass of the NSManagedObject, then inside of the implementation file of the subclass use -(void)awakeFromInsert;.

    - (void)awakeFromInsert
    {
        [super awakeFromInsert];
        self.groupName = NSLocalizedString(@"Group Name", @"");
    }
    

    This method is called every time a new instance of that entity is inserted into the managed object context. Then in your localized strings file you will be able to translate that string into whatever languages you want.