メインコンテンツまでスキップ

デバイス言語の設定

接続中のデバイスで使用する言語を設定します。対象言語は SDK の DeviceLanguage 列挙型で指定します。

前提条件

デバイス言語を設定する前に、次の条件を確認してください。

  • デバイスが接続済みで、安定した状態にあること
  • デバイスが DeviceInfoAPI プロトコルに対応していること
  • 選択した言語がデバイスの supportedLanguages リストに含まれていること

API リファレンス

フレームワーク

AIBuds.xcframework

インポート

SDK を使用するファイルで、メインフレームワークをインポートします。

Swift
import AIBuds

プロトコル

setDeviceLanguage メソッドは DeviceInfoAPI で定義されています。このプロトコルは基底デバイス API プロトコルを継承します。

Swift
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?
    )
}

インスタンスメソッド

デバイス言語を、対応する DeviceLanguage の値に設定します。

Swift
/// 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?
)

パラメータ

パラメータ説明
languageDeviceLanguage / AIBudsDeviceLanguage設定する言語。
completionAIBudsCompletionHandler?処理完了時に呼び出される任意の完了ハンドラー。

コールバックのパラメータ:

名前説明
successBool / BOOL処理が成功した場合は true、それ以外は false
errorNSError?処理失敗時のエラー詳細。成功時は nil

戻り値

このメソッドは値を直接返しません。結果は完了ハンドラーに渡されます。

使用例

Swift
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")
        }
    }
}

エラー処理

  1. 新しい言語が反映されたと判断する前に success を確認します。
  2. 処理に失敗した場合は、error から詳細を取得します。
  3. コマンド送信前に、非対応の値を除外します。
  4. 対象デバイスについて文書化されていない特定のエラーコードを前提にしないでください。

ベストプラクティス

  1. SDK の列挙型を使う: ISO 言語コード文字列ではなく、DeviceLanguage の値を渡します。
  2. 対応言語を確認する: メソッドを呼び出す前に、列挙値の raw value と supportedLanguages を比較します。
  3. プロトコル対応を確認する: デバイスが DeviceInfoAPI に対応していることを確認します。
  4. メインキューで UI を更新する: 完了ハンドラーからの UIKit 更新はメインキューへ切り替えます。

注意事項

  • supportedLanguages には NSNumber が含まれ、それぞれが DeviceLanguage の raw value を格納しています。
  • languageSetting はデバイスの現在の言語設定を返します。
  • 対応言語は、デバイスモデルやファームウェアによって異なる場合があります。
  • SDK Demo では言語を AIBudsDeviceLanguageEnglish に設定しています。本番アプリでは、デバイスが通知する対応値から選択してください。