Skip to content

Instantly share code, notes, and snippets.

Created July 22, 2009 17:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nickjs/152167 to your computer and use it in GitHub Desktop.
Save nickjs/152167 to your computer and use it in GitHub Desktop.
CPLocalizedString for Cappuccino
Use these files in your app. Make a directory called en.lproj (and for each locale you wish to use). Use CPLocalizedString() in your code wherever you want a localized string.
Generating String Files
OS X Only
cd My\ App
genstrings -o en.lproj -s CPLocalizedString *.j
(Edit strings file in en.lproj/)
cd en.lproj
plutil -convert xml1 Localizable.strings -o Localizable.xstrings
(Add to allowed language file)
* AppKit
* Created by Nicholas Small.
* Copyright 2009, 280 North, Inc.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
@import <AppKit/CPApplication.j>
var StringTables;
function CPLocalizedString(key, comment)
return CPLocalizedStringFromTable(key, nil, comment);
function CPLocalizedStringFromTable(key, table, comment)
return CPLocalizedStringFromTableInBundle(key, table, [CPBundle mainBundle], comment);
function CPLocalizedStringFromTableInBundle(key, table, bundle, comment)
return [bundle localizedStringForKey:key value:comment table:table];
@implementation CPBundle (CPLocale)
- (CPString)bundleLocale
return [self objectForInfoDictionaryKey:@"CPBundleLocale"];
- (CPString)localizedStringForKey:(CPString)aKey value:(CPString)aValue table:(CPString)aTable
if (!StringTables)
StringTables = [CPDictionary dictionary];
if (!aTable)
aTable = "Localizable";
var table = [StringTables objectForKey:aTable];
if (!table)
table = [CPDictionary dictionary];
[StringTables setObject:table forKey:aTable];
var string = [table objectForKey:aKey];
if (!string)
string = aValue;
[table setObject:string forKey:aKey];
return string;
- (void)setDictionary:(CPDictionary)aDictionary forTable:(CPString)aTable
if (!StringTables)
StringTables = [CPDictionary dictionary];
[StringTables setObject:aDictionary forKey:aTable];
window.LocaleCPApplicationMain = CPApplicationMain;
CPApplicationMain = function(args, namedArgs)
var mainBundle = [CPBundle mainBundle],
bundleLocale = [mainBundle bundleLocale];
if (bundleLocale)
var request = [CPURLRequest requestWithURL:bundleLocale + ".lproj/Localizable.xstrings"],
response = [CPURLConnection sendSynchronousRequest:request returningResponse:response error:nil];
var plist = [CPPropertyListSerialization propertyListFromData:response format:nil errorDescription:nil];
[mainBundle setDictionary:plist forTable:@"Localizable"];
window.LocaleCPApplicationMain(args, namedArgs);
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
<string>My App</string>
* main.j
* Pixity - Web
* Created by Nicholas Small on January 31, 2009.
* Copyright 2009. All rights reserved.
@import <Foundation/Foundation.j>
@import <AppKit/AppKit.j>
@import ""
@import "AppController.j"
function main(args, namedArgs)
CPApplicationMain(args, namedArgs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment