Giter VIP home page Giter VIP logo

intl_generator's Introduction

Intl_generator

This is a fork of intl_translation to have an up to date version.

If you prefer having translations as part of your build_runner process or need to support flavors, please check intl_flavors

This package provides message extraction and code generation from translated messages for the Intl package. It's a separate package so as to not require a dependency on analyzer for all users.

Extracting And Using Translated Messages

When your program contains messages that need translation, these must be extracted from the program source, sent to human translators, and the results need to be incorporated.

To extract messages, run the extract_to_arb.dart program.

  pub run intl_generator:extract_to_arb --output-dir=target/directory
      my_program.dart more_of_my_program.dart

This will produce a file intl_messages.arb with the messages from all of these programs. This is an ARB format file which can be used for input to translation tools like Localizely or (the deprecated) Google Translator Toolkit. The resulting translations can be used to generate a set of libraries using the generate_from_arb.dart program.

This expects to receive a series of files, one per locale.

pub run intl_generator:generate_from_arb --generated-file-prefix=<prefix>
    <my_dart_files> <translated_ARB_files>

This will generate Dart libraries, one per locale, which contain the translated versions. Your Dart libraries can import the primary file, named <prefix>messages_all.dart, and then call the initialization for a specific locale. Once that's done, any Intl.message calls made in the context of that locale will automatically print the translated version instead of the original.

import "my_prefix_messages_all.dart";
...
initializeMessages("dk").then(printSomeMessages);

Once the Future returned from the initialization call completes, the message data is available.

intl_generator's People

Contributors

aam avatar acoutts avatar alan-knight avatar creisman avatar davidmorgan avatar devoncarew avatar franklinyow avatar jamesblasco avatar jaumard avatar jonahwilliams avatar keertip avatar leonsenft avatar maxlapides avatar michaelgobbers avatar stereotype441 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

intl_generator's Issues

Feature Request: `String? getMessageByKey(String messageKey)`

TD;LR Read the title, I just want to have a dynamic way to resolve messages (if they exists) during runtime.

Full story:

I have a backend that is going to send both info & error messages using message-keys, ex: "{'id':302, 'error': 'itemNotFound'}". I want to dynamically resolve those keys using generated code but, for now, I don't have a method that can be used in order to solve the error message.

It would be great to have a method in the S class that would allow me to get the translated message if it exists, and if it doesn't, I can manually generate a generic message:

...
} catch BackendError {
  String? message  = getMessageByKey(errorKey);
  if(message == null) {
     print("ERROR: Unknown error: $errorKey")
  } else {
     print("ERROR: $message");
  }
}

Having such method probably will require the generation of a Map<String, String Function()> static member that will look like something like this:

{ 
  'itemNotFound': () => this.itemNotFound,
  'forbidden': () => this.forbidden,
  'notAuthorized': () => this.notAuthorized,
  ....
}

I've done something similar in bdlukaa/fluent_ui@b53288b#diff-ab7c9e25f588ae440e0de59542af84db24d60f230b9c7143d42b1afa9bc45da4R47 (In that case the motivation to generate a map was also to be able to search for the "icons" by name)

Add option to disable the generation of the "placeholders_order" element?

Hi,

I switched to your plugin since intl_translation has dependency problems.

Thanks for creating this fork, because it works fine, but I was wondering if it's possible to have a commandline option to disable the generation of the "placeholders_order" element in the arb files? This wasn't part of the files generated by intl_translation, and I already have quiet a large translated file (700+ translated strings), so I either have to redo the whole translated file, or it gets cumbersome to see the actual differences if I generate new files using your plugin. An option to not generate this element would help a lot.

Thanks!

Single quote is not being escaped when generating ARB file

Below is the plural message being used. But, when generating the ARB file, the single quote is not escaping. Similary, I also used double quotes to use the single quotes. That didn't give desired result either.

Intl.plural(
        numberOfOranges,
        zero: "You\'ve got zero orange.",
        one: "You\'ve got $numberOfOranges orange.",
        other: "You\'ve got $numberOfOranges oranges.",
        name: 'getOrangesText',
        args: [numberOfOranges],
      )

ARB: app_en.arb

{
  "@@last_modified": "2021-12-23T12:15:56.804292",
  "appTitle": "Home",
  "@appTitle": {
    "description": "Application title/name",
    "type": "text",
    "placeholders": {}
  },
  "helloWorld": "Hello World!",
  "@helloWorld": {
    "description": "Hello World message",
    "type": "text",
    "placeholders": {}
  },
  "getOrangesText": "{numberOfOranges,plural, =0{You''ve got zero orange.}=1{You''ve got {numberOfOranges} orange.}other{You''ve got {numberOfOranges} oranges.}}",
  "@getOrangesText": {
    "type": "text",
    "placeholders": {
      "numberOfOranges": {}
    }
  }
}

Syntax error in 0.4.0

Hi there! Thank you for releasing version 0.4.0!

I am now getting this error:

../../.pub-cache/hosted/pub.dartlang.org/intl_generator-0.4.0/lib/extract_messages.dart:169:74: Error: Too many positional arguments: 0 allowed, but 5 found.
Try removing the extra positional arguments.
  final FormalParameterList _emptyParameterList = FormalParameterListImpl(
                                                                         ^
../../.pub-cache/hosted/pub.dartlang.org/analyzer-5.2.0/lib/src/dart/ast/ast.dart:5342:3: Context: Found this candidate, but the arguments don't match.
  FormalParameterListImpl({
  ^^^^^^^^^^^^^^^^^^^^^^^
pub finished with exit code 1

I think we probably need to revert the changes in this commit: 64c3337. What do you think @jaumard?

Support for nullable parameters

The following code in appLocalization:

String lblUserWithName(String? name) => Intl.message('User $name', name: "lblUserWithName", args: [name!]);

leads to an error message:

Skipping invalid Intl.message invocation
    <Intl.message('User $name', name: "lblUserWithName", args: [name!])>
    reason: The 'args' argument must match the message arguments, e.g. args: [name]
    from lib/i10n/applocalization.dart    line: 84, column: 43

Suggestion is to either change the type of args to dynamic or Object? or support "name!" as args-parameter (I think the former is not consistent).

Update petitparser dependency

The petitparser library has reached version 6. Please can this library be updated to use this version as currently it is not possible to upgrade versions of other libraries such as xml.

Can't be used with json_serializable >= 5.0.0 (upgrade to analyzer ^2.0.0)

Since this package depends on analyzer 1.5 it cannot be used together with newer packages like json_serializable v5. It would be nice, if the package could be updated for analyzer >= 2.0

Because json_serializable >=5.0.0 depends on analyzer ^2.0.0 and every version of intl_generator depends on analyzer ^1.5.0, json_serializable >=5.0.0 is incompatible with intl_generator.
So, because motionsense depends on both intl_generator ^0.2.0+0 and json_serializable ^5.0.2, version solving failed.

petitparser dependency

Bonjour
(je suis celui qui vous a dérangé ;)
Est-il possible de mettre les dernières dépendances en place en même temps que les PR proposées
Merci

Duplicate keys when running generate_from_arb

Throughout my app I define Intl.message() entries in several different files, usually for each screen to keep it organized. Sometimes the same string might be used in two spots. When the dart files are generated, the map contains two entries with the same map key because two dart files in my project happen to use the same string.

Is the only solution here to always use one massive class with all of the intl messages in it?

foo.dart

String get foo => Intl.message('foo');

bar.dart

String get foo => Intl.message('foo');

messages_en_GB.dart (generated)

static Map<String, Function> _notInlinedMessages(_) => <String, Function> {
    "foo" : MessageLookupByLibrary.simpleMessage("foo"),
    "foo" : MessageLookupByLibrary.simpleMessage("foo"),
}

Two keys in a map literal shouldn't be equal.
Change or remove the duplicate key.dart(equal_keys_in_map)

The generated arb file from extract_to_arb is fine and has no duplicates, it's just the generated dart file.

Error when try extract .arb

Description

I have file lib/test.dart with the next content.

import 'package:intl/intl.dart';

String lblUserWithName(String? name) =>
    Intl.message('User $name', name: "lblUserWithName", args: [name!]);
 

When I start this command:
flutter pub global run intl_generator: extract_to_arb --output-dir=lib/ lib/test.dart

I had this output(

Screenshot 2022-01-28 at 20 58 46

Details

Flutter 2.8.1 • channel stable
intl_generator: ^0.2.1

Demo with Example

Hi

Thanks for the FORK.

Can you please update the ReadMe with real life example?

Because no versions of intl_generator match >0.2.1 <0.3.0 and intl_generator 0.2.1 depends on analyzer ^2.0.0, intl_generator ^0.2.1 requires analyzer ^2.0.0.

Suddenly an error occurs.

Running "flutter pub get" in pronto...

Because no versions of intl_generator match >0.2.1 <0.3.0 and intl_generator 0.2.1 depends on analyzer ^2.0.0, intl_generator ^0.2.1 requires analyzer ^2.0.0.
And because freezed >=2.0.0 depends on analyzer >=3.0.0 <5.0.0, intl_generator ^0.2.1 is incompatible with freezed >=2.0.0.
So, because pronto depends on both freezed 2.0.2 and intl_generator ^0.2.1, version solving failed.
pub get failed (1; So, because pronto depends on both freezed 2.0.2 and intl_generator ^0.2.1, version solving failed.)

Process finished with exit code 1

[✓] Flutter (Channel stable, 2.10.5, on macOS 12.3.1 21E258 darwin-arm, locale ko-KR)
    • Flutter version 2.10.5 at /Users/patrick386/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5464c5bac7 (8 days ago), 2022-04-18 09:55:37 -0700
    • Engine revision 57d3bac3dd
    • Dart version 2.16.2
    • DevTools version 2.9.2

[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /Users/patrick386/Library/Android/sdk
    • Platform android-32, build-tools 32.1.0-rc1
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 12.3.1 21E258 darwin-arm
    • Chrome (web)    • chrome • web-javascript • Google Chrome 100.0.4896.127

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

Analyser dependency 5.2.0

Hello
is it possible to publish a new version to pub.dev with analyzer 5.2.0 instead of : ">=4.0.0 <5.0.0"
Thank you

Does not support analyzer v4

flutter pub run intl_generator:extract_to_arb --output-dir=lib/l10n/gen lib/l10n/l10n.dart --warnings-are-errors --suppress-last-modified
Failed to build intl_generator:extract_to_arb:
/home/runner/.pub-cache/hosted/pub.dartlang.org/intl_generator-0.2.1/lib/extract_messages.dart:26:8: Error: Error when reading '/home/runner/.pub-cache/hosted/pub.dartlang.org/analyzer-4.0.0/lib/dart/ast/standard_ast_factory.dart': No such file or directory
import 'package:analyzer/dart/ast/standard_ast_factory.dart';
       ^
/home/runner/.pub-cache/hosted/pub.dartlang.org/intl_generator-0.2.1/lib/extract_messages.dart:173:7: Error: Undefined name 'astFactory'.
      astFactory.formalParameterList(Token.eof(0), [], null, null, Token.eof(0));
      ^^^^^^^^^^
pub finished with exit code 1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.