Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove the dependency on package:web #931

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkgs/intl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.20.2
* Remove the dependency on `package:http`.
* Remove the dependency on `package:web`.

## 0.20.1
* Upgrade `package:web` dependency constraint to `1.1.0`, fixes issue
[#916](https://github.com/dart-lang/i18n/issues/916).
Expand Down
6 changes: 3 additions & 3 deletions pkgs/intl/lib/date_symbol_data_http_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export 'src/data/dates/locale_list.dart';
/// The [url] parameter should end with a "/". For example,
/// "http://localhost:8000/dates/"
Future<void> initializeDateFormatting(String locale, String url) {
//Initialize symbols
// Initialize symbols
var symbolReader = HttpRequestDataReader('${url}symbols/');
LazyLocaleData symbolsInitializer() => LazyLocaleData(
symbolReader,
Expand All @@ -29,7 +29,7 @@ Future<void> initializeDateFormatting(String locale, String url) {
);
initializeDateSymbols(symbolsInitializer);

//Initialize patterns
// Initialize patterns
var patternsReader = HttpRequestDataReader('${url}patterns/');
LazyLocaleData patternsInitializer() => LazyLocaleData(
patternsReader,
Expand All @@ -43,7 +43,7 @@ Future<void> initializeDateFormatting(String locale, String url) {
availableLocalesForDateFormatting.contains,
)!;

//Initialize locale for both symbols and patterns
/// Initialize locale for both symbols and patterns.
Future<List<void>> initLocale(
LazyLocaleData symbols,
LazyLocaleData patterns,
Expand Down
23 changes: 12 additions & 11 deletions pkgs/intl/lib/intl_browser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@

/// This provides facilities for Internationalization that are only available
/// when running in the web browser. You should import only one of this or
/// intl_standalone.dart. Right now the only thing provided here is the
/// ability to find the default locale from the browser.
/// intl_standalone.dart. Right now the only thing provided here is the ability
/// to find the default locale from the browser.
library;

import 'package:web/web.dart';
import 'intl.dart';
import 'src/web.dart';

// TODO(alanknight): The need to do this by forcing the user to specially
// import a particular library is a horrible hack, only done because there
// seems to be no graceful way to do this at all. Either mirror access on
// dart2js or the ability to do spawnUri in the browser would be promising
// as ways to get rid of this requirement.
/// Find the system locale, accessed as window.navigator.language, and
/// set it as the default for internationalization operations in the
/// [Intl.systemLocale] variable.
// TODO(alanknight): The need to do this by forcing the user to specially import
// a particular library is a horrible hack, only done because there seems to be
// no graceful way to do this at all. Either mirror access on dart2js or the
// ability to do spawnUri in the browser would be promising as ways to get rid
// of this requirement.

/// Find the system locale, accessed as window.navigator.language, and set it as
/// the default for internationalization operations in the [Intl.systemLocale]
/// variable.
Future<String> findSystemLocale() {
Intl.systemLocale = Intl.canonicalizedLocale(window.navigator.language);
return Future.value(Intl.systemLocale);
Expand Down
1 change: 1 addition & 0 deletions pkgs/intl/lib/intl_standalone.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
library;

import 'dart:io';

import 'intl.dart';

// TODO(alanknight): The need to do this by forcing the user to specially
Expand Down
1 change: 1 addition & 0 deletions pkgs/intl/lib/message_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
library;

import 'dart:collection';

import 'intl.dart';

/// **MessageFormat grammar:**
Expand Down
1 change: 1 addition & 0 deletions pkgs/intl/lib/src/file_data_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ library;
import 'dart:io';

import 'package:path/path.dart';

import 'intl_helpers.dart';

class FileDataReader implements LocaleDataReader {
Expand Down
26 changes: 13 additions & 13 deletions pkgs/intl/lib/src/http_request_data_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// This contains a reader that accesses data using the HttpRequest
/// facility, and thus works only in the web browser.
/// This contains a reader that accesses data using the HttpRequest facility,
/// and thus works only in the web browser.
library;

import 'dart:async';
import 'package:http/http.dart';
import 'dart:js_interop';

import 'intl_helpers.dart';
import 'web.dart';

class HttpRequestDataReader implements LocaleDataReader {
/// The base url from which we read the data.
String url;
final String url;

HttpRequestDataReader(this.url);

@override
Future<String> read(String locale) {
final Client client = Client();
return _getString('$url$locale.json', client).timeout(
return _getString('$url$locale.json').timeout(
Duration(seconds: 5),
onTimeout: () {
client.close();
throw TimeoutException('Timeout while reading $locale');
},
);
}

Future<String> _getString(String url, Client client) async {
final response = await client.get(Uri.parse(url));
Future<String> _getString(String url) async {
final response = await window.fetch(url.toJS).toDart;

if ((response.statusCode >= 200 && response.statusCode < 300) ||
response.statusCode == 0 ||
response.statusCode == 304) {
return response.body;
if ((response.status >= 200 && response.status < 300) ||
response.status == 0 ||
response.status == 304) {
return (await response.text().toDart).toDart;
} else {
throw Exception('Failed to load $url');
}
Expand Down
1 change: 1 addition & 0 deletions pkgs/intl/lib/src/intl/number_format_parser.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. 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:math';

import 'package:intl/number_symbols.dart';
Expand Down
1 change: 1 addition & 0 deletions pkgs/intl/lib/src/lazy_locale_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
library;

import 'dart:convert';

import 'intl_helpers.dart';

/// This implements the very basic map-type operations which are used
Expand Down
30 changes: 30 additions & 0 deletions pkgs/intl/lib/src/web.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// Note: the contents of this file have been inlined from package:web/web.dart.
// We may periodically want to re-sync this, and / or look there when necessary
// to expose additional API.

import 'dart:js_interop';

@JS()
external Window get window;

extension type Window._(JSObject _) implements JSObject {
external Navigator get navigator;

external JSPromise<Response> fetch(
JSAny /*RequestInfo*/ input, [
JSObject /*RequestInit*/ init,
]);
}

extension type Navigator._(JSObject _) implements JSObject {
external String get language;
}

extension type Response._(JSObject _) implements JSObject {
external int get status;
external JSPromise<JSString> text();
}
8 changes: 3 additions & 5 deletions pkgs/intl/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: intl
version: 0.20.1
version: 0.20.2
description: >-
Contains code to deal with internationalized/localized messages, date and
number formatting and parsing, bi-directional text, and other
Expand All @@ -16,14 +16,12 @@ environment:

dependencies:
clock: ^1.1.0
http: ^1.0.0
meta: ^1.0.2
meta: ^1.3.0
path: ^1.8.0
web: ^1.1.0

dev_dependencies:
benchmark_harness: ^2.2.0
ffi: ^2.1.3
fixnum: ^1.0.0
lints: ^5.0.0
test: ^1.16.0
test: ^1.16.6
Loading