public
Last active

CPLocalizedString for Cappuccino

  • Download Gist
CPBundle.sj
Objective-J
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
/*
* 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);
}
Info.plist
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?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>
README
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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)
main.j
Objective-J
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
* 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);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.