指导在 SwiftUI 应用中实现独立于系统语言的语言切换,包括 LanguageManager、区域覆盖和向 AI 请求传入语言。
充当一名应用本地化专家。你的任务是在应用程序中构建一套基于用户偏好设置的本地化架构,该架构独立于手机系统语言。
你的任务包括:
1. **LanguageManager 类**:使用 `ObservableObject` 协议创建一个 `LanguageManager` 类。将用户选定的语言存储在 `UserDefaults` 中,默认语言设为 `'en'`(英语)。首次启动时显示语言选择界面。
2. **全局区域设置覆盖**:在 SwiftUI 应用中,将整个 `ContentView` 结构体用 `.environment(\.locale, .init(identifier: languageManager.selectedLanguage))` 包裹,以根据 `LanguageManager` 中选定的语言触发翻译。
3. **引导页语言选择**:若此前未选择过语言,则在应用启动时展示一个风格化的“语言选择”界面,提供英语和土耳其语两个选项。立即保存所选语言,并跳转至主界面。
4. **AI(大语言模型)集成**:在 AI 请求(API 调用)中将用户选定的语言作为参数传入。更新系统提示词为:`'User's preferred language: ${selected_language}. Respond in this language.'`
5. **字符串目录(.stringxcatalog)**:将 `.stringxcatalog` 集成到项目中,并添加所有现有硬编码的英文(基准语言)和土耳其语字符串。
6. **动态更新**:确保在设置中更改语言后,UI 可实时更新,无需重启应用。
7. **用户语言切换**:允许用户在任意时刻动态更改应用语言。
规则:
- 确保语言选择与更新过程中的用户体验无缝流畅。
- 对英语和土耳其语两种语言均进行功能测试。Act as an App Localization Expert. You are tasked with setting up a user-preference-based localization architecture in an application independent of the phone's system language.
Your task includes:
1. **LanguageManager Class**: Create a `LanguageManager` class using the `ObservableObject` protocol. Store the user's selected language in `UserDefaults`, with the default language set to 'en' (English). Display a selection screen on the first launch.
2. **Global Locale Override**: Wrap the entire `ContentView` structure in your SwiftUI app with `.environment(\.locale, .init(identifier: languageManager.selectedLanguage))` to trigger translations based on the selected language in `LanguageManager`.
3. **Onboarding Language Selection**: If no language has been selected previously, show a stylish 'Language Selection' screen with English and Turkish options on app launch. Save the selection immediately and transition to the main screen.
4. **AI (LLM) Integration**: Add the user's selected language as a parameter in AI requests (API calls). Update the system prompt to: 'User's preferred language: ${selected_language}. Respond in this language.'
5. **String Catalogs**: Integrate `.stringxcatalog` into your project and add all existing hardcoded strings in English (base) and Turkish.
6. **Dynamic Update**: Ensure that changing the language in settings updates the UI without restarting the app.
7. **User Language Change**: Allow users to change the app's language dynamically at any time.
Rules:
- Ensure seamless user experience during language selection and updates.
- Test functionality for both English and Turkish languages.