encode_kn_arb_files.dart 3.48 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This program 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
// here: https://github.com/flutter/flutter/issues/36704 and in the README
// in flutter_localizations/packages/lib/src/l10n.
//
// This app needs to be run by hand when material_kn.arb or cupertino_kn.arb
// have been updated.
//
// ## Usage
//
// Run this program from the root of the git repository.
//
// ```
21
// dart dev/tools/localization/bin/encode_kn_arb_files.dart
22 23 24 25 26 27 28 29
// ```

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

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

30
import '../localizations_utils.dart';
31 32 33 34

Map<String, dynamic> loadBundle(File file) {
  if (!FileSystemEntity.isFileSync(file.path))
    exitWithError('Unable to find input file: ${file.path}');
35
  return json.decode(file.readAsStringSync()) as Map<String, dynamic>;
36 37 38
}

void encodeBundleTranslations(Map<String, dynamic> bundle) {
39
  for (final String key in bundle.keys) {
40 41 42 43
    // The ARB file resource "attributes" for foo are called @foo. Don't need
    // to encode them.
    if (key.startsWith('@'))
      continue;
44
    final String translation = bundle[key] as String;
45 46 47 48 49 50 51 52 53 54 55 56
    // 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();
  }
}

void checkEncodedTranslations(Map<String, dynamic> encodedBundle, Map<String, dynamic> bundle) {
  bool errorFound = false;
  const JsonDecoder decoder = JsonDecoder();
57
  for (final String key in bundle.keys) {
58 59 60 61 62 63 64 65 66 67 68 69
    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');
}

void rewriteBundle(File file, Map<String, dynamic> bundle) {
  final StringBuffer contents = StringBuffer();
  contents.writeln('{');
70
  for (final String key in bundle.keys) {
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    contents.writeln('  "$key": "${bundle[key]}"${key == bundle.keys.last ? '' : ','}');
  }
  contents.writeln('}');
  file.writeAsStringSync(contents.toString());
}

Future<void> main(List<String> rawArgs) async {
  checkCwdIsRepoRoot('encode_kn_arb_files');

  final String l10nPath = path.join('packages', 'flutter_localizations', 'lib', 'src', 'l10n');
  final File materialArbFile = File(path.join(l10nPath, 'material_kn.arb'));
  final File cupertinoArbFile = File(path.join(l10nPath, 'cupertino_kn.arb'));

  final Map<String, dynamic> materialBundle = loadBundle(materialArbFile);
  final Map<String, dynamic> cupertinoBundle = loadBundle(cupertinoArbFile);

  encodeBundleTranslations(materialBundle);
  encodeBundleTranslations(cupertinoBundle);

  checkEncodedTranslations(materialBundle, loadBundle(materialArbFile));
  checkEncodedTranslations(cupertinoBundle, loadBundle(cupertinoArbFile));

  rewriteBundle(materialArbFile, materialBundle);
  rewriteBundle(cupertinoArbFile, cupertinoBundle);
}