Set Device Language
Set the language used by a connected device. The target language is represented by the SDK's DeviceLanguage enum.
Prerequisites
Before setting the device language, ensure:
- The device is connected and in a stable state
- The device supports the
DeviceInfoAPIprotocol - The selected language appears in the device's
supportedLanguageslist
API Reference
Framework
AIBuds.xcframework
Import
In the files where you want to use the SDK, import the main framework:
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>Protocol
The setDeviceLanguage method is defined in DeviceInfoAPI. The protocol inherits from the base device API protocol.
- Swift
- Objective-C
protocol DeviceInfoAPI: DeviceAPI {
/// Current language setting of the device.
var languageSetting: DeviceLanguage { get }
/// List of languages supported by the device, each element is an
/// `NSNumber` wrapping the raw value of `DeviceLanguage`.
var supportedLanguages: [NSNumber] { get }
/// Sets the device language.
/// - Parameters:
/// - language: The target language to set.
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
func setDeviceLanguage(
_ language: DeviceLanguage,
completion: AIBudsCompletionHandler?
)
}@protocol AIBudsDeviceInfoAPI <AIBudsDeviceAPI>
/// Current language setting of the device.
@property(nonatomic, readonly) enum AIBudsDeviceLanguage languageSetting;
/// List of languages supported by the device, each element is an
/// `NSNumber` wrapping the raw value of `DeviceLanguage`.
@property(nonatomic, readonly, copy) NSArray<NSNumber *> *_Nonnull supportedLanguages;
/// Sets the device language.
/// - Parameters:
/// - language: The target language to set.
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the
/// operation was successful.
- (void)setDeviceLanguage:(enum AIBudsDeviceLanguage)language
completion:(AIBudsCompletionHandler _Nullable)completion;
@endInstance Method
Sets the device language to a supported DeviceLanguage value.
- Swift
- Objective-C
/// Sets the device language.
/// - Parameters:
/// - language: The target language to set.
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
func setDeviceLanguage(
_ language: DeviceLanguage,
completion: AIBudsCompletionHandler?
)/// Sets the device language.
/// - Parameters:
/// - language: The target language to set.
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the
/// operation was successful.
- (void)setDeviceLanguage:(enum AIBudsDeviceLanguage)language
completion:(AIBudsCompletionHandler _Nullable)completion;Parameters
| Parameter | Type | Description |
|---|---|---|
language | DeviceLanguage / AIBudsDeviceLanguage | The target language to set. |
completion | AIBudsCompletionHandler? | Optional completion handler called when the operation finishes. |
Callback Parameters:
| Name | Type | Description |
|---|---|---|
success | Bool / BOOL | true if the operation succeeded; otherwise false. |
error | NSError? | Error details if the operation failed; otherwise nil. |
Return Value
This method does not return a value directly. The result is provided through the completion handler.
Usage Examples
- Swift
- Objective-C
import AIBuds
final class DeviceManager {
weak var device: DeviceConvertible?
func setDeviceLanguage(_ language: DeviceLanguage) {
guard let device = device as? DeviceInfoAPI else {
print("Device does not support language settings")
return
}
let isSupported = device.supportedLanguages.contains {
$0.intValue == language.rawValue
}
guard isSupported else {
print("The selected language is not supported by this device")
return
}
device.setDeviceLanguage(language) { success, error in
if !success {
print("Failed to set language: \(error?.localizedDescription ?? "Unknown error")")
return
}
print("Device language set successfully")
}
}
}#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
@interface DeviceManager ()
@property(weak, nonatomic) id<AIBudsDeviceConvertible> device;
@end
@implementation DeviceManager
- (void)setDeviceLanguage:(AIBudsDeviceLanguage)language {
id<AIBudsDeviceInfoAPI> device = (id<AIBudsDeviceInfoAPI>)self.device;
if (![device conformsToProtocol:@protocol(AIBudsDeviceInfoAPI)]) {
NSLog(@"Device does not support language settings");
return;
}
if (![device.supportedLanguages containsObject:@(language)]) {
NSLog(@"The selected language is not supported by this device");
return;
}
[device setDeviceLanguage:language
completion:^(BOOL success, NSError *_Nullable error) {
if (!success) {
NSLog(@"Failed to set language: %@",
error.localizedDescription ?: @"Unknown error");
return;
}
NSLog(@"Device language set successfully");
}];
}
@endError Handling
- Check
successbefore treating the new language as applied. - Use
errorfor failure details when the operation does not succeed. - Reject unsupported values before sending the command.
- Do not assume a specific error code unless it is documented for the target device.
Best Practices
- Use the SDK Enum: Pass a
DeviceLanguagevalue instead of an ISO language-code string. - Check Supported Languages: Compare the enum's raw value with
supportedLanguagesbefore calling the method. - Check Protocol Conformance: Confirm that the device supports
DeviceInfoAPI. - Update UI on the Main Queue: Dispatch completion-driven UIKit updates to the main queue.
Notes
supportedLanguagescontainsNSNumbervalues that wrapDeviceLanguageraw values.languageSettingexposes the device's current language setting.- Supported languages can vary by device model and firmware.
- The SDK Demo sets the language to
AIBudsDeviceLanguageEnglish; production applications should select from the device's advertised values.