encode_kn_arb_files.dart 3.22 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8 9
// The utility function `encodeKnArbFiles` replaces the material_kn.arb
// and cupertino_kn.arb files in flutter_localizations/packages/lib/src/l10n
// with versions where the contents of the localized strings have been
// replaced by JSON escapes. This is done because some of those strings
// contain characters that can crash Emacs on Linux. There is more information
10 11 12
// here: https://github.com/flutter/flutter/issues/36704 and in the README
// in flutter_localizations/packages/lib/src/l10n.
//
13 14
// This utility is run by `gen_localizations.dart` if --overwrite is passed
// in as an option.
15 16 17 18 19 20

import 'dart:convert';
import 'dart:io';

import 'package:path/path.dart' as path;

21
import '../localizations_utils.dart';
22

23
Map<String, dynamic> _loadBundle(File file) {
24 25
  if (!FileSystemEntity.isFileSync(file.path))
    exitWithError('Unable to find input file: ${file.path}');
26
  return json.decode(file.readAsStringSync()) as Map<String, dynamic>;
27 28
}

29
void _encodeBundleTranslations(Map<String, dynamic> bundle) {
30
  for (final String key in bundle.keys) {
31 32 33 34
    // The ARB file resource "attributes" for foo are called @foo. Don't need
    // to encode them.
    if (key.startsWith('@'))
      continue;
35
    final String translation = bundle[key] as String;
36 37 38 39 40 41 42 43 44
    // Rewrite the string as a series of unicode characters in JSON format.
    // Like "\u0012\u0123\u1234".
    bundle[key] = translation.runes.map((int code) {
      final String codeString = '00${code.toRadixString(16)}';
      return '\\u${codeString.substring(codeString.length - 4)}';
    }).join();
  }
}

45
void _checkEncodedTranslations(Map<String, dynamic> encodedBundle, Map<String, dynamic> bundle) {
46 47
  bool errorFound = false;
  const JsonDecoder decoder = JsonDecoder();
48
  for (final String key in bundle.keys) {
49 50 51 52 53 54 55 56 57
    if (decoder.convert('"${encodedBundle[key]}"') != bundle[key]) {
      stderr.writeln('  encodedTranslation for $key does not match original value "${bundle[key]}"');
      errorFound = true;
    }
  }
  if (errorFound)
    exitWithError('JSON unicode translation encoding failed');
}

58
void _rewriteBundle(File file, Map<String, dynamic> bundle) {
59 60
  final StringBuffer contents = StringBuffer();
  contents.writeln('{');
61
  for (final String key in bundle.keys) {
62 63 64 65 66 67
    contents.writeln('  "$key": "${bundle[key]}"${key == bundle.keys.last ? '' : ','}');
  }
  contents.writeln('}');
  file.writeAsStringSync(contents.toString());
}

68 69 70
void encodeKnArbFiles(Directory directory) {
  final File materialArbFile = File(path.join(directory.path, 'material_kn.arb'));
  final File cupertinoArbFile = File(path.join(directory.path, 'cupertino_kn.arb'));
71

72 73
  final Map<String, dynamic> materialBundle = _loadBundle(materialArbFile);
  final Map<String, dynamic> cupertinoBundle = _loadBundle(cupertinoArbFile);
74

75 76
  _encodeBundleTranslations(materialBundle);
  _encodeBundleTranslations(cupertinoBundle);
77

78 79
  _checkEncodedTranslations(materialBundle, _loadBundle(materialArbFile));
  _checkEncodedTranslations(cupertinoBundle, _loadBundle(cupertinoArbFile));
80

81 82
  _rewriteBundle(materialArbFile, materialBundle);
  _rewriteBundle(cupertinoArbFile, cupertinoBundle);
83
}