Search code examples
visual-studioxamarinlocalizationcross-platformmultilingual

Xamarin forms multi language app


im programming an application using xamarin forms And i want it to support 3 languages english freinch and Arabic How can i do so??
I can make it happen but only on the language ofdevice that the app is installed on for example if the emulator is using Arabic the app will display Arabic how can i make the user choose the language that he wants like a button or something can anyone help me please

I want it like a run time change in languages Like facebook u get to choose the language on the log in page and do not care about the device language

Thanks in advance


Solution

  • I think you can take a look to this Xamarin Forms Sample. It use RESX files.

    TodoLocalized

    In your code, you could create a picker with Language description. For example

        List<string> _language = new List<string>();
        List<string> _languageDescription = new List<string> ();
    
        // Strings used to identify RESX
        _language.Add ("it");
        _language.Add ("en");
    
        // Strings visualized in picker
        _languageDescription.Add (AppResources.Italian);
        _languageDescription.Add (AppResources.English);
    
        // Fill the picker with _languageDescription values
        Picker _pickerLanguage = new Picker ();
        _pickerLanguage.Items.Clear ();
        foreach (string language in _languageDescription)
            _pickerLanguage.Items.Add (language);
    
        // When I select a language, I change the AppResource.Culture value
        _pickerLanguage.SelectedIndexChanged += async (object sender, EventArgs e) => {
                if(_pickerLanguage.SelectedIndex >= 0){
    
                    // Search the "_language" value that has the same index of selected _languageDescription value
                    string myLanguage = _language [_languageDescription.FindIndex (o => o == _pickerLanguage.Items [_pickerLanguage.SelectedIndex])];
    
                    // Modify the culture
                    AppResources.Culture = new CultureInfo (myLanguage);
    
                }
            };
    

    Now you should refresh your page to see strings with new language

    Hope this help