我找到了这个插件并试图让它适用于 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