Test to ensure _kn.arb files are properly escaped (#56091)
* Run encode kn character encoding tool when gen_localizations is run * Add test that ensures *_kn.arb files are properly encoded/escaped in order to be checked in * Fix *_no.arb test to not incorrectly pass
This commit is contained in:
@@ -2,12 +2,19 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import '../test_utils.dart';
|
||||
|
||||
final String rootDirectoryPath = Directory.current.parent.path;
|
||||
|
||||
void main() {
|
||||
for (final String language in kCupertinoSupportedLanguages) {
|
||||
testWidgets('translations exist for $language', (WidgetTester tester) async {
|
||||
@@ -129,8 +136,13 @@ void main() {
|
||||
|
||||
// Regression test for https://github.com/flutter/flutter/issues/53036.
|
||||
testWidgets('`nb` uses `no` as its synonym when `nb` arb file is not present', (WidgetTester tester) async {
|
||||
final File nbCupertinoArbFile = File('lib/src/l10n/cupertino_nb.arb');
|
||||
final File noCupertinoArbFile = File('lib/src/l10n/cupertino_no.arb');
|
||||
final File nbCupertinoArbFile = File(
|
||||
path.join(rootDirectoryPath, 'lib', 'src', 'l10n', 'cupertino_nb.arb'),
|
||||
);
|
||||
final File noCupertinoArbFile = File(
|
||||
path.join(rootDirectoryPath, 'lib', 'src', 'l10n', 'cupertino_no.arb'),
|
||||
);
|
||||
|
||||
|
||||
if (noCupertinoArbFile.existsSync() && !nbCupertinoArbFile.existsSync()) {
|
||||
Locale locale = const Locale.fromSubtags(languageCode: 'no', scriptCode: null, countryCode: null);
|
||||
@@ -151,4 +163,31 @@ void main() {
|
||||
expect(localizations.cutButtonLabel, cutButtonLabelNo);
|
||||
}
|
||||
});
|
||||
|
||||
// Regression test for https://github.com/flutter/flutter/issues/36704.
|
||||
testWidgets('kn arb file should be properly Unicode escaped', (WidgetTester tester) async {
|
||||
final File file = File(
|
||||
path.join(rootDirectoryPath, 'lib', 'src', 'l10n', 'cupertino_kn.arb'),
|
||||
);
|
||||
|
||||
final Map<String, dynamic> bundle = json.decode(file.readAsStringSync()) as Map<String, dynamic>;
|
||||
|
||||
// Encodes the arb resource values if they have not already been
|
||||
// encoded.
|
||||
encodeBundleTranslations(bundle);
|
||||
|
||||
// Generates the encoded arb output file in as a string.
|
||||
final String encodedArbFile = generateArbString(bundle);
|
||||
|
||||
// After encoding the bundles, the generated string should match
|
||||
// the existing material_kn.arb.
|
||||
if (Platform.isWindows) {
|
||||
// On Windows, the character '\n' can output the two-character sequence
|
||||
// '\r\n' (and when reading the file back, '\r\n' is translated back
|
||||
// into a single '\n' character).
|
||||
expect(file.readAsStringSync().replaceAll('\r\n', '\n'), encodedArbFile);
|
||||
} else {
|
||||
expect(file.readAsStringSync(), encodedArbFile);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,12 +2,19 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import '../test_utils.dart';
|
||||
|
||||
final String rootDirectoryPath = Directory.current.parent.path;
|
||||
|
||||
void main() {
|
||||
for (final String language in kMaterialSupportedLanguages) {
|
||||
testWidgets('translations exist for $language', (WidgetTester tester) async {
|
||||
@@ -467,8 +474,12 @@ void main() {
|
||||
|
||||
// Regression test for https://github.com/flutter/flutter/issues/53036.
|
||||
testWidgets('`nb` uses `no` as its synonym when `nb` arb file is not present', (WidgetTester tester) async {
|
||||
final File nbMaterialArbFile = File('lib/src/l10n/material_nb.arb');
|
||||
final File noMaterialArbFile = File('lib/src/l10n/material_no.arb');
|
||||
final File nbMaterialArbFile = File(
|
||||
path.join(rootDirectoryPath, 'lib', 'src', 'l10n', 'material_nb.arb'),
|
||||
);
|
||||
final File noMaterialArbFile = File(
|
||||
path.join(rootDirectoryPath, 'lib', 'src', 'l10n', 'material_no.arb'),
|
||||
);
|
||||
|
||||
// No need to run test if `nb` arb file exists or if `no` arb file does not exist.
|
||||
if (noMaterialArbFile.existsSync() && !nbMaterialArbFile.existsSync()) {
|
||||
@@ -492,4 +503,33 @@ void main() {
|
||||
expect(localizations.okButtonLabel, okButtonLabelNo);
|
||||
}
|
||||
});
|
||||
|
||||
// Regression test for https://github.com/flutter/flutter/issues/36704.
|
||||
testWidgets('kn arb file should be properly Unicode escaped', (WidgetTester tester) async {
|
||||
final File file = File(
|
||||
path.join(rootDirectoryPath, 'lib', 'src', 'l10n', 'material_kn.arb'),
|
||||
);
|
||||
|
||||
final Map<String, dynamic> bundle = json.decode(
|
||||
file.readAsStringSync(),
|
||||
) as Map<String, dynamic>;
|
||||
|
||||
// Encodes the arb resource values if they have not already been
|
||||
// encoded.
|
||||
encodeBundleTranslations(bundle);
|
||||
|
||||
// Generates the encoded arb output file in as a string.
|
||||
final String encodedArbFile = generateArbString(bundle);
|
||||
|
||||
// After encoding the bundles, the generated string should match
|
||||
// the existing material_kn.arb.
|
||||
if (Platform.isWindows) {
|
||||
// On Windows, the character '\n' can output the two-character sequence
|
||||
// '\r\n' (and when reading the file back, '\r\n' is translated back
|
||||
// into a single '\n' character).
|
||||
expect(file.readAsStringSync().replaceAll('\r\n', '\n'), encodedArbFile);
|
||||
} else {
|
||||
expect(file.readAsStringSync(), encodedArbFile);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
30
packages/flutter_localizations/test/test_utils.dart
Normal file
30
packages/flutter_localizations/test/test_utils.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Encodes ARB file resource values with Unicode escapes.
|
||||
void encodeBundleTranslations(Map<String, dynamic> bundle) {
|
||||
for (final String key in bundle.keys) {
|
||||
// The ARB file resource "attributes" for foo are called @foo. Don't need
|
||||
// to encode them.
|
||||
if (key.startsWith('@'))
|
||||
continue;
|
||||
final String translation = bundle[key] as String;
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
|
||||
String generateArbString(Map<String, dynamic> bundle) {
|
||||
final StringBuffer contents = StringBuffer();
|
||||
contents.writeln('{');
|
||||
for (final String key in bundle.keys) {
|
||||
contents.writeln(' "$key": "${bundle[key]}"${key == bundle.keys.last ? '' : ','}');
|
||||
}
|
||||
contents.writeln('}');
|
||||
return contents.toString();
|
||||
}
|
||||
Reference in New Issue
Block a user