Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
atif_imran
Posts: 13
Registered: ‎10-04-2012
My Device: BlackBerry 9930
My Carrier: RIM
Accepted Solution

Picking values from two resource bundles for in-app localization.

Hello everyone !

This is my first post and I feel good about it :smileyhappy:I have created two resource files and now I want the user to select the language so that the app refreshes itself to that locale, Here's how I did it.

 

public static String localizeString(int key) {
		
		String appLocale = ConfigManager.getLanguage();
		String localizedString = "";
	
		if(appLocale.equals("en")){
			ResourceBundle _resources = ResourceBundle.getBundle(0x8db49c0ed78e47deL, "com.blackberry.util.LocalizeEN");
			localizedString = _resources.getString(key);
		}
		else if(appLocale.equals("es")){
			ResourceBundle _resources = ResourceBundle.getBundle(0x6185d12a6d73a498L, "com.blackberry.util.LocalizeES");
			localizedString = _resources.getString(key);
		}

		return localizedString;
	}

 I have implemented two interfaces created by BlackBerry but when I call this method it says the field is ambigous probably because it doesn't know from what resource to pick value. Please help :smileysad:

Please use plain text.
Developer
maadani
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Picking values from two resource bundles for in-app localization.

Please use plain text.
Contributor
atif_imran
Posts: 13
Registered: ‎10-04-2012
My Device: BlackBerry 9930
My Carrier: RIM

Re: Picking values from two resource bundles for in-app localization.

Unfortunately that's not what I am looking for. The example simply picks the resources from one file. In my case I am truing it with two rrh(bundle) files so that the localization is done instantly wihout changing the Device langugage setting.

Please use plain text.
Developer
hemant_sain
Posts: 179
Registered: ‎12-09-2011
My Device: 9360, BB10
My Carrier: Aircel

Re: Picking values from two resource bundles for in-app localization.

Hi Atif

 

you can do the language localization via very simple way.

 

create the resource localization file in your project and add the value and key pair of the texts which you want in your project.

 

use this code for the language localization it will automatically take the text according to your app language.

 

public  ResourceBundle testResources = ResourceBundle.getBundle(TestResourceResource.BUNDLE_ID,TestResourceResource.BUNDLE_NAME);

testResources.getString(TestResourceResource.lblTest);

 here TestResource is the name of the resource file which you can create in your project.

 

Please use plain text.
Contributor
atif_imran
Posts: 13
Registered: ‎10-04-2012
My Device: BlackBerry 9930
My Carrier: RIM

Re: Picking values from two resource bundles for in-app localization.

Thanks, but if you see my code I have taken a parameter 'key' in my method so that I can dynamically display the localized text in my app. So, I cannot use any specific key like the 'lblTest' 

Please use plain text.
Developer
hemant_sain
Posts: 179
Registered: ‎12-09-2011
My Device: 9360, BB10
My Carrier: Aircel

Re: Picking values from two resource bundles for in-app localization.

Hi Atif

 

can i know that why are you using two separate resource bundles for diff diff language? You can do this via single resource bundle and i think key ramains unique for the multiple languages.

Please use plain text.
Contributor
atif_imran
Posts: 13
Registered: ‎10-04-2012
My Device: BlackBerry 9930
My Carrier: RIM

Re: Picking values from two resource bundles for in-app localization.

I am giving user an option to select a language and when he does the screen refreshes and the screen content also gets localized. Now depending upon the app language the bundle should be selected. If my app language is "es" how do I pick the es.rrc? 

Please use plain text.
Contributor
atif_imran
Posts: 13
Registered: ‎10-04-2012
My Device: BlackBerry 9930
My Carrier: RIM

Re: Picking values from two resource bundles for in-app localization.

Thank you all!

Finally I did what I wanted :smileyvery-happy:

 

if(appLocale.equals("en")){
			ResourceBundle _resources = ResourceBundle.getBundle(LocalizationResource.BUNDLE_ID, LocalizationResource.BUNDLE_NAME).getBundle(Locale.get(Locale.LOCALE_en));
			localizedString = _resources.getString(key);
		}
		else if(appLocale.equals("es")){
			ResourceBundle _resources = ResourceBundle.getBundle(LocalizationResource.BUNDLE_ID, LocalizationResource.BUNDLE_NAME).getBundle(Locale.get(Locale.LOCALE_es));
			localizedString = _resources.getString(key);
		}

 I used only one resource file and simply picked up the one using

Locale.LOCALE_es and Locale.LOCALE_en

 depending upon my app language.

 

Please use plain text.
Developer
hemant_sain
Posts: 179
Registered: ‎12-09-2011
My Device: 9360, BB10
My Carrier: Aircel

Re: Picking values from two resource bundles for in-app localization.

use this code to set the language:

 

public void SetLanguage(int langType)
{
	Locale _locale;	
	switch (langType) 
	{				
		case LanguageType.ENGLISH:
			AppConstants.LANGUAGE_CODE = "en";
			_locale = Locale.get(Locale.LOCALE_en);
			Locale.setDefault(_locale);
			break;
		case LanguageType.ARABIC:
			AppConstants.LANGUAGE_CODE = "ar";
			_locale = Locale.get(Locale.LOCALE_ar);
			Locale.setDefault(_locale);
			break;
		default:
			_locale=Locale.getDefault();
			Locale.setDefault(_locale);
			break;
	}
}

 

Please use plain text.
Developer
hemant_sain
Posts: 179
Registered: ‎12-09-2011
My Device: 9360, BB10
My Carrier: Aircel

Re: Picking values from two resource bundles for in-app localization.

good to know. :smileyhappy:

 

Please use plain text.