|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:open_authenticator/model/app_unlock/reason.dart'; |
| 5 | +import 'package:path/path.dart' as path; |
| 6 | + |
| 7 | +/// Generates the polkit policy file and translations as well. |
| 8 | +Future<void> main() async { |
| 9 | + stdout.writeln('Reading translations...'); |
| 10 | + List<String> locales = []; |
| 11 | + List<FileSystemEntity> files = Directory('lib/i18n').listSync(); |
| 12 | + for (FileSystemEntity file in files) { |
| 13 | + if (file is Directory) { |
| 14 | + locales.add(path.basename(file.path)); |
| 15 | + } |
| 16 | + } |
| 17 | + Map<String, Map> translations = {}; |
| 18 | + for (String locale in locales) { |
| 19 | + File translationFile = File('lib/i18n/$locale/app_unlock.json'); |
| 20 | + translations[locale] = jsonDecode(translationFile.readAsStringSync()); |
| 21 | + } |
| 22 | + |
| 23 | + stdout.writeln('Generating polkit policy file...'); |
| 24 | + String policyFileContent = '''<?xml version="1.0" encoding="UTF-8"?> |
| 25 | +<policyconfig> |
| 26 | +'''; |
| 27 | + for (UnlockReason reason in UnlockReason.values) { |
| 28 | + policyFileContent += ''' |
| 29 | + <action id="app.openauthenticator.${reason.name}"> |
| 30 | + <description gettext-domain="polkit.app.openauthenticator">${translations["en"]!["authenticationRequired"]}</description> |
| 31 | + <message gettext-domain="polkit.app.openauthenticator">${translations["en"]!["localAuthentication(map)"][reason.name]}</message> |
| 32 | + <defaults> |
| 33 | + <allow_any>auth_self</allow_any> |
| 34 | + <allow_inactive>auth_self</allow_inactive> |
| 35 | + <allow_active>auth_self</allow_active> |
| 36 | + </defaults> |
| 37 | + </action> |
| 38 | +'''; |
| 39 | + } |
| 40 | + policyFileContent += '</policyconfig>'; |
| 41 | + File policyFile = File('snap/meta/polkit/polkit.app.openauthenticator.policy'); |
| 42 | + policyFile.parent.createSync(recursive: true); |
| 43 | + policyFile.writeAsStringSync(policyFileContent); |
| 44 | + stdout.writeln('Done : ${policyFile.path}.'); |
| 45 | + |
| 46 | + for (String locale in locales) { |
| 47 | + if (locale == 'en') { |
| 48 | + continue; |
| 49 | + } |
| 50 | + stdout.writeln('Generating $locale translation...'); |
| 51 | + String translationFileContent = '''msgid "${translations["en"]!["authenticationRequired"]}" |
| 52 | +msgstr "${translations[locale]!["authenticationRequired"]}" |
| 53 | +'''; |
| 54 | + for (UnlockReason reason in UnlockReason.values) { |
| 55 | + translationFileContent += ''' |
| 56 | +
|
| 57 | +msgid "${translations["en"]!["localAuthentication(map)"][reason.name]}" |
| 58 | +msgstr "${translations[locale]!["localAuthentication(map)"][reason.name]}" |
| 59 | +'''; |
| 60 | + } |
| 61 | + File translationFile = File('snap/meta/polkit/po/$locale.po'); |
| 62 | + translationFile.parent.createSync(recursive: true); |
| 63 | + translationFile.writeAsStringSync(translationFileContent); |
| 64 | + stdout.writeln('Done : ${translationFile.path}.'); |
| 65 | + } |
| 66 | +} |
0 commit comments