Skip to content

Instantly share code, notes, and snippets.

@j4johnfox
Forked from nickjs/CPBundle.sj
Created July 7, 2010 04:22
Show Gist options
  • Save j4johnfox/466301 to your computer and use it in GitHub Desktop.
Save j4johnfox/466301 to your computer and use it in GitHub Desktop.
Modifications to support of multiple localized resources
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
==========
Rinse and repeat for as many localized strings files as you wish, making sure you add the appropriate key in the Info.plist (CPBundleLocalizedResourceKeys) in your application (see sample in this gist).
Helpful hint: You can use the comments argument in your use of CPLocalizedStrin() as a fall back:
CPLocalizedString("doneButtonLabel", " Done ");
You can create localized cib files as needed in the language specific *.lproj folders and load them with the CPBundle category method:
+ (CPCib)loadLocalizedCibNamed:(CPString)aName
owner:(id)anOwner
loadDelegate:(id)aDelegate
See the example in this gist called "MyController.j".
/*
* CPBundle.j
* AppKit
*
* Created by Nicholas Small, extended by John C. Fox
* 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;
var UserLanguage = null;
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)
+ (CPCib)loadLocalizedCibNamed:(CPString)aName owner:(id)anOwner loadDelegate:(id)aDelegate
{
if (![aName hasSuffix:@".cib"])
aName = [aName stringByAppendingString:@".cib"];
// Path is based solely on anOwner:
var bundle = anOwner ? [CPBundle bundleForClass:[anOwner class]] : [CPBundle mainBundle],
path = [bundle pathForLocalizedResource:aName];
return [self loadCibFile:path externalNameTable:[CPDictionary dictionaryWithObject:anOwner forKey:CPCibOwner] loadDelegate:aDelegate];
}
- (CPString)browserLanguage
{
if (navigator.appVersion.indexOf("MSIE") >= 0)
return navigator.browserLanguage.substring(0,2);
else
return navigator.language.substring(0,2);
}
- (CPString)bundleLocale
{
return [self bundleLocaleWithUserLanguage:UserLanguage];
}
- (CPString)pathForLocalizedResource:(CPString)aResourceName
{
var mainBundle = [CPBundle mainBundle];
return [mainBundle pathForResource:[mainBundle bundleLocale] + ".lproj/" +aResourceName];
}
- (CPString)bundleLocaleWithUserLanguage:(CPString)aUserLanguage
{
var defaultLocale = [self objectForInfoDictionaryKey:@"CPBundleDevelopmentRegion"];
var availableLocales = [self objectForInfoDictionaryKey:@"CPBundleLocalizedResourceKeys"];
var bestMatch, myLocale= nil;
UserLanguage = aUserLanguage;
// First try matching against the UserLanguage
if (UserLanguage && [UserLanguage length])
{
for (var i = 0; i < [availableLocales count]; i++)
{
myLocale = availableLocales[i];
if (UserLanguage && (myLocale == UserLanguage))
{
bestMatch = myLocale;
break;
}
}
}
// No match? Try the browser language
if (!bestMatch)
{
for (var i = 0; i < [availableLocales count]; i++)
{
myLocale = availableLocales[i];
if (myLocale == [self browserLanguage])
{
bestMatch = myLocale;
break;
}
}
}
// Still nothing? Use for the default, or hope your users understand English.
if (!bestMatch)
{
bestMatch = ([defaultLocale length]) ? defaultLocale : "en";
}
return bestMatch;
}
- (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)
{
// No localized value for key? Use the comment,
// or failing that, the key
string = ([aValue length] > 0) ? aValue : aKey;
[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)
{
// Look for some clue in the URL args
// for the user chosen language
var enumerator = [namedArgs keyEnumerator];
var key, value;
var userLanguage = nil;
while ((key = [enumerator nextObject]) != nil)
{
value = [namedArgs objectForKey:key];
if (key == "language" && ([value length] > 1))
{
userLanguage = value;
}
}
var mainBundle = [CPBundle mainBundle],
bundleLocale = [mainBundle bundleLocaleWithUserLanguage:userLanguage];
if (bundleLocale)
{
var request = [CPURLRequest requestWithURL:[mainBundle pathForLocalizedResource:"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>CPBundleDevelopmentRegion</key>
<string>en</string>
<key>CPBundleLocalizedResourceKeys</key>
<array>
<string>en</string>
<string>cs</string>
<string>de</string>
</array>
<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.j"
@import "AppController.j"
function main(args, namedArgs)
{
CPApplicationMain(args, namedArgs);
}
- (id)init
{
self = [super init];
// Load the localized cib file
[CPBundle loadLocalizedCibNamed:"MMSSharingController.cib" owner:self loadDelegate:self];
return self;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment