Re: localizable strings file with macro
Re: localizable strings file with macro
- Subject: Re: localizable strings file with macro
- From: "Julien Jalon" <email@hidden>
- Date: Tue, 4 Mar 2008 10:28:40 +0100
1) Might not be a good idea to use CFStringGetCStringPtr as it might return
NULL2) kCFStringEncodingMacRoman is likely not a good choice as the
encoding, especially for localized strings
If I were you, I'd do something like (warning: Mail compiled code):
const char* MyGetLocalizedCString(CFStringRef string)
{
// if you need to be thread safe, add proper locks around that
static CFMutableDictionaryRef mapping = NULL;
if(mapping == NULL) {
mapping = CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks, NULL);
}
const char* result = (const char *)CFDictionaryGetValue(mapping,
string);
if(result == NULL) {
CFStringRef localizedString = CFCopyLocalizedStringFromTable(string,
CFSTR("LibraryLocalizable"), NULL);
result = (const char
*)malloc(CFStringGetMaximumSizeForEncoding(CFStringGetLength(localizedString),
kCFStringEncodingUTF8);
CFStringGetCString(localizedString, (char *)result, bufferSize,
kCFStringEncodingUTF8);
CFDictionarySetValue(mapping, string, result);
}
return result;
}
And use that as you localizing function:
#define _(a) MyGetLocalizedCString(CFSTR(a))
Even better, if you can change that, add an extra parameter to your
localizing macro and give it a better name:
#define _loc(a, comment) MyGetLocalizedCString(CFSTR(a))
This keeps your code portable and you should be able to replace the
occurrences of "_" in all your code fairly easily with a search and replace.
Added value is:
A) it makes your code more readable (IMHO)
B) it adds a proper way to explain the context of the localization
C) you can generate your strings file directly using genstrings:
genstrings -s _loc *.c -o /tmp ; mv /tmp/Localized.strings
<path>/LibraryLocalizable.strings
If you really can't do that, preprocess your files before launching
genstrings and call use "-s MyGetLocalizedCString"
--
Julien
On Tue, Mar 4, 2008 at 3:53 AM, Mitchell Livingston <email@hidden>
wrote:
> Hello,
>
> I have C code that I want to generate a strings file for use on Mac. I
> use:
>
> #if defined(SYS_DARWIN)
> #include <CoreFoundation/CFBundle.h>
> #define _(a) CFStringGetCStringPtr(CFCopyLocalizedStringFromTable( \
> CFSTR( a ), CFSTR("LibraryLocalizable"), "comment" ),
> kCFStringEncodingMacRoman)
> #else
> ...
>
> Unfortunately genstrings does not seem to compile the code before
> attempting to create the strings file. Is there some way to generate a
> string file while still considering that the code will also be run on
> other operating systems that generate localizable strings in different
> ways?
>
> Thanks,
> Mitchell Livingston
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden