2012-10-30 119 views
0

我找到了this plugin並試圖讓它在PhoneGap 2.1上工作。我更新如下。 我把插件引用Cordova.plist並創建可本地化的字符串。Phonegap 2.1 iOS本地化

因爲這NSLog(jsString);代碼的打印正確的本地化字符串到控制檯

[6766:c07] <null>("Hello"); 

但我不能讓它在JS文件的工作。它不會給出任何錯誤,但也不起作用。即使我刪除result,警報框也不會彈出。

app.Localizer.get('HelloKey', 

         function(result) { 
            alert("We got a setting: " + result); 
            }); 

我的修改:

localizable.js 
************** 

function localizable() { 
} 

localizable.prototype.get = function(name, success) 
{ 

    Cordova.exec("localizable.get", name, success); 

}; 

Cordova.addConstructor(function() 
{ 
    if(!window.plugins) 
    { 
     window.plugins = {}; 
    } 
    window.plugins.localizable = new localizable(); 
}); 

-

localizable.h 
************* 

#import <Cordova/CDVPlugin.h> 
#import <Foundation/Foundation.h> 



@interface localizable : CDVPlugin {} 
- (void) get:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; 
@end 

-

localizable.m 
************* 

#import "localizable.h" 


@implementation localizable 
- (void)get:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options 
{ 
    NSUInteger argc = [arguments count]; 
    NSString* jsString; 

    if(argc == 2) 
    { 
     NSString *key = [arguments objectAtIndex:0]; 
     NSString *successCallback = [arguments objectAtIndex:1]; 

     NSString *returnVar = NSLocalizedString(key, nil); 

     jsString = [NSString stringWithFormat:@"%@(\"%@\");",successCallback,returnVar]; 

     NSLog(jsString); 

     [self writeJavascript:jsString]; //Write back to JS 
    } 

} 
@end 

回答

0

您正在使用舊的PhoneGap語法。雖然我不是說這是問題所在。但我做了以下,它爲我工作。也許你可以在下面試試。它與您的代碼基本相同,但語法不同。

的JavaScript

window.getString = function(str) { 
    cordova.exec(
       function(ans){gotString(ans);}, 
       function(err){},"Tools","getString",[str] 
       ); 
} 

function gotString(ans) { 
    navigator.notification.alert(ans); 
} 

function onDeviceReady() { 
    window.getString("invalid_msg"); 
} 

的Tools.h

#import<Foundation/Foundation.h> 
#import<Cordova/CDV.h> 

@interface Tools: CDVPlugin 

-(void) getString:(CDVInvokedUrlCommand*) command; 

Tools.m

#import "Tools.h" 
@implementation Tools 

-(void) getString:(CDVInvokedUrlCommand*) command { 
    NSString *string = [command.arguments objectAtIndex:0]; 
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:NSLocalizedString(string,nil)]; 
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 
} 

@end 

個Localizable.strings(EN)

"invalid_msg" = "It is not correct" 

Localizable.strings(DE)

"invalid_msg" = "Das ist falsch"