Skip to content

Commit

Permalink
Implement i18n.getSystemUILanguage and i18n.getPreferredSystemLanguages.
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=280586

Reviewed by NOBODY (OOPS!).

WECG issue: w3c/webextensions#252
WECG proposal: w3c/webextensions#569

Also changed the i18n APIs to return `"und"` instead of `undefined` when there is no language code.

* Source/WebKit/Platform/spi/Cocoa/FoundationSPI.h:
* Source/WebKit/Shared/Extensions/WebExtensionUtilities.mm:
(WebKit::toWebAPI): Return "und" where then is no language code.
* Source/WebKit/WebProcess/Extensions/API/Cocoa/WebExtensionAPILocalizationCocoa.mm:
(WebKit::WebExtensionAPILocalization::getPreferredSystemLanguages):
(WebKit::WebExtensionAPILocalization::getSystemUILanguage):
* Source/WebKit/WebProcess/Extensions/API/WebExtensionAPILocalization.h:
* Source/WebKit/WebProcess/Extensions/Interfaces/WebExtensionAPILocalization.idl:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebExtensionAPILocalization.mm:
(TestWebKitAPI::localeStringInWebExtensionFormat): Return "und".
(TestWebKitAPI::TEST(WKWebExtensionAPILocalization, i18n)):
(TestWebKitAPI::TEST(WKWebExtensionAPILocalization, i18nWithFallback)):
(TestWebKitAPI::TEST(WKWebExtensionAPILocalization, i18nWithoutMessages)):
(TestWebKitAPI::TEST(WKWebExtensionAPILocalization, i18nWithoutDefaultLocale)):
  • Loading branch information
carlosjeurissen authored and xeenon committed Oct 3, 2024
1 parent 4e612c4 commit f494ddc
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Source/WebKit/Platform/spi/Cocoa/FoundationSPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#if USE(APPLE_INTERNAL_SDK)

#import <Foundation/NSExtension.h>
#import <Foundation/NSLocale_Private.h>
#import <Foundation/NSPrivateDecls.h>

#if PLATFORM(IOS_FAMILY)
Expand Down Expand Up @@ -59,6 +60,10 @@ NS_ASSUME_NONNULL_BEGIN
@property (copy, NS_NONATOMIC_IOSONLY) void (^requestInterruptionBlock)(id <NSCopying>);
@end

@interface NSLocale ()
+ (NSString *)_deviceLanguage;
@end

NS_ASSUME_NONNULL_END

#endif // USE(APPLE_INTERNAL_SDK)
4 changes: 2 additions & 2 deletions Source/WebKit/Shared/Extensions/WebExtensionUtilities.mm
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ JSObjectRef toJSRejectedPromise(JSContextRef context, NSString *callingAPIName,

NSString *toWebAPI(NSLocale *locale)
{
if (!locale.languageCode)
return nil;
if (!locale.languageCode.length)
return @"und";

if (locale.countryCode.length)
return [NSString stringWithFormat:@"%@-%@", locale.languageCode, locale.countryCode];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#import "WebExtensionAPILocalization.h"

#import "CocoaHelpers.h"
#import "FoundationSPI.h"
#import "Logging.h"
#import "MessageSenderInlines.h"
#import "WebExtensionContextMessages.h"
Expand Down Expand Up @@ -88,6 +89,16 @@
callback->call(acceptLanguages.array);
}

void WebExtensionAPILocalization::getPreferredSystemLanguages(Ref<WebExtensionCallbackHandler>&& callback)
{
callback->call(NSLocale.preferredLanguages);
}

void WebExtensionAPILocalization::getSystemUILanguage(Ref<WebExtensionCallbackHandler>&& callback)
{
callback->call(NSLocale._deviceLanguage);
}

} // namespace WebKit

#endif // ENABLE(WK_WEB_EXTENSIONS)
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class WebExtensionAPILocalization : public WebExtensionAPIObject, public JSWebEx
NSString *getMessage(NSString* messageName, id substitutions);
NSString *getUILanguage();
void getAcceptLanguages(Ref<WebExtensionCallbackHandler>&&);
void getPreferredSystemLanguages(Ref<WebExtensionCallbackHandler>&&);
void getSystemUILanguage(Ref<WebExtensionCallbackHandler>&&);
};

} // namespace WebKit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@

void getAcceptLanguages([Optional, CallbackHandler] function callback);

void getPreferredSystemLanguages([Optional, CallbackHandler] function callback);

void getSystemUILanguage([Optional, CallbackHandler] function callback);
};
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#import "CocoaHelpers.h"
#import "WebExtensionUtilities.h"

@interface NSLocale ()
+ (NSString *)_deviceLanguage;
@end

static NSString * const messageKey = @"message";
static NSString * const placeholdersKey = @"placeholders";
static NSString * const placeholderDictionaryContentKey = @"content";
Expand Down Expand Up @@ -77,15 +81,16 @@

static NSString *localeStringInWebExtensionFormat(NSLocale *locale)
{
if (!locale.languageCode)
return @"";
if (!locale.languageCode.length)
return @"und";

if (locale.countryCode.length)
return [NSString stringWithFormat:@"%@-%@", locale.languageCode, locale.countryCode];
return locale.languageCode;
}

static NSString *currentLocaleString = localeStringInWebExtensionFormat(currentLocale);
static NSString *currentSystemLocaleString = NSLocale._deviceLanguage;

static auto *baseURLString = @"test-extension://76C788B8-3374-400D-8259-40E5B9DF79D3";

Expand Down Expand Up @@ -122,10 +127,13 @@
}

auto *acceptedLanguagesString = Util::constructJSArrayOfStrings(acceptedLanguages.array);
auto *preferredLanguagesString = Util::constructJSArrayOfStrings(preferredLocaleIdentifiers);

auto *backgroundScript = Util::constructScript(@[
[NSString stringWithFormat:@"const acceptedLanguages = %@", acceptedLanguagesString],
[NSString stringWithFormat:@"const preferredLanguages = %@", preferredLanguagesString],
[NSString stringWithFormat:@"const currentUILanguage = '%@'", currentLocaleString],
[NSString stringWithFormat:@"const currentSystemUILanguage = '%@'", currentSystemLocaleString],
[NSString stringWithFormat:@"const textDirection = '%@'", textDirection],
[NSString stringWithFormat:@"const reversedTextDirection = '%@'", reversedTextDirection],
[NSString stringWithFormat:@"const startEdge = '%@'", startEdge],
Expand All @@ -140,9 +148,14 @@
@"browser.test.assertEq(browser.i18n.getMessage('@@bidi_end_edge'), endEdge)",
@"browser.test.assertEq(browser.i18n.getMessage('unknown_message'), '')",

@"browser.test.assertFalse(currentUILanguage === 'und')",
@"browser.test.assertEq(browser.i18n.getUILanguage(), currentUILanguage)",
@"browser.test.assertDeepEq(await browser.i18n.getAcceptLanguages(), acceptedLanguages)",

@"browser.test.assertFalse(currentSystemUILanguage === 'und')",
@"browser.test.assertEq(await browser.i18n.getSystemUILanguage(), currentSystemUILanguage)",
@"browser.test.assertDeepEq(await browser.i18n.getPreferredSystemLanguages(), preferredLanguages)",

@"browser.test.notifyPass()",
]);

Expand All @@ -166,10 +179,13 @@
}

auto *acceptedLanguagesString = Util::constructJSArrayOfStrings(acceptedLanguages.array);
auto *preferredLanguagesString = Util::constructJSArrayOfStrings(preferredLocaleIdentifiers);

auto *backgroundScript = Util::constructScript(@[
[NSString stringWithFormat:@"const acceptedLanguages = %@", acceptedLanguagesString],
[NSString stringWithFormat:@"const preferredLanguages = %@", preferredLanguagesString],
[NSString stringWithFormat:@"const currentUILanguage = '%@'", currentLocaleString],
[NSString stringWithFormat:@"const currentSystemUILanguage = '%@'", currentSystemLocaleString],
[NSString stringWithFormat:@"const textDirection = '%@'", textDirection],
[NSString stringWithFormat:@"const reversedTextDirection = '%@'", reversedTextDirection],
[NSString stringWithFormat:@"const startEdge = '%@'", startEdge],
Expand All @@ -186,9 +202,14 @@
@"browser.test.assertEq(browser.i18n.getMessage('@@bidi_end_edge'), endEdge)",
@"browser.test.assertEq(browser.i18n.getMessage('unknown_message'), '')",

@"browser.test.assertFalse(currentUILanguage === 'und')",
@"browser.test.assertEq(browser.i18n.getUILanguage(), currentUILanguage)",
@"browser.test.assertDeepEq(await browser.i18n.getAcceptLanguages(), acceptedLanguages)",

@"browser.test.assertFalse(currentSystemUILanguage === 'und')",
@"browser.test.assertEq(await browser.i18n.getSystemUILanguage(), currentSystemUILanguage)",
@"browser.test.assertDeepEq(await browser.i18n.getPreferredSystemLanguages(), preferredLanguages)",

@"browser.test.notifyPass()",
]);

Expand Down Expand Up @@ -249,10 +270,13 @@
}

auto *acceptedLanguagesString = Util::constructJSArrayOfStrings(acceptedLanguages.array);
auto *preferredLanguagesString = Util::constructJSArrayOfStrings(preferredLocaleIdentifiers);

auto *backgroundScript = Util::constructScript(@[
[NSString stringWithFormat:@"const acceptedLanguages = %@", acceptedLanguagesString],
[NSString stringWithFormat:@"const preferredLanguages = %@", preferredLanguagesString],
[NSString stringWithFormat:@"const currentUILanguage = '%@'", currentLocaleString],
[NSString stringWithFormat:@"const currentSystemUILanguage = '%@'", currentSystemLocaleString],

@"browser.test.assertEq(browser.i18n.getMessage('@@extension_id'), '76C788B8-3374-400D-8259-40E5B9DF79D3')",
@"browser.test.assertEq(browser.i18n.getMessage('@@ui_locale'), '')",
Expand All @@ -262,9 +286,14 @@
@"browser.test.assertEq(browser.i18n.getMessage('@@bidi_end_edge'), '')",
@"browser.test.assertEq(browser.i18n.getMessage('unknown_message'), '')",

@"browser.test.assertFalse(currentUILanguage === 'und')",
@"browser.test.assertEq(browser.i18n.getUILanguage(), currentUILanguage)",
@"browser.test.assertDeepEq(await browser.i18n.getAcceptLanguages(), acceptedLanguages)",

@"browser.test.assertFalse(currentSystemUILanguage === 'und')",
@"browser.test.assertEq(await browser.i18n.getSystemUILanguage(), currentSystemUILanguage)",
@"browser.test.assertDeepEq(await browser.i18n.getPreferredSystemLanguages(), preferredLanguages)",

@"browser.test.notifyPass()",
]);

Expand Down Expand Up @@ -302,10 +331,13 @@
};

auto *acceptedLanguagesString = Util::constructJSArrayOfStrings(acceptedLanguages.array);
auto *preferredLanguagesString = Util::constructJSArrayOfStrings(preferredLocaleIdentifiers);

auto *backgroundScript = Util::constructScript(@[
[NSString stringWithFormat:@"const acceptedLanguages = %@", acceptedLanguagesString],
[NSString stringWithFormat:@"const preferredLanguages = %@", preferredLanguagesString],
[NSString stringWithFormat:@"const currentUILanguage = '%@'", currentLocaleString],
[NSString stringWithFormat:@"const currentSystemUILanguage = '%@'", currentSystemLocaleString],

@"browser.test.assertEq(browser.i18n.getMessage('@@extension_id'), '76C788B8-3374-400D-8259-40E5B9DF79D3')",
@"browser.test.assertEq(browser.i18n.getMessage('@@ui_locale'), '')",
Expand All @@ -315,9 +347,14 @@
@"browser.test.assertEq(browser.i18n.getMessage('@@bidi_end_edge'), '')",
@"browser.test.assertEq(browser.i18n.getMessage('unknown_message'), '')",

@"browser.test.assertFalse(currentUILanguage === 'und')",
@"browser.test.assertEq(browser.i18n.getUILanguage(), currentUILanguage)",
@"browser.test.assertDeepEq(await browser.i18n.getAcceptLanguages(), acceptedLanguages)",

@"browser.test.assertFalse(currentSystemUILanguage === 'und')",
@"browser.test.assertEq(await browser.i18n.getSystemUILanguage(), currentSystemUILanguage)",
@"browser.test.assertDeepEq(await browser.i18n.getPreferredSystemLanguages(), preferredLanguages)",

@"browser.test.notifyPass()",
]);

Expand Down

0 comments on commit f494ddc

Please sign in to comment.