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

how to pass a parameter to be added to strings localized like "Here is the number:$number" #135875

Closed
2 tasks done
EgHubs opened this issue Oct 2, 2023 · 4 comments
Closed
2 tasks done
Labels
r: solved Issue is closed as solved

Comments

@EgHubs
Copy link

EgHubs commented Oct 2, 2023

Is there an existing issue for this?

What package does this bug report belong to?

animations

What target platforms are you seeing this bug on?

Android, iOS

Have you already upgraded your packages?

Yes

Dependency versions

No response

Steps to reproduce

I use these two classes for localization in flutter
following this article setup.

import 'package:flutter/cupertino.dart';

abstract class Languages {
  final String? extra;

  Languages({this.extra});
  static Languages? of(BuildContext context, {String? extra}) {
    return Localizations.of<Languages>(context, Languages);
  }
String get text199;
}

and this LanguageEn:

import 'languages.dart';

class LanguageEn extends Languages
{
  LanguageEn({String? extra}) : super(extra: extra);
  @override
String get text199 => "Update data($extra)";
}

when I pass this extra parameter I get null "Update data(null)" in whatever language I'm in

Languages.of(context,extra: "$activeID")!.text199),

but when I directly select a language class and do this it works and I get the number and not null "Update data(4)"

LanguageEn(extra: "$activeID").text199),

how to pass dynamic strings with this package?

Expected results

the extra parameter should not be null

Actual results

it's null if I called the Languages class and not null in the LanguagesEn class

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.13.6, on macOS 13.5.2 22G91 darwin-arm64, locale en-EG)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.82.2)
[✓] Connected device (3 available)
[✓] Network resources

• No issues found!
@EgHubs EgHubs changed the title how to pass a parameter to be added to strings localized like "Here is the number:$number" #8 how to pass a parameter to be added to strings localized like "Here is the number:$number" Oct 2, 2023
@Ignacio1110
Copy link

you didn't use the extra in your language class.
this article is 3 years ago, you have other better choices to do localization.

//this method received String? but did use it anywhere.
static Languages? of(BuildContext context, {String? extra}) {
    return Localizations.of<Languages>(context, Languages);
  }

@darshankawar darshankawar added the in triage Presently being triaged by the triage team label Oct 3, 2023
@darshankawar
Copy link
Member

@EgHubs
Check above solution and see if it helps in your case or not. Also, take a look at official localization documentation for further reference.

I'll close it for now, as above link and solution should help to resolve the issue. If not, write in comments and I'll reopen it.

@darshankawar darshankawar added r: solved Issue is closed as solved and removed in triage Presently being triaged by the triage team labels Oct 3, 2023
@EgHubs
Copy link
Author

EgHubs commented Oct 4, 2023

@Ignacio1110
Thanks your comment did help me to solve the problem!
in case anybody needs the solution here is it:
modified the Language class to this:

import 'package:flutter/cupertino.dart';

abstract class Languages {
    String? extra;

  Languages({this.extra});
  static Languages? of(BuildContext context, {String? extra}) {
    final Languages? instance = Localizations.of<Languages>(context, Languages);
    if (instance != null) {
      instance.extra = extra; // Assign the extra parameter to the instance.
    }
    return instance;
  }

  String get text199;
  }

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
r: solved Issue is closed as solved
Projects
None yet
Development

No branches or pull requests

3 participants