You Are Here:

Community: Wiki

This page was last modified on 6 October 2009, at 09:47.

应用程序启动时的语言选择

From Forum Nokia Wiki

Contents

概述

Symbian/S60应用程序默认使用当前的手机语言作为应用程序的语言,但是许多开发者希望在应用程序启动时给用户选择应用程序语言的机会。

方案

本方案可以使用户能够在应用程序启动时选择期望应用程序使用的语言。

步骤 1:在应用程序一开始启动就显示一个全局列表,列出应用程序支持的所有语言,然后用某种方法记住用户的选择。示例程序定义了一个发布与订阅属性来保存用户的选择。

// begin by chen
...
#include <badesca.h>
#include <akngloballistquery.h>
#include <e32property.h>
 
void MainL()
{
_LIT(KPhoneLanugage, "Phone language");
_LIT(KEnglish, "English");
_LIT(KChinese, "Chinese");
CDesCArray* array = new(ELeave) CDesCArrayFlat(5);
CleanupStack::PushL(array);
array->AppendL(KPhoneLanugage);
array->AppendL(KEnglish);
array->AppendL(KChinese);
// the application framework has not be created yet so we the notifier service to show the list
CAknGlobalListQuery* query = CAknGlobalListQuery::NewLC();
TRequestStatus status = KRequestPending;
query->ShowListQueryL(array, status);
User::WaitForRequest(status);
TInt ret = status.Int();
if(ret>=0)
{
// ret is the index of the user selected item
// here we map the user selection to a language code
TLanguage language = ELangEnglish;
switch(ret)
{
case 0:
{
language = User::Language();
break;
}
case 1:
{
language = ELangEnglish;
break;
}
case 2:
{
language = ELangPrcChinese;
break;
}
default:
{
language = User::Language();
break;
}
}
// here we define a property to hold the user selection, but this is definitely not the only way, for example you can also save it to a file.
RProperty::Define(KUidUrzpvtchApplication, 0, RProperty::EInt);
RProperty::Set(KUidUrzpvtchApplication, 0, (TInt)language);
}
CleanupStack::PopAndDestroy(2, array);
}
 
GLDEF_C TInt E32Main()
{
// begin by chen
// at this stage we have to create the cleanup stack by ourself
// Cleanup stack needed
CTrapCleanup* cleanup = CTrapCleanup::New();
if(cleanup)
{
TRAPD(err, MainL());
if(err != KErrNone)
{
_LIT(KUserPanic,"Failed to start");
User::Panic(KUserPanic, err);
}
delete cleanup;
}
// end by chen
return EikStart::RunApplication( NewApplication );
}

步骤 2:在应用程序框架初始化过程中会调用App类的ResourceFileName()方法获取应用程序资源文件的文件名,因此这里重载该方法通过更改文件扩展名返回用户期望的资源文件。

#include <coeutils.h>
...
TFileName CUrzpvtchApplication::ResourceFileName() const
{
TFileName filename = CAknApplication::ResourceFileName();
TInt language = 0;
TInt err = RProperty::Get(KUidUrzpvtchApplication, 0, language);
if(err==KErrNone)
{
TFileName fn = filename;
_LIT(KExtFormat, "r%d");
TBuf<16> buf;
buf.Format(KExtFormat, language);
if(buf.Length()<=2) // for example "r1"
{
_LIT(KZero, "0");
buf.Insert(1, KZero); // now should be "r01"
}
TInt pos = fn.LocateReverse('.');
fn.SetLength(pos+1); // remove the extention
fn.Append(buf); // add new extention
// make sure that the filename actually exists, or there will be a User 23 panic
TBool exists = ConeUtils::FileExists(fn);
if(exists)
{
filename=fn;
}
}
return filename;
}

源代码

完整的示例程序: Urzpvtch(LanguageSelection).zip

注意

1. 应用程序的标题不会变化,因为它定义在_reg.rsc资源文件里。

2. 系统字符串,例如CBA的"选项"/"选择"/"取消",也不会变化,因为它们定义在avkon.rsc里。

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia