デバイス言語の設定
接続中のデバイスで使用する言語を設定します。対象言語は SDK の DeviceLanguage 列挙型で指定します。
前提条件
デバイス言語を設定する前に、次の条件を確認してください。
- デバイスが接続済みで、安定した状態にあること
- デバイスが
DeviceInfoAPIプロトコルに対応していること - 選択した言語がデバイスの
supportedLanguagesリストに含まれていること
API リファレンス
フレームワーク
AIBuds.xcframework
インポート
SDK を使用するファイルで、メインフレームワークをインポートします。
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>プロトコル
setDeviceLanguage メソッドは DeviceInfoAPI で定義されています。このプロトコルは基底デバイス API プロトコルを継承します。
- 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;
@endインスタンスメソッド
デバイス言語を、対応する DeviceLanguage の値に設定します。
- 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;パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
language | DeviceLanguage / AIBudsDeviceLanguage | 設定する言語。 |
completion | AIBudsCompletionHandler? | 処理完了時に呼び出される任意の完了ハンドラー。 |
コールバックのパラメータ:
| 名前 | 型 | 説明 |
|---|---|---|
success | Bool / BOOL | 処理が成功した場合は true、それ以外は false。 |
error | NSError? | 処理失敗時のエラー詳細。成功時は nil。 |
戻り値
このメソッドは値を直接返しません。結果は完了ハンドラーに渡されます。
使用例
- 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");
}];
}
@endエラー処理
- 新しい言語が反映されたと判断する前に
successを確認します。 - 処理に失敗した場合は、
errorから詳細を取得します。 - コマンド送信前に、非対応の値を除外します。
- 対象デバイスについて文書化されていない特定のエラーコードを前提にしないでください。
ベストプラクティス
- SDK の列挙型を使う: ISO 言語コード文字列ではなく、
DeviceLanguageの値を渡します。 - 対応言語を確認する: メソッドを呼び出す前に、列挙値の raw value と
supportedLanguagesを比較します。 - プロトコル対応を確認する: デバイスが
DeviceInfoAPIに対応していることを確認します。 - メインキューで UI を更新する: 完了ハンドラーからの UIKit 更新はメインキューへ切り替えます。
注意事項
supportedLanguagesにはNSNumberが含まれ、それぞれがDeviceLanguageの raw value を格納しています。languageSettingはデバイスの現在の言語設定を返します。- 対応言語は、デバイスモデルやファームウェアによって異なる場合があります。
- SDK Demo では言語を
AIBudsDeviceLanguageEnglishに設定しています。本番アプリでは、デバイスが通知する対応値から選択してください。