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

feat: introduce navigator.language #12322

Merged
merged 26 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
72dcc05
Add navigator.language and tests
AnInternetTroll Oct 4, 2021
c114fde
Merge github.com:AnInternetTroll/deno
AnInternetTroll Oct 4, 2021
3955a42
Add typings and a fancier function thx @crowlKats
AnInternetTroll Oct 4, 2021
4576676
Add `configurable` and `enumerable` properties and add `language` to …
AnInternetTroll Oct 5, 2021
9e35957
Re-format code
AnInternetTroll Oct 5, 2021
fcdbd92
Remove --allow-env requirement for `navigator.language`
AnInternetTroll Oct 6, 2021
cde6093
Merge branch 'main' of github.com:denoland/deno
AnInternetTroll Oct 10, 2021
e1acc7f
Move from making an op call to having a Bootstrap option
AnInternetTroll Oct 10, 2021
792e42d
Merge branch 'main' of github.com:denoland/deno
AnInternetTroll Oct 10, 2021
c63e28b
Add test for `navigator.languages` and freeze it
AnInternetTroll Oct 11, 2021
3408957
Remove debug print
AnInternetTroll Oct 11, 2021
833e51d
Set `LANGUAGE` env variable in tests
AnInternetTroll Oct 12, 2021
05a8829
Fix test to use LC_ALL instead of LANG
AnInternetTroll Oct 28, 2021
0154976
Merge github.com:denoland/deno
AnInternetTroll Oct 28, 2021
09b596e
Merge github.com:denoland/deno
AnInternetTroll Feb 27, 2022
f21e908
Replace `Navigator` with `WorkerNavigatorPrototype` where appropriate
AnInternetTroll Feb 27, 2022
e244948
Merge branch 'main' into AnInternetTroll/main
bartlomieju Sep 26, 2022
f33c85a
update to latest rusty_v8
bartlomieju Sep 30, 2022
81f4707
cleanup
bartlomieju Sep 30, 2022
683020c
Merge branch 'main' into AnInternetTroll/main
bartlomieju Oct 8, 2022
7eb1523
try to fix test
bartlomieju Oct 9, 2022
10d548e
Use LC_ALL in test instead of LANG and LANGUAGE
AnInternetTroll Oct 16, 2022
6d990ae
Format
AnInternetTroll Oct 16, 2022
bd8ee25
Merge branch 'main' into AnInternetTroll/main
bartlomieju Oct 16, 2022
c98844a
fix lint
bartlomieju Oct 16, 2022
9cec295
Merge branch 'main' into main
bartlomieju Oct 18, 2022
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
2 changes: 2 additions & 0 deletions cli/dts/lib.deno.window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ declare class Navigator {
readonly gpu: GPU;
readonly hardwareConcurrency: number;
readonly userAgent: string;
readonly language: string;
readonly languages: string[];
}

/** @category Web APIs */
Expand Down
2 changes: 2 additions & 0 deletions cli/dts/lib.deno.worker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ declare class WorkerNavigator {
readonly gpu: GPU;
readonly hardwareConcurrency: number;
readonly userAgent: string;
readonly language: string;
readonly languages: string[];
}

/** @category Web APIs */
Expand Down
1 change: 1 addition & 0 deletions cli/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ pub async fn run(
.unwrap_or(1),
debug_flag: metadata.log_level.map_or(false, |l| l == Level::Debug),
enable_testing_features: false,
locale: deno_core::v8::icu::get_language_tag(),
location: metadata.location,
no_color: !colors::use_color(),
is_tty: colors::is_tty(),
Expand Down
53 changes: 53 additions & 0 deletions cli/tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2377,6 +2377,59 @@ itest!(eval_context_throw_dom_exception {
output: "run/eval_context_throw_dom_exception.js.out",
});

#[test]
#[cfg(unix)]
fn navigator_language_unix() {
let (res, _) = util::run_and_collect_output(
true,
"run navigator_language.ts",
None,
Some(vec![("LC_ALL".to_owned(), "pl_PL".to_owned())]),
false,
);
assert_eq!(res, "pl-PL\n")
}

#[test]
fn navigator_language() {
let (res, _) = util::run_and_collect_output(
true,
"run navigator_language.ts",
None,
None,
false,
);
assert!(!res.is_empty())
}

#[test]
#[cfg(unix)]
fn navigator_languages_unix() {
let (res, _) = util::run_and_collect_output(
true,
"run navigator_languages.ts",
None,
Some(vec![
("LC_ALL".to_owned(), "pl_PL".to_owned()),
("NO_COLOR".to_owned(), "1".to_owned()),
]),
false,
);
assert_eq!(res, "[ \"pl-PL\" ]\n")
}

#[test]
fn navigator_languages() {
let (res, _) = util::run_and_collect_output(
true,
"run navigator_languages.ts",
None,
None,
false,
);
assert!(!res.is_empty())
}

/// Regression test for https://github.com/denoland/deno/issues/12740.
#[test]
fn issue12740() {
Expand Down
1 change: 1 addition & 0 deletions cli/tests/testdata/navigator_language.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(navigator.language);
1 change: 1 addition & 0 deletions cli/tests/testdata/navigator_languages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(navigator.languages);
2 changes: 2 additions & 0 deletions cli/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ pub async fn create_main_worker(
.log_level()
.map_or(false, |l| l == log::Level::Debug),
enable_testing_features: ps.options.enable_testing_features(),
locale: deno_core::v8::icu::get_language_tag(),
location: ps.options.location_flag().map(ToOwned::to_owned),
no_color: !colors::use_color(),
is_tty: colors::is_tty(),
Expand Down Expand Up @@ -534,6 +535,7 @@ fn create_web_worker_callback(
.log_level()
.map_or(false, |l| l == log::Level::Debug),
enable_testing_features: ps.options.enable_testing_features(),
locale: deno_core::v8::icu::get_language_tag(),
location: Some(args.main_module.clone()),
no_color: !colors::use_color(),
is_tty: colors::is_tty(),
Expand Down
1 change: 1 addition & 0 deletions runtime/examples/hello_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async fn main() -> Result<(), AnyError> {
cpu_count: 1,
debug_flag: false,
enable_testing_features: false,
locale: deno_core::v8::icu::get_language_tag(),
location: None,
no_color: false,
is_tty: false,
Expand Down
36 changes: 35 additions & 1 deletion runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ delete Intl.v8BreakIterator;

const navigator = webidl.createBranded(Navigator);

let numCpus, userAgent;
let numCpus, userAgent, language;

ObjectDefineProperties(Navigator.prototype, {
gpu: {
Expand All @@ -362,6 +362,22 @@ delete Intl.v8BreakIterator;
return userAgent;
},
},
language: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, NavigatorPrototype);
return language;
},
},
languages: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, NavigatorPrototype);
return [language];
},
},
});
const NavigatorPrototype = Navigator.prototype;

Expand Down Expand Up @@ -393,6 +409,22 @@ delete Intl.v8BreakIterator;
webidl.assertBranded(this, WorkerNavigatorPrototype);
return numCpus;
},
language: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return language;
},
},
languages: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return [language];
},
},
},
});
const WorkerNavigatorPrototype = WorkerNavigator.prototype;
Expand Down Expand Up @@ -705,6 +737,7 @@ delete Intl.v8BreakIterator;

numCpus = runtimeOptions.cpuCount;
userAgent = runtimeOptions.userAgent;
language = runtimeOptions.locale;

const internalSymbol = Symbol("Deno.internal");

Expand Down Expand Up @@ -793,6 +826,7 @@ delete Intl.v8BreakIterator;

location.setLocationHref(runtimeOptions.location);
numCpus = runtimeOptions.cpuCount;
language = runtimeOptions.locale;

globalThis.pollForMessages = pollForMessages;

Expand Down
1 change: 1 addition & 0 deletions runtime/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ mod tests {
cpu_count: 1,
debug_flag: false,
enable_testing_features: false,
locale: deno_core::v8::icu::get_language_tag(),
location: None,
no_color: true,
is_tty: false,
Expand Down
3 changes: 3 additions & 0 deletions runtime/worker_bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct BootstrapOptions {
pub cpu_count: usize,
pub debug_flag: bool,
pub enable_testing_features: bool,
pub locale: String,
pub location: Option<ModuleSpecifier>,
/// Sets `Deno.noColor` in JS runtime.
pub no_color: bool,
Expand Down Expand Up @@ -47,6 +48,7 @@ impl Default for BootstrapOptions {
enable_testing_features: Default::default(),
debug_flag: Default::default(),
ts_version: Default::default(),
locale: "en-EN".to_string(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like an error - there's no region EN.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Silly mistake on my part, I'll fix it

location: Default::default(),
unstable: Default::default(),
inspect: Default::default(),
Expand All @@ -63,6 +65,7 @@ impl BootstrapOptions {
"cpuCount": self.cpu_count,
"debugFlag": self.debug_flag,
"denoVersion": self.runtime_version,
"locale": self.locale,
"location": self.location,
"noColor": self.no_color,
"isTty": self.is_tty,
Expand Down