localize UIAlertView & offline notifications in project with no storyboard - ios

I'm working on a project that doesn't have any storyboards. So I can't use "base internationalization"
I've created localizable.string then turned on localization for each required languages.
Then tried doing
- UIAlertView * messageBox = [[UIAlertView alloc] initWithTitle:
NSLocalizedString(#"title", nil)
message: NSLocalizedString(#"msg", nil)
delegate: self
cancelButtonTitle: NSLocalizedString(#"ok", nil)
otherButtonTitles: nil];
And for notification i'm using nsdictionary and store notifications as:
- [alertMessages setObject:NSLocalizedString(#"test_notification", nil)
forKey:#"6"];
but it doesn't do any changes when changing language of the device. Is there any step i'm missing or if any better method is there? any working references would be greatly appreciated!

SOLUTION 1:
Do the following steps.
Clean Project
Delete App From Device/Simulator
If Simulator - Reset it.
Delete Derived Files (XCode > Window > Projects > Derived Data Delete)
Install App, and it will work.
I think its simply Cache Issue.
SOLUTION 2:
NSLocalizedString uses Localizable.strings file by default. Change your File.strings name and try again.
NSLocalizedStringFromTable(#"Message", #"File");

Related

IOS 8.1 localization not working,only show key not value

First of all,I did this follow this site http://www.raywenderlich.com/64401/internationalization-tutorial-for-ios-2014
I create a new Localizable.strings file,and localize it ,then add English and Japanese support.
I edit my Localizable.strings(en) file as follow:
"APPTITLE" = "Calibrate";
I edit my Localizable.strings(ja) file as follow:
"APPTITLE" = "こんにちわ";
At the source code,the early code :
self.title = #"PianoDisc Calibrate";
instead of :
self.title = NSLocalizedString(#"APPTITLE", nil);
Then I run it,But I get "APPTITLE" not "こんにちわ";
My environment is iPad mini,ios 8.1 system,Xcode 7 or 6.4. I'm also searched long time on the StackOverflow,like as NSLocalizedString only retrieves the key, not the value in Localizable.strings (IOS) ,I have tried the all answer,But it still not work.
There are some info,I use follow code,
NSString *path = [[NSBundle mainBundle] pathForResource:#"ja" ofType:#"lproj"];
NSLog(#"!!!!!!!!!!!!!!!!%#",path);
sometimes get null,sometimes get the true path.
What I'm missing? Thanks anyway!
I resolved it. Some guy remind that "check you haven't somehow messed up with the Build Rules (Xcode 4 -> select your target -> tab "Build Rules") There, check if you don't have too many "CopyStringsFile" rules that aren't using "CopyStringsFile". If so you can remove them all but one."
This step just resolve my problem,thanks god.

UIDocumentInteractionController presentOptionsMenuFromBarButtonItem error in ios8- Unknown activity items supplied

UIDocumentInteractionController presentOptionsMenuFromBarButtonItem gives me a console error in ios8 hardware (and not on 7.1 hardware or earlier):
Unknown activity items supplied: (
{
"com.adobe.pdf" = ;
},
""
)
In my official App Store version of my app, the app crashes at this point. When I compile and run on my iPad it just gives the error but does not crash.
My code:
In the .h:
UIDocumentInteractionController *docInteractionController;
In the .m:
self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
self.docInteractionController.delegate = self;
//UIBarButtonItem *element is an element in my toolbar
[self.docInteractionController presentOptionsMenuFromBarButtonItem:element animated:YES];
If I do a NSLog of docInteractionController.UTI I see "com.adobe.pdf" at the console, so the UTI is being recognized properly.
I can get around the "Unknown activity items" by using presentOpenInMenuFromBarButtonItem instead of presentOptionsMenuFromBarButtonItem for the UIDocumentInteractionController call, but I want to show the user the print and email options as well, not only the external app opening options.
Tested on iPad version 8.0.2. Xcode version 6.0.1, deployment target 6.0 (also tested with deployment target 8.0). All objective-c. Running on iPad version 7.1 does not produce the error.
See radar: http://openradar.appspot.com/radar?id=5800473659441152
As noted you can use presentOpenInMenu instead of presentOptionsMenu. You will loose the mail option but you can do it yourself with MFMailComposeViewController with a dedicated mail button.
Or use UIActivityViewController with an "Open In" activity item.
Or just a UIActivityViewController without an "Open In" activity item if that is enough
Or do presentOptionsMenu on iOS7 runtime and UIActivityViewController on iOS8+ runtime (where share extensions exist)
For people not wanting to use UIActivityViewController because the document controller is what we want (all actions show up): you can get around the crash by retaining the UIDocumentInteractionController, e.g. by assigning it to a property and releasing when the document interaction finishes:
- (void)share:(id)sender
{
self.documentInteraction = [UIDocumentInteractionController interactionControllerWithURL:_shareURL];
_documentInteraction.delegate = self;
_documentInteraction.name = self.title;
[_documentInteraction presentOptionsMenuFromBarButtonItem:_actionItem animated:YES];
}
- (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
{
if (controller == _documentInteraction) {
self.documentInteraction = nil;
}
}
The log will still show up, at least in debug, haven't checked with a release build. But it all works.
This solved the problem for me:
dispatch_async(dispatch_get_main_queue(), ^() {
[_docController presentOptionsMenuFromRect:button.bounds inView:button animated:YES];
});
I was getting the same console logs, but only in debug builds. Try creating a release build and you should see it still prints the "Unknown activity items supplied" error, but without the entire contents of the file.

How can I use OpenEars in a tweak?

I'm trying to include OpenEars in a theos project I'm making, a tweak for jailbroken iDevices, as I need speech recognition for my tweak. I was able to link the OpenEars framework by putting it in the same folder as my private frameworks, and I'm currently trying to get the tutorial code to work. Here's my current code:
#import <OpenEars/LanguageModelGenerator.h>
#import <OpenEars/PocketsphinxController.h>
#import <OpenEars/AcousticModel.h>
%hook SBLockScreenView
-(void)setCustomSlideToUnlockText:(id)arg1 {
LanguageModelGenerator *lmGenerator = [[LanguageModelGenerator alloc] init];
NSArray *words = [NSArray arrayWithObjects:#"WORD", #"STATEMENT", #"OTHER WORD", #"A PHRASE", nil];
NSString *name = #"NameIWantForMyLanguageModelFiles";
NSError *err = [lmGenerator generateLanguageModelFromArray:words withFilesNamed:name forAcousticModelAtPath:[AcousticModel pathToModel:#"AcousticModelEnglish"]];
//NSError* err = [lmGenerator generateLanguageModelFromArray:words withFilesNamed:name forAcousticModelAtPath:imagePath];
//NSError* err = [[NSError alloc] init];
NSDictionary *languageGeneratorResults = nil;
NSString *lmPath = nil;
NSString *dicPath = nil;
if([err code] == noErr) {
languageGeneratorResults = [err userInfo];
lmPath = [languageGeneratorResults objectForKey:#"LMPath"];
dicPath = [languageGeneratorResults objectForKey:#"DictionaryPath"];
}
else {
NSLog(#"Error: %#",[err localizedDescription]);
}
%orig;
}
%end
This compiles fine, but when it runs, I get these error messages and my device crashes: "While trying to reference the requested acoustic model bundle which is expected to be at the path (null), no bundle was found. This means that when the listening loop begins, it will crash due to the missing required resources. The problem finding the acoustic model bundle could be because the name of the bundle was not given to this method in a way it can use; for instance, if you are trying to use the English acoustic model and you have added that bundle to your app project, you would invoke this method by passing [AcousticModel pathToAcousticModel:#"AcousticModelEnglish"] (or [AcousticModel pathToAcousticModel:#"AcousticModelSpanish"] for the Spanish bundle), without appending ".bundle" to the end, and making sure that the bundle name is spelled exactly as it appears in the actual bundle name (the bundle can be seen in this distribution's folder "Framework".
If this doesn't fix the problem, it is very likely to be due to the fact that the acoustic model bundle wasn't imported successfully into the root level of your app project and its mainBundle. This usually happens either because the acoustic model bundle was never dragged into your app project when the "Framework" folder was originally supposed to be dragged in, or because it was dragged in but instead of using the setting "Create groups for any added folders" in Xcode's "Add Files" dialog, the option "Create folder references for any added folders" was unintentionally chosen. To fix this, just remove the acoustic model bundle or the "Framework" folder from your app and add it again to your app project with the correct setting of "Create groups for any added folders" in Xcode's "Add Files" dialog."
I also get these messages in my syslog:
May 27 00:54:49 Phillips-iPhone SpringBoard[17785] <Warning>: acousticModelPath is (null)
May 27 00:54:49 Phillips-iPhone SpringBoard[17785] <Warning>: Error: the default phonetic dictionary (null)/LanguageModelGeneratorLookupList.text can't be found in the app bundle but the app is attempting to access it, most likely there will be a crash now.
May 27 00:54:49 Phillips-iPhone SpringBoard[17785] <Warning>: Error while trying to load the pronunciation dictionary: Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x1883cbb0 {NSFilePath=(null)/LanguageModelGeneratorLookupList.text, NSUnderlyingError=0x1883cb40 "The operation couldn’t be completed. No such file or directory"}
May 27 00:54:49 Phillips-iPhone SpringBoard[17785] <Error>: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid domain=nil in -[NSError initWithDomain:code:userInfo:]'
Any help in getting this working is greatly appreciated. Thanks!
I recently made an OpenEars based iOS tweak and I went through the same problem. Because your tweak is not a normal app, the location that pathToAcousticModel gives will not be correct. The easiest solution is to place the acoustic model bundle in a known location and hard code it.
For example, if you are using theos you can place AcousticModelEnglish.bundle in the folder "layout/Library/OpenEars/". Then replace
[AcousticModel pathToAcousticModel:#"AcousticModelEnglish"]
with
#"/Library/OpenEars/AcousticModelEnglish.bundle"
The easiest solution that I found was dragging the "Framework" folder from the OpenEars library that contains the bundle files to the "Frameworks" folder inside the Xcode project. After dragging, mark the option "copy items if needed".
It sounds strange in the first look but this solution works and you won't change the code.
You will get a file structure like that:
You can also remove the oldest references of these libraries under the "Framework" folder to keep your project clean.
Cheers,
David

Issue with localization in iOS

I have been stuck in a mysterious problem related to localization in the last two days.
I am trying to add languages to my app, but it seems to me that something is going wrong.
To make things simple, I added the following lines to my current app, to see if the word test will translate or not :
- (void)viewDidLoad
{ [super viewDidLoad];
UILabel *lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(0,5,100,20)];
lbl1.textColor=[UIColor blackColor];
[self.view addSubview:lbl1];
lbl1.text= NSLocalizedString(#"test", nil);
}
After adding the language in the section of localization in "Project info" section, and checking "Use based internationalization". In addition to creating "Localizable.strings" file and clicking on Localize to choose the language, the word test does not translate.
BTW, here is Localizable.strings file :
"test" = "test translated";
I've done clean, and deleted the app form my phone and build it again, but nothing changed !
I took the same code, and done the same steps by creating a new project on xCode, surprisingly it translates !
Anyone knows why the translation does NOT happen in my original app ?
Firstly, Base Internationalization is for storyboards, it is not for NSLocalizedString.
Secondly, the file should be Localizable.strings, not Localization.strings.
It happpened to me recently. I did those things:
1) Deleted app from simulator/iPhone
2) Cleaned app (Product -> Clean)
3) Deleted derived data (Window -> Organizer -> Projects (tab) -> (Select own project from left) -> Click "Delete..." on right of Derived data)
It helped me

Using standard Apple translations for Alert button?

Is this true? When you instantiate a UIAlertButton, you have to pass it an explicit title for the Cancel button, like so:
UIAlertView *av =
[[UIAlertView alloc]
initWithTitle:#"Error"
message:err.localizedDescription
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
That means that if you want a localized app (which of course you do), you have to localize the Cancel string too, even though Apple has obviously got a canonical translation already. Am I really forced to write something like this to handle it (or is this even OK)?
NSBundle* uikitBundle = [NSBundle bundleForClass:[UIButton class]];
UIAlertView *av =
[[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Error", #"Title for Alert box when error occurs")
message:err.localizedDescription
delegate:nil
cancelButtonTitle:NSLocalizedStringFromTableInBundle(#"Cancel", #"Localizable", uikitBundle, nil)
otherButtonTitles:nil];
This looks horrible to me, but the idea that I have to maintain my own translations of words mandated by Apple's HIG (like "Cancel" or "OK") seems equally absurd.
As you expect, that's not recommended as your code introduces an undocumented, unsupported dependency which could break your app if a future iOS update comes along that changes how Apple localizes their UIButton (not very likely, but who knows).
Really, "OK" and "Cancel" are not difficult things to translate. If you don't wish a translator to re-localize these for you as part of your app's localization work then you could retrieve these yourself from iOS (using your code) and copy the translation to your .strings file, so that you'll have a reliable copy of the translation from now on!

Resources