diff --git a/SketchI18N.sketchplugin/Contents/Sketch/app.js b/SketchI18N.sketchplugin/Contents/Sketch/app.js index f068354..00a991e 100644 --- a/SketchI18N.sketchplugin/Contents/Sketch/app.js +++ b/SketchI18N.sketchplugin/Contents/Sketch/app.js @@ -1,27 +1,39 @@ var onRun = function(context) { var manager, + languagePath, doc = context.document, command = context.command, identifier = command.identifier(), resourcesPath = command.pluginBundle().url().path(), - language = NSUserDefaults.standardUserDefaults().objectForKey("AppleLanguages").objectAtIndex(0), + language = NSUserDefaults.standardUserDefaults().objectForKey("AppleLanguages").objectAtIndex(0); + + for(;;){ languagePath = resourcesPath + "/Contents/Resources/i18n/" + language + ".json"; + if (NSFileManager.defaultManager().fileExistsAtPath(languagePath)) { + break; + } + else { + var index = language.lastIndexOf("-"); + if (index == -1) { + return; + } + language = language.substring(0, index); + } + } - if (NSFileManager.defaultManager().fileExistsAtPath(languagePath)) { + if (!NSClassFromString("SketchI18NPluginManager")) { var mocha = Mocha.sharedRuntime(); - i18n = [NSString stringWithContentsOfFile: languagePath encoding: NSUTF8StringEncoding error: nil]; + mocha.loadFrameworkWithName_inDirectory("SketchI18NPlugin", resourcesPath + "/Contents/Resources"); + manager = SketchI18NPluginManager.manager(); + var i18n = NSString.stringWithContentsOfFile_encoding_error(languagePath, NSUTF8StringEncoding, nil); + manager.loadStrings(i18n); + } else { + manager = SketchI18NPluginManager.manager(); + } - if (!NSClassFromString("SketchI18NPluginManager")) { - mocha.loadFrameworkWithName_inDirectory("SketchI18NPlugin", resourcesPath + "/Contents/Resources"); - manager = SketchI18NPluginManager.manager(); - manager.loadStrings(i18n); - } else { - manager = SketchI18NPluginManager.manager(); - } - if (identifier == "i18n-toggle-command") { - manager.toggle(); - } else { - manager.run(); - } + if (identifier == "i18n-toggle-command") { + manager.toggle(); + } else { + manager.run(); } }