Jesway / flutter_translate

Flutter Translate is a fully featured localization / internationalization (i18n) library for Flutter.
MIT License
401 stars 118 forks source link

Added support for list of strings #45

Closed Skogsfrae closed 4 years ago

Skogsfrae commented 4 years ago

What

Added support to translate a list of strings.

Why

For a project I'm working on I needed to get a list of strings to display in a random way (ex.: a pseudo bot that says different sentences for a specific action) using the nested json feature provided by lokalise (link to specific doc page).

Example

json:

{
   "level_1": {
      "attack": [
          "Your attack was effective",
          "You hit your enemy!"
      ]
   }
}

flutter:

import "dart:math";
import 'package:flutter/material.dart';
import 'package:flutter_translate/flutter_translate.dart';

class FightFeedback extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    final rnd = Random();
    final messages = translateList('level_1.attack');
    final message = messages[rnd.nextInt(messages.length)];

    return Text(message);
  }
}
ezamagni commented 4 years ago

Very useful feature! Tried it and works perfectly

bratan commented 4 years ago

Thanks for the pull request, however I will not merge this feature. This problem can easily be solved by creating an array of localization keys instead and is very situational.

Skogsfrae commented 4 years ago

An array of localization keys could be a solution if the array is of fixed length. However, if I want to add a new message with your solution I should add another key to that specific array and thus create another build of the app

bratan commented 4 years ago

And adding a new string to the localization files wouldn't require creating another build?

ezamagni commented 4 years ago

No if the strings are downloaded OTA

bratan commented 4 years ago

The localization service loads the localization strings from the embedded assets (JSON files). So in order to add a new string/message, you would need to add it to the specific JSON files and recompile the app.

What you're saying would work if the localization files would be retrieved remotely for example but this is not the case here.

If you meant the array keys would have to be static, this is not the case either. You can just retrieve the list of localization keys from an API and create/modify the array based on that.