ParsedLocaleFile.load constructor

ParsedLocaleFile.load(
  1. String path
)

Loads and parses a .sway.json file from disk.

Throws SwayJsonParseException for malformed JSON, SwayEncodingException for non-UTF8 files.

Implementation

factory ParsedLocaleFile.load(String path) {
  final file = File(path);
  if (!file.existsSync()) {
    throw SwayJsonParseException(
      'Locale file not found',
      filePath: path,
    );
  }

  String content;
  try {
    content = file.readAsStringSync(encoding: utf8);
  } on FileSystemException {
    throw SwayEncodingException(
      'Non-UTF8 encoding detected in file',
      filePath: path,
    );
  }

  final parsed = _parseJson(content, path);
  final languageCode = _extractLanguageCode(path);

  return ParsedLocaleFile(
    languageCode: languageCode,
    data: parsed,
    filePath: path,
  );
}