Coder Social home page Coder Social logo

flutter-icapps-translations's Introduction

flutter icapps translations

A dart package to automaticly download translations from the icapps translation tool. The rest of the implementation is done by locale_gen https://pub.dev/packages/locale_gen

pub package Build Status Coverage Status MIT license

Example

Example

Setup

Add dependency to pubspec

pub package

dependencies:
  sprintf: ^6.0.2

dev-dependencies:
  icapps_translations: <latest-version>

Add config to pubspec

Add your locale folder to the assets to make use all your translations are loaded.

flutter:
  assets:
    - assets/locale/

Add the icapps_translations config to fetch the latest translations

icapps_translations:
  api_key: 'enter-your-api-key' #For the new translations tool, this is the bearer token
  project_id: 'optional project id' #To use the new translations tool, enter the project id here
  default_language: 'nl'
  languages: ['en', 'nl']
  locale_assets_path: 'assets/locale/' #This is the location where your json files should be saved.
  assets_path: 'assets/locale/' #This is the location where your json files are located in your flutter app.
  nullsafety: true #Generate nullsafety compatible code. Defaults to false

alternatively set an env variable for your api_key and/or project_id

API_KEY_ICAPPS_TRANSLATIONS
PROJECT_ID_ICAPPS_TRANSLATIONS

Run package with Flutter

flutter packages pub run icapps_translations

Run package with Dart

pub run icapps_translations

Migration steps <9.0.0 to >=9.0.0

With version 9.0.0 of local gen you no longer have static access to the translations, instead you can now manually manage the different localization instances. You can for example store a localization instance as a static. This way you can use it largely the same way as before. Example from the example project:

class LocaleViewModel with ChangeNotifier {
  static final Localization localizationInstance = Localization();
  ...

  Future<void> init(){
    await Localization.load(locale: locale);
    notifyListeners();
  }

You can then access this localizationInstance anywhere in the project like:

LocaleViewModel.localizationInstance.translation1;
LocaleViewModel.localizationInstance.translation2;
LocaleViewModel.localizationInstance.translation3;

Migration steps <7.0.0 to >=7.0.0

With the newest version of locale_gen the context no longer needs to be provided when accessing the translations. This means there are a couple of breaking changes.

The first one is that you can now directly get the translation from the Localization object without having to pass the context, so instead of:

Localization.of(context).translation;

you can now do

Localization.translation;

The second breaking change is how you initialize/change the locale. Before you could do this by changing the localizationDelegate that is passed to the materialApp, but now you just call the load function of the Localization object. So instead of:

      localeDelegate = LocalizationDelegate(
        newLocale: locale,
        localizationOverrides: customLocalizationOverrides,
      );

you now do:

await Localization.load(
      locale: locale,
      localizationOverrides: customLocalizationOverrides,
    );

Arguments

Arguments are supported as of 0.1.3

You can pass a String or a num to as an argument.

Formatting for String: %1$s Formatting for num: %1$d

The number in between % and $ indicate the index of the argument. It is possible to place an argument in 1 language first but in another second:

ex (Grammatically incorrect but it makes my point):

nl '%1$s, ik woon in %2$s. Wist je dat niet?' => KOEN, ik woon in ANTWERPEN. Wist je dat niet?

fr 'I live in %2$s. You didn't knew that %1$s?" => I live in ANTWERP. You didn't knew that KOEN?

Plurals

Since 8.0.0 plurals are supported. To specify a plural, you can use the following syntax in the json file:

{
  "example_plural": {
    "zero": "You have no items",
    "one": "You have %1$d item",
    "two": "You have 2 items, party!",
    "few": "You have a few items, nice!",
    "many": "You have many items, fantastic!",
    "other": "You have %1$d items"
  }
}

This will generate functions where you pass the number of items as an argument. The function will then return the correct translation based on the number of items. The count argument WILL NOT be passed as an argument for string interpolation.

Note that the "other" key is always required, the other keys are dependant on the language in question

Working on mac?

add this to you .bash_profile

fluttertranslations(){
 flutter packages get && flutter packages pub run icapps_translations
}

now you can use the icapps translations with a single command.

fluttertranslations

Example

This repo contains an example how to use this package.

Packages used:

  • flutter_localizations
  • shared_preferences
  • provider
  • sprintf
  • icapps_translations

flutter-icapps-translations's People

Contributors

hannesvdb avatar jorre127 avatar nicolaverbeeck avatar vanlooverenkoen avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flutter-icapps-translations's Issues

Add a param to use a custom output location

by default the output location = lib/util/locale

This should still be used as default. But we should add a param so we can use a custom output location

usecase:
multi package setup

  • mobile
  • desktop
  • web

An enter is handled wrongly

I copy pasted some text from my editor and it contained an endline. This was then added in the transtations and created an issue in the keys-file... I fixed it by removing hidden enters in the translation tool, this is probably a bug there ra

  /// Translations:
  ///
  /// en:  **'CBDC/Digital Assets
\n'**
  static const programmeFilterRedThreadsCbdcDigitalAssets = 'programme_filter_red_threads_cbdc_digital_assets';

Support strict mode

When analysis is configured in strict mode, generated files do not compile

asset_path is used instead of locale_asset_path for reading the json files

Updating nl...
Updating en...
Unhandled exception:
Exception: /Users/vanlooverenkoen/Documents/work/flutter_app/app_shared/assets/locale/nl.json does not exists
#0      LocaleGenWriter.write (package:locale_gen/src/locale_gen_writer.dart:17:7)
#1      main (file:///Users/vanlooverenkoen/.pub-cache/hosted/pub.dartlang.org/icapps_translations-3.0.4/bin/icapps_translations.dart:28:19)
<asynchronous suspension>
#2      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299:32)
#3      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
pub finished with exit code 255

New line not supported

When entering a string with a newline in the web portal. It's converted to a \\n instead of an \n And the package currently doesn't support this.
So the package should remove change the \\n to \n

Refactor to use LocaleGen internally

Localegen & flutter-icapps-translations are the same package. with the difference of flutter-icapps-translations using an api to fetch the data.

Refactor to use locale_gen internally

@MSWagner #10 will not be relevant if this is merged

Add comments to see translation in IDE

I18n adds the default translation in file as description, this has the effect that in your IDE you can immediately see what it means and prevents wrong labels.

  /// No description provided for @careerInfoTitle.
  ///
  /// In en, this message translates to:
  /// **'Info'**
  String get careerInfoTitle;

image

Following error is given when using the translation tool on Windows 11 Home version 22H2

`Unhandled exception:

Invalid argument(s): output_path should always start with lib
#0 LocaleGenParams.configure (package:locale_gen/src/locale_gen_params.dart:91:7)

#1 Params.configure (file:///C:/Users/lucav/AppData/Local/Pub/Cache/hosted/pub.flutter-io.cn/icapps_translations-10.0.0/bin/src/params.dart:34:11)

#2 new LocaleGenParams.fromYamlString (package:locale_gen/src/locale_gen_params.dart:50:5)

#3 new Params.fromYamlString (file:///C:/Users/lucav/AppData/Local/Pub/Cache/hosted/pub.flutter-io.cn/icapps_translations-10.0.0/bin/src/params.dart:30:15)

#4 new Params (file:///C:/Users/lucav/AppData/Local/Pub/Cache/hosted/pub.flutter-io.cn/icapps_translations-10.0.0/bin/src/params.dart:26:19)

#5 main (file:///C:/Users/lucav/AppData/Local/Pub/Cache/hosted/pub.flutter-io.cn/icapps_translations-10.0.0/bin/icapps_translations.dart:13:18)

#6 _delayEntrypointInvocation. (dart:isolate-patch/isolate_patch.dart:295:32)

#7 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

pub finished with exit code 255`

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.