mirror of
https://github.com/flutter/samples.git
synced 2025-11-14 03:19:06 +00:00
Merge in the flutter gallery (#176)
This commit is contained in:
13
gallery/l10n_cli/.gitignore
vendored
Normal file
13
gallery/l10n_cli/.gitignore
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
# Files and directories created by pub
|
||||
.dart_tool/
|
||||
.packages
|
||||
# Remove the following pattern if you wish to check in your lock file
|
||||
pubspec.lock
|
||||
|
||||
# Conventional directory for build outputs
|
||||
build/
|
||||
|
||||
# Directory created by dartdoc
|
||||
doc/api/
|
||||
|
||||
.DS_Store
|
||||
3
gallery/l10n_cli/CHANGELOG.md
Normal file
3
gallery/l10n_cli/CHANGELOG.md
Normal file
@@ -0,0 +1,3 @@
|
||||
## 1.0.0
|
||||
|
||||
- Initial version, created by Stagehand
|
||||
4
gallery/l10n_cli/README.md
Normal file
4
gallery/l10n_cli/README.md
Normal file
@@ -0,0 +1,4 @@
|
||||
A command-line application that converts .arb files to .xml files for translation consumption.
|
||||
|
||||
Created from templates made available by Stagehand under a BSD-style
|
||||
[license](https://github.com/dart-lang/stagehand/blob/master/LICENSE).
|
||||
39
gallery/l10n_cli/analysis_options.yaml
Normal file
39
gallery/l10n_cli/analysis_options.yaml
Normal file
@@ -0,0 +1,39 @@
|
||||
# Defines a default set of lint rules enforced for
|
||||
# projects at Google. For details and rationale,
|
||||
# see https://github.com/dart-lang/pedantic#enabled-lints.
|
||||
include: package:pedantic/analysis_options.yaml
|
||||
|
||||
# For lint rules and documentation, see http://dart-lang.github.io/linter/lints.
|
||||
# Uncomment to specify additional rules.
|
||||
# linter:
|
||||
# rules:
|
||||
# - camel_case_types
|
||||
|
||||
analyzer:
|
||||
# exclude:
|
||||
# - path/to/excluded/files/**
|
||||
|
||||
linter:
|
||||
rules:
|
||||
- avoid_types_on_closure_parameters
|
||||
- avoid_void_async
|
||||
- await_only_futures
|
||||
- camel_case_types
|
||||
- cancel_subscriptions
|
||||
- close_sinks
|
||||
- constant_identifier_names
|
||||
- control_flow_in_finally
|
||||
- empty_statements
|
||||
- hash_and_equals
|
||||
- implementation_imports
|
||||
- non_constant_identifier_names
|
||||
- package_api_docs
|
||||
- package_names
|
||||
- package_prefixed_library_names
|
||||
- test_types_in_equals
|
||||
- throw_in_finally
|
||||
- unnecessary_brace_in_string_interps
|
||||
- unnecessary_getters_setters
|
||||
- unnecessary_new
|
||||
- unnecessary_statements
|
||||
- directives_ordering
|
||||
9
gallery/l10n_cli/bin/main.dart
Normal file
9
gallery/l10n_cli/bin/main.dart
Normal file
@@ -0,0 +1,9 @@
|
||||
// Copyright 2019 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:l10n_cli/l10n_cli.dart' as l10n_cli;
|
||||
|
||||
main(List<String> arguments) async {
|
||||
await l10n_cli.englishArbsToXmls();
|
||||
}
|
||||
144
gallery/l10n_cli/lib/l10n_cli.dart
Normal file
144
gallery/l10n_cli/lib/l10n_cli.dart
Normal file
@@ -0,0 +1,144 @@
|
||||
// Copyright 2019 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
const _l10nDir = '../gallery/lib/l10n';
|
||||
const _intlHeader = '''
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
This file was automatically generated.
|
||||
Please do not edit it manually.
|
||||
It was based on gallery/lib/src/l10n/intl_en_US.arb.
|
||||
-->
|
||||
<resources>
|
||||
''';
|
||||
|
||||
const _pluralSuffixes = <String>[
|
||||
'Zero',
|
||||
'One',
|
||||
'Two',
|
||||
'Few',
|
||||
'Many',
|
||||
'Other',
|
||||
];
|
||||
|
||||
String _escapeXml(String xml) {
|
||||
return xml
|
||||
.replaceAll('&', '&')
|
||||
.replaceAll('"', '"')
|
||||
.replaceAll("'", ''')
|
||||
.replaceAll('>', '>')
|
||||
.replaceAll('<', '<');
|
||||
}
|
||||
|
||||
/// Processes the XML files.
|
||||
Future<void> englishArbsToXmls() async {
|
||||
await generateXmlFromArb(
|
||||
inputArb: File('$_l10nDir/intl_en_US.arb'),
|
||||
outputXml: File('$_l10nDir/intl_en_US.xml'),
|
||||
xmlHeader: _intlHeader,
|
||||
);
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
Future<void> generateXmlFromArb({
|
||||
File inputArb,
|
||||
File outputXml,
|
||||
String xmlHeader,
|
||||
}) async {
|
||||
final Map<String, dynamic> bundle = jsonDecode(await inputArb.readAsString());
|
||||
String translationFor(String key) {
|
||||
assert(bundle[key] != null);
|
||||
return _escapeXml(bundle[key]);
|
||||
}
|
||||
|
||||
final xml = StringBuffer(xmlHeader);
|
||||
|
||||
for (String key in bundle.keys) {
|
||||
if (key == '@@last_modified') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!key.startsWith('@')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final resourceId = key.substring(1);
|
||||
final name = _escapeXml(resourceId);
|
||||
final Map<String, dynamic> metaInfo = bundle[key];
|
||||
assert(metaInfo != null && metaInfo['description'] != null);
|
||||
var description = _escapeXml(metaInfo['description']);
|
||||
|
||||
if (metaInfo.containsKey('plural')) {
|
||||
// Generate a plurals resource element formatted like this:
|
||||
// <plurals
|
||||
// name="dartVariableName"
|
||||
// description="description">
|
||||
// <item
|
||||
// quantity="other"
|
||||
// >%d translation</item>
|
||||
// ... items for quantities one, two, etc.
|
||||
// </plurals>
|
||||
final quantityVar = "\$${metaInfo['plural']}";
|
||||
description = description.replaceAll('\$$quantityVar', '%d');
|
||||
xml.writeln(' <plurals');
|
||||
xml.writeln(' name="$name"');
|
||||
xml.writeln(' description="$description">');
|
||||
for (String suffix in _pluralSuffixes) {
|
||||
final pluralKey = '$resourceId$suffix';
|
||||
if (bundle.containsKey(pluralKey)) {
|
||||
final translation =
|
||||
translationFor(pluralKey).replaceFirst(quantityVar, '%d');
|
||||
xml.writeln(' <item');
|
||||
xml.writeln(' quantity="${suffix.toLowerCase()}"');
|
||||
xml.writeln(' >$translation</item>');
|
||||
}
|
||||
}
|
||||
xml.writeln(' </plurals>');
|
||||
} else if (metaInfo.containsKey('parameters')) {
|
||||
// Generate a parameterized string resource element formatted like this:
|
||||
// <string
|
||||
// name="dartVariableName"
|
||||
// description="string description"
|
||||
// >string %1$s %2$s translation</string>
|
||||
// The translated string's original $vars, which must be listed in its
|
||||
// description's 'parameters' value, are replaced with printf positional
|
||||
// string arguments, like "%1$s".
|
||||
var translation = translationFor(resourceId);
|
||||
assert(metaInfo['parameters'].trim().isNotEmpty);
|
||||
final parameters = metaInfo['parameters']
|
||||
.split(',')
|
||||
.map<String>((s) => s.trim())
|
||||
.toList();
|
||||
var index = 1;
|
||||
for (String parameter in parameters) {
|
||||
translation = translation.replaceAll('\$$parameter', '%$index\$s');
|
||||
description = description.replaceAll('\$$parameter', '%$index\$s');
|
||||
index += 1;
|
||||
}
|
||||
xml.writeln(' <string');
|
||||
xml.writeln(' name="$name"');
|
||||
xml.writeln(' description="$description"');
|
||||
xml.writeln(' >$translation</string>');
|
||||
} else {
|
||||
// Generate a string resource element formatted like this:
|
||||
// <string
|
||||
// name="dartVariableName"
|
||||
// description="string description"
|
||||
// >string translation</string>
|
||||
final translation = translationFor(resourceId);
|
||||
xml.writeln(' <string');
|
||||
xml.writeln(' name="$name"');
|
||||
xml.writeln(' description="$description"');
|
||||
xml.writeln(' >$translation</string>');
|
||||
}
|
||||
}
|
||||
xml.writeln('</resources>');
|
||||
|
||||
await outputXml.writeAsString(xml.toString());
|
||||
}
|
||||
11
gallery/l10n_cli/pubspec.yaml
Normal file
11
gallery/l10n_cli/pubspec.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
name: l10n_cli
|
||||
description: A sample command-line application.
|
||||
# version: 1.0.0
|
||||
# author: Rami Abou Ghanem <raboughanem@google.com>
|
||||
|
||||
environment:
|
||||
sdk: '>=2.2.0 <3.0.0'
|
||||
|
||||
dev_dependencies:
|
||||
pedantic: 1.8.0
|
||||
test: ^1.0.0
|
||||
Reference in New Issue
Block a user