Customization and Localization of Permissions in Flutter
We will be discussing on how to achieve the following permissions in Flutter.
Android Customization
As of writing, Android does not allow custom message for their system permissions. Instead, they require you to explain why your app needs that permission, using your app’s UI element. After the user have read the app’s explanation and agreed, then call the Android permission.
Android Localization
Localization is already handled by Android itself. Simply go to settings and change to the desired language. However, if you want to change the locale during runtime, this wouldn’t affect permissions locale since the permission depends on the Android system language.
iOS Customization
Luckily iOS allows customization of permissions. On Xcode, go to Info.plist located in Runner > Runner. Select the privacy permission you want to update and change its value. In my case, I want to update the value of Photo Library Usage Description.
However we can only customize the content of the permissions. The headers and buttons value are controlled by iOS.
iOS Localization
Like Android, localization is already handled by iOS itself. Just change the language on the settings and the permission vocabulary would change.
However, if you have defined a custom content for the permission, then you have to create a new infoplist. This new infoplist would contain the custom content translated to the supported language, in this case spanish. Else you would end up with a permission like this: english content, but the headers and actions are in spanish.
To create a translated custom content, first create a new file under Runner > Runner.
On the templates window, search for “string” under iOS.
and then save the Strings file as “InfoPlist.strings”
You should now have an empty InfoPlist.strings under Runner > Runner and should look similar to this.
We will now localize this newly created InfoPlist.strings, to do that first toggle the inspector by tapping on the upper right icon besides the + icon.
On the inspector locate the “Localization” section and tap on the button “Localize…”
The newly created InfoPlist should now look like this, having an InfoPlist (en) as its name.
We can now use this InfoPlist as the file containing all english permissions. To do that, we will declare them using the raw key as key and our custom content as the value. In our example the raw key of Photo Library Usage Description is “NSPhotoLibraryUsageDescription”, and we will supply it with our english content.
If you want to get the raw key equivalent of the permissions, simply right click on Info and choose “Raw Keys and Values”
Now we will update the localizations configuration of the main Runner. Under the main Runner, select Project > Runner and select the info section. Under info we have the localizations config.
Below the last row we see a plus and minus text; we’ll choose the plus text and add our chosen language, in this example its Spanish (es).
Then a prompt would show asking where to reference this spanish language. Make sure to only check InfoPlist.strings and uncheck the first 2, Main.storyboard & LaunchScreen.storyboard
Once done it should look like this, Localization would be updated with the new language Spanish, and to your left the InfoPlist will now have a dropdown icon, meaning a new strings file was created.
Tap on the InfoPlist dropdown and it will reveal the files under it, there should be 2 InfoPlists, one English and one Spanish. Open the InfoPlist (Spanish) and there we will update the spanish translation equivalent of our english content.
Run the app again and see that our custom content is now being localized.
Changing Locale on runtime
Although we can change the locale of the app during runtime, this app locale can’t influence the locale of the system permissions of iOS or Android. For the permissions to change its language, the language settings of the Android or iOS phone should be set to the desired language.