月曜日, 1月 14, 2008

Select and set a locale for InputContext from the available locales list

Yesterday, I wrote in this blog about InputContext and how to set the language environment. Here are some more codes for listing up all the available languages, from which you can select the Locale and set it for the InputContext for your application.


Locale locales[]=Locale.getAvailableLocales();


A word of caution is that not all listed locales are available for InputContext -- of those, only that has both the language code and the country code can be set to use. But once you have the array of strings for the available locales, this input dialog will do the job nicely to put all locales in scrollable pane and wait for user input. Here, names is the string array that contains the list of locales.


Object selectedValue = JOptionPane.showInputDialog(null,
"Select a locale", "Language",
JOptionPane.INFORMATION_MESSAGE, null,
names,names[0]);


Perhaps it gets more clearer if you look at the code so I put it right here. Here all the locales that only have either the country code or the language code are filtered out. This is what this following code does: the list of locales will be presented and then when a value is selected, the locale will be set for the InputContext.

As of 2008-1-16, the code has been modified so that the list will be sorted out in alphabetical order.

   public void langList(JMenuItem mi)
{
int x=mi.getX();
x+=mi.getWidth();
int y=mi.getY();
Locale locales[]=Locale.getAvailableLocales();
String names[]=new String[locales.length];
String languages[]=new String[locales.length];
String countries[]=new String[locales.length];
int index=0;
for(int i=0;i<locales.length;i++)
{
languages[index]=locales[i].getLanguage();
countries[index]=locales[i].getCountry();
if(!languages[index].isEmpty() && !countries[index].isEmpty())
{
names[index]=locales[i].getDisplayName();
index++;
}
}
int nLocales=index;
int sortedIndex[]=new int[nLocales];
String sortedNames[]=new String[nLocales];
for(int i=0;i<nLocales;i++)
sortedIndex[i]=i;
for(int i=0;i<nLocales;i++)
{
for(int j=i+1;j<nLocales;j++)
{
if(0<names[sortedIndex[i]].compareTo(names[sortedIndex[j]]))
{
int t=sortedIndex[i];
sortedIndex[i]=sortedIndex[j];
sortedIndex[j]=t;
}
}
}
for(int i=0;i<nLocales;i++)
{
sortedNames[i]=names[sortedIndex[i]];
}
if(0<nLocales)
{
Object selectedValue = JOptionPane.showInputDialog(null,
"Set Language Environment", "Language",
JOptionPane.INFORMATION_MESSAGE, null,
sortedNames,sortedNames[0]);
int i=0;
if(selectedValue!=null)
{
while(!((String)selectedValue).equals(names[i]) && ++i<nLocales);
if(i<nLocales)
{
Locale newLocale=new Locale(languages[i],countries[i]);
inputContext.selectInputMethod(newLocale);
}
}
}
}

Qt: 外部プログラムを起動する

  Qt/C++ のアプリは、外部へ直接アクセスできます。これはネットアプリでは不可能な Qt のメリットです。 外部プログラムを起動することもできます。QProcess::startDetached() を使うと独立したプロセスを立ち上げることができます。 この QProces...