TEXT

Custom Localization and AI Integration for Apps

Contributed by ahmettzorlutuna

Improved by Laravel Company · 2026-09-07

Improved prompt:

Act as a Senior Localization Architect with extensive experience in user-preference-based language management for iOS applications. Your task is to design and implement a robust, user-driven localization strategy independent of the device's system language settings.

Your project requirements include:

  1. LanguageManager Class: Create a LanguageManager class conforming to the ObservableObject protocol, utilizing SwiftUI's state management capabilities. This class will serve as the primary controller for the application's language preferences.

    • Implement a property called selectedLanguage that stores the user's chosen language as an ISO 639-1 two-letter code (e.g., 'en' for English, 'tr' for Turkish).
    • Initialize selectedLanguage with a default value of 'en' (English) using UserDefaults to maintain persistence across app sessions.
    • On the first app launch, trigger the display of a language selection screen using SwiftUI's navigation logic.
  2. Global Locale Override: Implement a global language override for the entire application's UI.

    • Wrap the root ContentView structure of your SwiftUI application with the .environment(\.locale, .init(identifier: languageManager.selectedLanguage)) modifier. This will ensure that all native and custom SwiftUI views throughout the app will adopt the selected language for text rendering.

    • Ensure that the app utilizes iOS's internal string catalog system (Localizable.strings) and SwiftUI's Text(_:file:) function to render localized strings automatically based on the selected language.

  3. Onboarding Language Selection: Develop a user-friendly language selection interface for the initial app launch.

    • Design a stylish, visually appealing language selection screen that appears only if the user has not selected a language previously.
    • Include clearly labeled buttons or options for the supported languages (currently English and Turkish).
    • Implement immediate saving of the selected language to UserDefaults upon tap, followed by an automatic transition to the main application screen using SwiftUI's navigation stack.
  4. AI Integration: Incorporate the user's selected language preference into the application's AI-driven components.

    • Add a selectedLanguage parameter to the payload of all API requests involving AI interaction (e.g., text generation, translation, or chatbot responses).
    • If the selectedLanguage property is set to a value other than 'en' (English), append the selected language to the system prompt as a directive, formatted as: 'User's preferred language: ${selected_language}. Respond in this language.'
  5. String Catalogs: Integrate string catalog files into your Xcode project for multi-language support.

    • Add a base Localizable.strings file containing all hardcoded English strings (base language).
    • Create a Localizable.strings file for each supported language, e.g., Localizable.strings(tr) for Turkish.
    • Ensure that all existing hardcoded strings in the English language are migrated to the base Localizable.strings file.
  6. Dynamic Language Update: Implement real-time language updates without requiring an app restart.

    • When the user changes the language in the app's settings or through the language selection screen, update the selectedLanguage property in the LanguageManager object.
    • Utilize the .environment(\.locale, .init(identifier: languageManager.selectedLanguage)) modifier to propagate the language change to the entire app UI instantly.
    • Ensure that the app's AI components receive the updated selectedLanguage value in real-time for subsequent interactions.
  7. User Language Change: Provide a mechanism for users to change the app's language at any time.

    • Implement a language selection option in the app's settings or a dedicated language preference screen.
    • Ensure that changing the language dynamically triggers an immediate UI update and updates the selectedLanguage property in the LanguageManager object.
  8. Testing and Validation: Conduct thorough testing to ensure a seamless user experience across language selection and updates.

    • Test the language selection functionality for both the initial app launch and subsequent language changes.
    • Verify that the UI displays the correct localized text in both English and Turkish.
    • Validate that the AI components respond appropriately using the specified language directive.

Your goal is to create a highly polished, user-driven localization architecture that provides a seamless and intuitive language management experience while ensuring the application's AI components consistently adhere to the user's preferred language.

Original prompt (before our improvements)

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.