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

Different case in flat map #47

Closed
vladaman opened this issue Dec 12, 2021 · 7 comments
Closed

Different case in flat map #47

vladaman opened this issue Dec 12, 2021 · 7 comments
Labels
wontfix This will not be worked on

Comments

@vladaman
Copy link

Affected version: 5.6.0

Setting following in build.yaml does not seem to have effect on generated maps case:

      key_map_case: pascal
      flat_map: false
      maps:
        - ProjectActivityType
        - AddressBookType
  1. When flat_map is false it does not generate any maps in strings.g.dart
  2. Even when flat_map is enabled to true than the key_map_case case transformation is ignored.

JSON:

{
  "AddressBookType": {
    "COMPANY": "Company",
    "PERSON": "Person"
  }
}

will produce nin strings.g.dart:

AppLocale.cs: {
  'addressBookType.company': 'Firma',
  'addressBookType.person': 'Osoba',
}
@vladaman vladaman added the bug Something isn't working label Dec 12, 2021
@Tienisto
Copy link
Member

Tienisto commented Dec 12, 2021

Hi,

Did you configure key_case: camel? Then the maps should also follow the case:

key_case: camel
key_map_case: pascal
flat_map: false
maps:
  - projectActivityType
  - addressBookType

@vladaman
Copy link
Author

Thanks, but still doesn't seem to work as we expect. With following example we'd expect the flat map will be generted just for maps mentioned in the list. Neither of them work. None of the maps is generated. We'd expect the regular keys be converted into camel case, and maps for these objects generated with Pascal case.

          key_case: camel
          key_map_case: pascal
          flat_map: false
          maps:
            - ProjectActivityType
            - AddressBookType
            - addressBookType

@Tienisto
Copy link
Member

Tienisto commented Dec 13, 2021

You mean this following map?

AppLocale.cs: {
  'addressBookType.company': 'Firma',
  'addressBookType.person': 'Osoba',
}

Then flat_map needs to be true. The flat map is the map containing all translations regardless of "map" or "not map".
It is the one-dimensional map (hence flat) for all translations.

Actually, I don't know exactly what you are going to do... If you want to access the keys dynamically, then this should be enough:

final t = Translations.of(context);
String a = t.projectActivityType['Company']; // Company because pascal case is applied

Just to make sure: key_map_case applies to all keys inside a map, key_case applies to all other keys, therefore also to all map parents like addressBookType.

@Tienisto
Copy link
Member

It seems to be alright on my side.

@vladaman
Copy link
Author

Sorry @Tienisto , but I am still having issues. My config is:

key_case: camel
key_map_case: snake
flat_map: true
maps:
   - ProjectActivityType
   - AddressBookType
   - FieldType

But unfortunately the maps in strings.g.dart are still translated to:

'fieldType.lplate': 'License plate',
'fieldType.desc': 'Description',
'fieldType.contactInvestor': 'Contact Investor',

In .json file we have following:

"FieldType": {
 "LPLATE": "License plate",
 "DESC": "Description",
 "CONTACT_INVESTOR": "Contact Investor",

I would expect the map keys to be in snake case but it's not. But our use case is different. We'd like to use camel case but ONLY for maps to disable any text transformations. Reason is we use enums like:

// The key will be "FieldType.LPLATE"
labelText: t[FieldType.LPLATE.toString()]

@Tienisto
Copy link
Member

Alright,

currently, it is not possible to have different cases.

key_map_case only applies to local maps (i.e. t.error['camel_case']).

I think a new configuration flat_map_key_case should be implemented.

@Tienisto Tienisto reopened this Feb 22, 2022
@Tienisto Tienisto changed the title key_map_case is ignored in 5.6.0 Different case in flat map Feb 23, 2022
@Tienisto Tienisto added enhancement New feature or request and removed bug Something isn't working labels Feb 23, 2022
@Tienisto
Copy link
Member

Tienisto commented Mar 17, 2022

I think there is no elegant solution for your problem.

FieldType.LPLATE.toString() will be probably calculated as FieldType.LPLATE. This string contains 2 different cases.

The best solution would be to use the new dart enum feature: FieldType.LPLATE.name. Then you will get LPLATE.

Then use the following config:

key_case: pascal
# do not set key_map_case, so we retain the UPPERCASE_SNAKE case
maps:
  - FieldType

Will generate:

Map<String, String> get FieldType => {
	'LPLATE': 'License plate',
	'DESC': 'Description',
	'CONTACT_INVESTOR': 'Contact Investor',
};

Usage:

String a = t.FieldType['CONTACT_INVESTOR'];
String b = t.FieldType[FieldType.LPLATE.name];

@Tienisto Tienisto added wontfix This will not be worked on and removed enhancement New feature or request labels Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants