Manual Language Selection in an Ios-App (Iphone and Ipad)

manual language selection in an iOS-App (iPhone and iPad)

In the meantime I did find a solution for my problem on myself:

I created a new class "LocalizeHelper":


Header LocalizeHelper.h

//LocalizeHelper.h

#import <Foundation/Foundation.h>

// some macros (optional, but makes life easy)

// Use "LocalizedString(key)" the same way you would use "NSLocalizedString(key,comment)"
#define LocalizedString(key) [[LocalizeHelper sharedLocalSystem] localizedStringForKey:(key)]

// "language" can be (for american english): "en", "en-US", "english". Analogous for other languages.
#define LocalizationSetLanguage(language) [[LocalizeHelper sharedLocalSystem] setLanguage:(language)]

@interface LocalizeHelper : NSObject

// a singleton:
+ (LocalizeHelper*) sharedLocalSystem;

// this gets the string localized:
- (NSString*) localizedStringForKey:(NSString*) key;

//set a new language:
- (void) setLanguage:(NSString*) lang;

@end

iMplementation LocalizeHelper.m

// LocalizeHelper.m
#import "LocalizeHelper.h"

// Singleton
static LocalizeHelper* SingleLocalSystem = nil;

// my Bundle (not the main bundle!)
static NSBundle* myBundle = nil;


@implementation LocalizeHelper


//-------------------------------------------------------------
// allways return the same singleton
//-------------------------------------------------------------
+ (LocalizeHelper*) sharedLocalSystem {
// lazy instantiation
if (SingleLocalSystem == nil) {
SingleLocalSystem = [[LocalizeHelper alloc] init];
}
return SingleLocalSystem;
}


//-------------------------------------------------------------
// initiating
//-------------------------------------------------------------
- (id) init {
self = [super init];
if (self) {
// use systems main bundle as default bundle
myBundle = [NSBundle mainBundle];
}
return self;
}


//-------------------------------------------------------------
// translate a string
//-------------------------------------------------------------
// you can use this macro:
// LocalizedString(@"Text");
- (NSString*) localizedStringForKey:(NSString*) key {
// this is almost exactly what is done when calling the macro NSLocalizedString(@"Text",@"comment")
// the difference is: here we do not use the systems main bundle, but a bundle
// we selected manually before (see "setLanguage")
return [myBundle localizedStringForKey:key value:@"" table:nil];
}


//-------------------------------------------------------------
// set a new language
//-------------------------------------------------------------
// you can use this macro:
// LocalizationSetLanguage(@"German") or LocalizationSetLanguage(@"de");
- (void) setLanguage:(NSString*) lang {

// path to this languages bundle
NSString *path = [[NSBundle mainBundle] pathForResource:lang ofType:@"lproj" ];
if (path == nil) {
// there is no bundle for that language
// use main bundle instead
myBundle = [NSBundle mainBundle];
} else {

// use this bundle as my bundle from now on:
myBundle = [NSBundle bundleWithPath:path];

// to be absolutely shure (this is probably unnecessary):
if (myBundle == nil) {
myBundle = [NSBundle mainBundle];
}
}
}


@end

For each language you want to support you need a file named Localizable.strings. This works exactly as described in Apples documentation for localization. The only difference: Now you even can use languages like hindi or esperanto, that are not supported by Apple.

To give you an example, here are the first lines of my english and german versions of Localizable.strings:

English

/* English - English */

/* for debugging */
"languageOfBundle" = "English - English";

/* Header-Title of the Table displaying all lists and projects */
"summary" = "Summary";

/* Section-Titles in table "summary" */
"help" = "Help";
"lists" = "Lists";
"projects" = "Projects";
"listTemplates" = "List Templates";
"projectTemplates" = "Project Templates";

German

/* German - Deutsch */

/* for debugging */
"languageOfBundle" = "German - Deutsch";

/* Header-Title of the Table displaying all lists and projects */
"summary" = "Überblick";

/* Section-Titles in table "summary" */
"help" = "Hilfe";
"lists" = "Listen";
"projects" = "Projekte";
"listTemplates" = "Vorlagen für Listen";
"projectTemplates" = "Vorlagen für Projekte";

To use localizing, you must have some settings-routines in your app, and in the language-selection you call the macro:

LocalizationSetLanguage(selectedLanguage);

After that you must enshure, that everything that was displayed in the old language, gets redrawn in the new language right now (hidden texts must be redrawn as soon as they get visible again).

To have localized texts available for every situation, you NEVER must write fix texts to the objects titles. ALWAYS use the macro LocalizedString(keyword).

don't:

cell.textLabel.text = @"nice title";

do:

cell.textLabel.text = LocalizedString(@"nice title");

and have a "nice title" entry in every version of Localizable.strings!

How does an iOS app know the final user's language choice?

The app determines which language to use based on the Region and Language set up by the user.

Change Language section on published iOS App


IMPORTANT:

Languages display in the App Store and primary language is totally different.

Which languages are displayed in AppStore?

Languages which are display in-app store are the list of language
support by your application, That means if you want to include german
language then you have to put localise.string file for german
language. English is a base language for any application by default.

I have already worked with an application, Which is supported multilanguage (8 Language). I have attached the screenshot below. How it is displayed in-app store.

iOS Sample Image 19

Solution:

Just add localization file for german language then resubmit it. Then automatically german display languages list.

OR

You can change the language in the App store by changing the base language in the project setting. For Reference find the below screenshot.

iOS Sample Image 20

Reference:

Changing the development language in Xcode

Video tutorial for changing development language

How change app language in real time?

Depends on what are your trying to achieve, there is an approach that might be useful to your case, which is:

  1. Declare a global variable which should represents the key of the current used language in the app, call it appLanguageKey String -for example-:

    var appLanguageKey = "english"

  2. Create a datastore for storing both languages desired caption for each term, it might looks like (contains):

    term_key term_english term_farsi

    greeting_key Hello Hoi

    bye_key Bye Doei

For now, you could get the desired value if you tried to do:

desiredTerm = "select term_\(appLanguageKey) where term_key = 'greeting_key'"

Consider it as pseudo-code, the condition should be similar to it.

By implementing such a function that returns a string (I will assume that it is: getDesiredCaption(_ termKey: String) -> String in the following code snippet), you will be able to automatically set the desired caption for any UI component, just call it in viewWillAppear method the view controller:

override func viewWillAppear(_ animated: Bool) {
.
.
.

label.text = getDesiredCaption("greeting_key")
// Since that 'appLanguageKey' global viriable is "english",
// the output should be "Hello"

.
.
.
}

All you have to do for changing the language of the app is to change the value of appLanguageKey to "farsi".


For another approach which related to the Internationalization and Localization, you might want to check this answer, it should affect the app to let its navigation to be from right to left.

Hope this helped.

How to programmatically access the apps language in iOS 13?

You may try the below code to get the application preferred language.

let appLang = Locale.preferredLanguages[0]


Related Topics



Leave a reply



Submit