My client has given me a custom TTF font only for English. So my intention is to load that TTF only for English and for other languages it should fallback to the available System font.
I am doing the following steps:
- Registering fonts with my app.
Copying into Resources folder and add to my project. -
Loading the font with name.
NSMutableDictionary * aTitleAttributes = [[[NSMutableDictionary alloc] initWithObjectsAndKeys: [NSFont fontWithName:CP_FONT_NAME size:[NSFont systemFontSize]], NSFontAttributeName, aParagraphStyle, NSParagraphStyleAttributeName, nil] autorelease];
But this results into another problem, since I am loading the fonts in this way for all the languages, my localization text looks english. Hence I think I should have some condition like
NSFont *font = nil;
If (currentLocaleIsEnglish()) {
/// font to load my custom TTF
}
Then use [NSFont fontwithName:font ...]
So can anybody help me to realize whatever I am doing is right with registering the font with App. If so how can I register my font only for English version. Because for other locales this font is not available then NSFont::fontWithName
will return nil and automatically falls back to the SystemFont and hence my locale shall be shown properly.
Otherwise I have to have a condition like what I mentioned above, which is a crude way of doing. If so how can I write such an API for finding currentLocale Is English?
Best Solution
Get the current locale from NSLocale, then send it an
objectForKey:
message and ask forNSLocaleLanguageCode
. If the current language code isen
,en_US
,en_GB
,en_AU
, oren_CA
, then the current language is some form of English.To simplify the test, you can put the language codes into an array or set and test the current language code's membership in that collection. Or, better yet, put them into a dictionary as the keys, with the value for all of them being
CP_FONT_NAME
; then, if the client later provides other fonts for other languages, you can add those new font-language pairs easily.