0

我找到了这个插件并试图让它适用于 PhoneGap 2.1。我像下面这样更新了它。我将插件引用放在 Cordova.plist 中并创建了可本地化的字符串。

因此,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
4

1 回答 1

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");
}

工具.h

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

@interface Tools: CDVPlugin

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

工具.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 (zh)

"invalid_msg" = "It is not correct"

Localizable.strings (de)

"invalid_msg" = "Das ist falsch"
于 2013-02-15T03:20:11.193 回答