I am trying to read a properties file in node js but I get something weird any unicode like \u00c5tg
becomes \\u00c5tg
so I have one more backslash
fs.readFile('./Language.properties', { encoding: "utf-8" }, (err, data) => {
if (!err) {
const split_string = data.split("=");
console.log(split_string)
}
});
I get :
'actions',
'"\\u00c5tg\\u00e4rder"\r\nadd-attendee',
'L\\u00e4gg till deltagare\r\nadd-event',
'L\\u00e4gg till evenemang\r\nadd-new-attendee',
the file Language.properties has :
actions=\u00c5tg\u00e4rder"
add-attendee=L\u00e4gg till deltagare
add-event=L\u00e4gg till evenemang
add-new-attendee=L\u00e4gg till deltagare
are-you-sure-you-want-to-cancel-this=\u00c4r du s\u00e4ker p\u00e5 att du vill st\u00e4lla in det h\u00e4r evenemanget och ta bort anm\u00e4lda deltagare?
are-you-sure-you-want-to-delete-this-and-remove-any-existing-attendees=\u00c4r du s\u00e4ker p\u00e5 att du vill radera det h\u00e4r evenemanget och ta bort anm\u00e4lda deltagare?
attendees=Deltagare
calendarServiceException.ATTENDEE_NOT_FOUND=Kunde inte hitta deltagare
calendarServiceException.BAD_REQUEST_CONFLICTING_RECURRENCE_VALUES="Antal f\u00f6rekomster" och "Forts\u00e4tt till" kan inte vara i samma f\u00f6rfr\u00e5gan
calendarServiceException.BAD_REQUEST_CREATEDBY_UNDEFINED=Skapad av ogiltig anv\u00e4ndare
calendarServiceException.BAD_REQUEST_EMAIL_ALREADY_REGISTERED=Den h\u00e4r epostadressen \u00e4r redan anm\u00e4ld
calendarServiceException.BAD_REQUEST_EMPTY_OR_MISSING_FIELDS=Namn eller e-postadress m\u00e5ste anges
calendarServiceException.BAD_REQUEST_ENDDATE_BEFORE_STARTDATE=Startdatum m\u00e5ste vara tidigare \u00e4n slutdatum
calendarServiceException.BAD_REQUEST_FREQUENCY_MISSING=Frekvens saknas f\u00f6r \u00e5terkommande evenemang
calendarServiceException.BAD_REQUEST_MODIFIEDBY_UNDEFINED=\u00c4ndrad av ogiltig anv\u00e4ndare
calendarServiceException.BAD_REQUEST_STARTDATE_BEFORE_PUBLICATIONDATE=Publiceringsdatum m\u00e5ste vara tidigare \u00e4n startdatum
calendarServiceException.BAD_REQUEST_VALIDATON_ERROR=Valideringsfel
calendarServiceException.CALENDAR_NOT_FOUND=Kunde inte hitta extern kalender
calendarServiceException.CUSTOMER_NOT_FOUND=Kunde inte hitta deltagare
calendarServiceException.EVENT_NOT_FOUND=Evenemanget kunde inte hittas
calendarServiceException.FORBIDDEN_ATTENDEES_EXISTS=Evenemanget eller en annan f\u00f6rekomst av \u00e5terkommande evenemang har anm\u00e4lda deltagare.
calendarServiceException.FORBIDDEN_EVENTS_EXISTS=Kunde inte ta bort deltagaren. Deltagaren \u00e4r anm\u00e4ld till evenemanget.
calendarServiceException.FORBIDDEN_MAXIMUM_ATTENDEES_REGISTERED=Evenemanget \u00e4r fullbokat.
calendarServiceException.FORBIDDEN_REGISTER_NOT_ALLOWED=Det \u00e4r inte m\u00f6jligt att anm\u00e4la sig till det h\u00e4r evenemanget.
cancel-all-occurences-in-the-serie=St\u00e4ll in alla f\u00f6rekomster i serien och ta bort anm\u00e4lda deltagare
cancel-event-title=Avbryt evenemang
cancel-this-event=St\u00e4ll in evenemanget
cancel-this-occurence-only=St\u00e4ll in endast denna f\u00f6rekomst och ta bort anm\u00e4lda deltagare
cancelled=Inst\u00e4llt
copy-emails=Kopiera e-postadresser
createdDate=Skapat
customerId.invalid=Ogiltigt kund-ID f\u00f6r evenemangsmodul
customerId.label=Kund-ID f\u00f6r evenemangsmodul
date=Datum
defaultAllowedAttendees.label=F\u00f6rvalt antal platser per deltagare
delete-all-occurences-in-the-serie=Radera alla f\u00f6rekomster i serien och ta bort anm\u00e4lda deltagare
delete-event-title=Radera evenemang
delete-this-occurence-only=Radera endast denna f\u00f6rekomst och ta bort anm\u00e4lda deltagare
email-address=E-postadress
endDate=Slutdatum
endTime=Sluttid
event-admin-audience=M\u00e5lgrupp och deltagare
event-admin-datetime=Datum och tid
event-admin-image=Bild
event-admin-location=Plats
event-end-date-before-now=Slutdatum m\u00e5ste vara senare \u00e4n idag
event-end-date-before-start-date=Startdatum m\u00e5ste vara tidigare \u00e4n slutdatum
event-is-fully-booked=Evenemanget \u00e4r fullbokat
event-picture=Bild
event-recurrence-end-date-before-start-date=Startdatum f\u00f6r \u00e5terkommande evenemang m\u00e5ste vara tidigare \u00e4n slutdatum
event-series-start-date-before-publication-date=Publiceringsdatum av \u00e5terkommande evenemang m\u00e5ste vara tidigare \u00e4n startdatum
event-series-start-date-end-time-before-series-start-date-start-time=Starttid p\u00e5 startdatum f\u00f6r \u00e5terkommande evenemang m\u00e5ste vara tidigare \u00e4n sluttid
event-start-date-before-publication-date=Publiceringsdatum m\u00e5ste vara tidigare \u00e4n startdatum
event-statistics-filename=event_statistics
event.endDate=Slutdatum
event.location.value=Plats
event.seriesInitialStartDate=Startdatum, \u00e5terkommande evenemang
event.startDate=Startdatum
event.status.ACTIVE=Aktivt
event.status.ALL=Alla
event.status.CANCELLED=Inst\u00e4llt
event.status.COMPLETED=Avslutat
event.status.DRAFT=Utkast
event.status.PLANNED=Planerat
event.status.PUBLISHED=Publicerat
event.title=Namn
export-library-statistics-link=Exportera statistik
filter=Filtrera
first-name=F\u00f6rnamn
frequency=Frekvens
full-name=Fullst\u00e4ndigt namn
imageAlt=Alt-text
invalid-number-of-attendees-error=Du kan inte anm\u00e4la detta antal deltagare.
javax.portlet.description.com_axiell_arena_liferay_modules_calendar_event_admin_web_portlet_CalendarEventAdminPortlet=Evenemang beskriver n\u00e5got som h\u00e4nder p\u00e5 biblioteket. Med den h\u00e4r portleten kan administrat\u00f6rer skapa och hantera evenemangen i Arena.
javax.portlet.display-name.com_axiell_arena_liferay_modules_calendar_event_admin_web_portlet_CalendarEventAdminPortlet=Administration av evenemang
javax.portlet.keywords.com_axiell_arena_liferay_modules_calendar_event_admin_web_portlet_CalendarEventAdminPortlet=kalender, evenemang
javax.portlet.short-title.com_axiell_arena_liferay_modules_calendar_event_admin_web_portlet_CalendarEventAdminPortlet=Administration av evenemang
javax.portlet.title.com_axiell_arena_liferay_modules_calendar_event_admin_web_portlet_CalendarEventAdminPortlet=Administration av evenemang
last-name=Efternamn
location=Plats
locationVocabularyId.invalid=Ogiltigt ID f\u00f6r platser f\u00f6r evenemang
locationVocabularyId.label=ID f\u00f6r platser f\u00f6r evenemang
modifiedDate=\u00c4ndrat
multi-day-event=Flerdagarsevenemang
no-limit=Obegr\u00e4nsat
no-recurrence=Ej \u00e5terkommande
no-registration=Ingen registrering
no-registration-needed=Ingen anm\u00e4lan kr\u00e4vs
no-week-day-selected=Ingen veckodag vald f\u00f6r det veckovis \u00e5terkommande evenemanget
not-enough-seats-available-error=Det finns inte s\u00e5 m\u00e5nga platser kvar.
nrRegistered=Antal deltagare (maximalt {0})
num-of-attendees=Antal deltagare
number-of-attendees=Faktiskt antal deltagare
number-of-seats=Antal platser
number-of-seats-per-attendee=Antal platser per deltagare
occurs-every=\u00c5terkommer varje
onDayOfMonth=p\u00e5 dag {0} i m\u00e5naden
onWeekDayOfMonth=p\u00e5 {0} {1} i m\u00e5naden
planned=Planerat
publication-date=Publiceringsdatum
publication-time=Publiceringstid
recurrence=\u00c5terkommande
recurrence-by-month-day=\u00c5terkommer varje m\u00e5nad
recurrence-end-date=Slutdatum, \u00e5terkommande evenemang
recurring-event=\u00c5terkommande evenemang
registerable=Till\u00e5t online-registrering
registrations=Registrerade deltagare
reschedule-event=\u00c4ndra evenemang
room=Lokal
select-event-image=V\u00e4lj den bild som du vill koppla till evenemanget
select-image=V\u00e4lj bild
seriesInitialEndTime=Sluttid
seriesInitialStartDate=Startdatum, \u00e5terkommande evenemang
seriesInitialStartTime=Starttid
something-unexpected-happened-please-try-again=Ett ov\u00e4ntat fel uppstod. F\u00f6rs\u00f6k igen
sort=Sortera
startDate=Startdatum
startTime=Starttid
tags=Taggar
target-audience=M\u00e5lgrupp
targetAudienceVocabularyId.invalid=Ogiltigt ID f\u00f6r m\u00e5lgrupper f\u00f6r evenemang
targetAudienceVocabularyId.label=ID f\u00f6r m\u00e5lgrupper f\u00f6r evenemang
the-text-is-copied-to-the-clipboard=E-postadresserna finns nu i dina urklipp och kan klistras in.
there-are-no-attendees=Det finns inga anm\u00e4lda deltagare
there-are-no-attendees.-you-can-add-a-category-by-clicking-the-plus-button-on-the-bottom-right-corner=Det finns inga anm\u00e4lda deltagare. Du kan l\u00e4gga till en deltagare genom att trycka p\u00e5 -knappen nere till h\u00f6ger.
there-are-no-events=Det finns inga evenemang.
there-are-no-events.-you-can-add-an-event-by-clicking-the-plus-button-on-the-bottom-right-corner=Det finns inga evenemang. Du kan skapa ett nytt evenemang genom att klicka p\u00e5 den bl\u00e5 -knappen nere till h\u00f6ger.
this-event-is-inactive-and-can-not-be-edited=Det h\u00e4r evenemanget \u00e4r inte aktivt och kan inte redigeras.
this-is-one-event-in-a-series-press-ok-to-edit-the-series-or-cancel-to-edit-the-single-event=Det h\u00e4r evenemanget tillh\u00f6r en serie. Tryck "OK" f\u00f6r att redigera hela serien eller "Avbryt" f\u00f6r att endast redigera det enskilda evenemanget.
unauthorizedException=Obeh\u00f6rig \u00e5tkomst
unpublish=Avpublicera
CodePudding user response:
The text in your Language.properties
files is literal text so the unicode in there is not unicode but text. So if you read the file with JavaScript you want to read the text and not parse the unicode, that is why it escapes it with an extra backslash.
If you want to read the unicode itself you should not use \u00c5tg but Åtg
And if you actually console.log that item it will only show 1 backslash.
CodePudding user response:
I found the following two ways to go around this :
1.First way, is just string replacing we get
const PropertiesReader = require("properties-reader");
const properties = PropertiesReader("./Language_sv.properties", 'utf-8').getAllProperties();
console.log(properties);
for(let i in properties){
let addQuotation = false;
let translation = properties[i];
if (translation) {
if (translation.startsWith('?')) {
translation = translation.replace(/^\?[^?] \? /, '');
}
if (translation.includes('\\"')) {
translation = translation.replace(/\\"/g, '""');
addQuotation = true;
}
else if (translation.includes('"')) {
translation = translation.replace(/"/g, '""');
addQuotation = true;
}
if (translation.includes(",")) {
addQuotation = true;
}
if (addQuotation) {
translation = '"' translation '"';
}
if (addQuotation) {
translation = '"' translation '"';
}
console.log (translation);
}
}
- second way better answer :
that way @VladimirM answer here : convert properties to javascript object ( bad character result)
const fs = require("fs");
const PropertiesReader = require("properties-reader");
const properties = PropertiesReader("./Language_sv.properties", 'utf-8').getAllProperties();
console.log(properties);
for(let i in properties){
properties[i] = properties[i].split('"').join('\\"')
properties[i] = decodeURIComponent(
JSON.parse('"' properties[i] '"')
)
}
console.log(properties);
nothing more than string replacing in first way , but second one @VladimirM covert property value into a string enclosed within quotes, and then parse it with JSON.parse. It parses the escaped backslash properly. And decodeURIComponent coverts unicode escape sequence to the character.