slang-i18n / slang

Type-safe i18n for Dart and Flutter
https://pub.dev/packages/slang
MIT License
459 stars 39 forks source link

Newlines are lost when overriding translations #195

Closed lohnn closed 7 months ago

lohnn commented 7 months ago

Describe the bug We have a few texts in the app that are multi-line. Generating and using them works fine in the app. When we try to override the translation to update the text, the new-lines are escaped out and shows up as literal \n.

To Reproduce Steps to reproduce the behavior:

  1. Create translation file that contains newlines in the texts
  2. Generate and run in the app
  3. Override the texts that contains newlines
  4. See error

Properly generated code works as expected:

{
  "customerService": {
    "title": "Customer service",
    "openHours": "Office: closed\nChat: All days 8-23"
  }
}

image

Overriding text escapes the newlines \n:

final translation = {
  'customerService': {
    'title': 'Customer service',
    'openHours': 'Office: Week days 7-23\nChat: All days 8-23',
  },
};
LocaleSettings.overrideTranslationsFromMap(
  locale: AppLocale.en,
  isFlatMap: false,
  map: translation,
);

image

The same issue occurs for both LocaleSettings.overrideTranslations and LocaleSettings.overrideTranslationsFromMap

Expected behavior Newlines show up as expected in the app, such as: image

Tienisto commented 7 months ago

I cannot reproduce this error. Be aware that JSON supports \n natively so you might accidentally escape this as an input to the overrides

lohnn commented 7 months ago

I could try to create a minimal reproducible repo and link it here if it would help? :)

Tienisto commented 7 months ago

What file type do you use? I can see inconsistencies when parsing YAML (\n are lost) and CSV (\n are escaped)

lohnn commented 7 months ago

I've tested using JSON as well as using overrideTranslationsFromMap, which takes a Dart map.

Tienisto commented 7 months ago

overrideTranslationsFromMap works on my side.

import 'package:example/i18n/strings.g.dart';

void main() {
  final s1 = t.customerService.openHours;
  print(s1);

  print('------');

  final translation = {
    'customerService': {
      'title': 'Customer service',
      'openHours': 'Office111: Week days 7-23\nChat: All days 8-23',
    },
  };

  LocaleSettings.overrideTranslationsFromMap(
    locale: AppLocale.en,
    isFlatMap: false,
    map: translation,
  );

  final s2 = t.customerService.openHours;
  print(s2);
}

prints

Office: closed
Chat: All days 8-23
------
Office111: Week days 7-23
Chat: All days 8-23
lohnn commented 7 months ago

Hmm, testing this in a self-contained example, it seems to work. I'll check a bit more and see if I can figure out what's what and get back to you!

lohnn commented 7 months ago

You know what?! It seems to have been fixed between the version that we apparently ran (and I totally forgot to update - slang: ^3.22.0) and the current version. It is now working perfectly as intended.

I apologise for wasting your time and thank you very much for looking at it.

Closing due to PEBCAK.