Skip to content

Instantly share code, notes, and snippets.

@martinrehfeld
Forked from evadne/CPBundle.sj
Created January 15, 2010 10:32
Show Gist options
  • Save martinrehfeld/277951 to your computer and use it in GitHub Desktop.
Save martinrehfeld/277951 to your computer and use it in GitHub Desktop.
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)
/*
* CPBundle.sj
* 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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* 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];
}
@end
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" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CPApplicationDelegateClass</key>
<string>AppController</string>
<key>CPBundleLocale</key>
<string>en</string>
<key>CPBundleName</key>
<string>My App</string>
<key>CPPrincipalClass</key>
<string>CPApplication</string>
</dict>
</plist>
/*
* 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 "CPBundle.sj"
@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