-
Notifications
You must be signed in to change notification settings - Fork 15
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
Unicode support in format strings #5
base: release
Are you sure you want to change the base?
Conversation
This change fixes an issue where the Datetime Format GUI fails to load with the error 'Failed to convert locale string to UTF8: Invalid byte sequence in conversion input' if unicode characters are entered in any of the format strings. Date().toLocaleFormat(format) only accepts ascii characters in the format string. This change works around that limitation by escaping non-ascii characters in the format string before passing the string to toLocaleFormat(), then escaping the result in case non-ascii characters are in the localized string, and finally unescaping the resulting string by passing it to JSON.parse.
src/Utilities.js
Outdated
@@ -12,7 +12,13 @@ const Gtk = imports.gi.Gtk; | |||
/// @return {string} Datetime representation of format, or format if the conversion fails, or datetime representation of defaultFormat, or blank. | |||
/// | |||
function dateTimeFormat(format, defaultFormat) { | |||
return (format && new Date().toLocaleFormat(format) || format) || defaultFormat && new Date().toLocaleFormat(defaultFormat) || ""; | |||
function escapeUnicode(str) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this function out and add comments like the other functions in this file? And could you also rename the 'str' parameter to 'string'?
src/Utilities.js
Outdated
}); | ||
} | ||
|
||
return JSON.parse('"' + escapeUnicode((format && new Date().toLocaleFormat(escapeUnicode(format)) || format) || defaultFormat && new Date().toLocaleFormat(escapeUnicode(defaultFormat)) || "").replace('"', '\\"') + '"'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wanted to test this before I merge it, want to make sure all edge cases are accounted for.
Thanks Daniel. I did some edge case testing just now on this and found this doesn't handle " and \ characters well, need to escape them to make JSON.parse happy. I will push an update today with these fixes and your requested changes. |
This change expands on the previous unicode fix to also escape \ and " characters that are problematic for JSON.parse, and make formatting changes. escapeJson() includes escaping problematic ascii characters and unicode, while escapeUnicode() escapes only unicode characters. escapeJson() should be run only once on the raw format string to avoid over-escaping, and the escapeUnicode() must be run after toLocaleFormat() because toLocaleFormat() can return additional unicode characters in languages like Japanese.
Just pushed an update that addresses your feedback and fixes the " and \ issues. |
This change fixes an issue where the Datetime Format GUI fails to load with the error 'Failed to convert locale string to UTF8: Invalid byte sequence in conversion input' if unicode characters are entered in any of the format strings.
Date().toLocaleFormat(format) only accepts ascii characters in the format string. This change works around that limitation by escaping non-ascii characters in the format string before passing the string to toLocaleFormat(), then escaping the result in case non-ascii characters are in the localized string, and finally unescaping the resulting string by passing it to JSON.parse.