updateInfoPlist static method
Implementation
static Future<void> updateInfoPlist({
required String plistPath,
required List<String> localizations,
}) async {
final file = File(plistPath);
if (!file.existsSync()) {
throw Exception('Info.plist file not found at $plistPath');
}
final document = XmlDocument.parse(await file.readAsString());
final dictElement = document.findAllElements('dict').first;
// Collect elements to be removed
final elementsToRemove = <XmlElement>[];
dictElement
.findElements('key')
.where((element) => element.innerText == 'CFBundleLocalizations')
.forEach((element) {
elementsToRemove.add(element);
if (element.nextElementSibling != null) {
elementsToRemove.add(element.nextElementSibling!);
}
});
// Remove collected elements
for (var element in elementsToRemove) {
element.parent?.children.remove(element);
}
// Create new CFBundleLocalizations element
final localizationsKey = XmlElement(XmlName('key'), [], [XmlText('CFBundleLocalizations')]);
final localizationsArray = XmlElement(
XmlName('array'),
[],
localizations
.map(
(locale) => XmlElement(XmlName('string'), [], [XmlText(locale)]),
)
.toList(),
);
// Add new elements to the dict
dictElement.children.add(localizationsKey);
dictElement.children.add(localizationsArray);
// Write the updated content back to the file
await file.writeAsString(
document.toXmlString(pretty: true, indent: ' '),
);
}