一个用于解析 Linux 上 freedesktop (XDG) 桌面条目的 Dart 包。

特点

  • 通过键获取值
  • 获取本地化值
  • 从操作组获取值
  • 根据规范的 locale 匹配规则本地化值
  • 本地化整个桌面条目

此包提供了 DesktopEntryKey 枚举以方便使用,但它不对值类型做出任何假设,也不对键是否必需做出假设。所有键都被视为可选。

用法

解析桌面条目文件

import 'package:freedesktop_desktop_entry/freedesktop_desktop_entry.dart';
import 'dart:io';

final file = File("desktop-entry.desktop");
String content = await file.readAsString();
DesktopEntry desktopEntry = DesktopEntry.parse(content);

本地化整个桌面条目

LocalizedDesktopEntry localizedDesktopEntry = desktopEntry.localize(lang: 'fr', country: 'BE');

获取本地化值

String? localizedComment = localizedDesktopEntry.entries[DesktopEntryKey.comment.string];
// OR
String? localizedComment = desktopEntry.entries[DesktopEntryKey.comment.string]?.localize(lang: 'fr', country: 'BE');

除非你只对少数字段感兴趣,否则请优先本地化整个桌面条目,以避免每次都指定 locale。

localize 方法将根据官方 locale 匹配规则本地化值,并在没有匹配的 locale 时使用默认值。这很可能是你想要做的。

获取默认值

String? name = desktopEntry.entries[DesktopEntryKey.name.string]?.value;
bool? terminal = desktopEntry.entries[DesktopEntryKey.terminal.string]?.value.getBoolean();
List<String>? keywords = desktopEntry.entries[DesktopEntryKey.keywords.string]?.value.getStringList();
bool? startupNotify = desktopEntry.entries['X-KDE-StartupNotify']?.value.getBoolean();

通过精确 locale 获取值

String? frenchComment = desktopEntry.entries[DesktopEntryKey.comment.string]?.localizedValues[Locale(lang: 'fr', country: 'BE')];

GitHub

查看 Github