Skip to content

Commit

Permalink
feat: base root class should be config.className
Browse files Browse the repository at this point in the history
  • Loading branch information
Tienisto committed Dec 4, 2023
1 parent e499a54 commit 4d48ab6
Show file tree
Hide file tree
Showing 16 changed files with 281 additions and 291 deletions.
48 changes: 21 additions & 27 deletions slang/example/lib/i18n/strings.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// Locales: 2
/// Strings: 12 (6 per locale)
///
/// Built on 2023-07-17 at 14:17 UTC
/// Built on 2023-12-04 at 01:16 UTC
// coverage:ignore-file
// ignore_for_file: type=lint
Expand All @@ -24,19 +24,19 @@ const AppLocale _baseLocale = AppLocale.en;
/// - LocaleSettings.setLocale(AppLocale.en) // set locale
/// - Locale locale = AppLocale.en.flutterLocale // get flutter locale from enum
/// - if (LocaleSettings.currentLocale == AppLocale.en) // locale check
enum AppLocale with BaseAppLocale<AppLocale, _StringsEn> {
en(languageCode: 'en', build: _StringsEn.build),
enum AppLocale with BaseAppLocale<AppLocale, Translations> {
en(languageCode: 'en', build: Translations.build),
de(languageCode: 'de', build: _StringsDe.build);

const AppLocale({required this.languageCode, this.scriptCode, this.countryCode, required this.build}); // ignore: unused_element

@override final String languageCode;
@override final String? scriptCode;
@override final String? countryCode;
@override final TranslationBuilder<AppLocale, _StringsEn> build;
@override final TranslationBuilder<AppLocale, Translations> build;

/// Gets current instance managed by [LocaleSettings].
_StringsEn get translations => LocaleSettings.instance.translationMap[this]!;
Translations get translations => LocaleSettings.instance.translationMap[this]!;
}

/// Method A: Simple
Expand All @@ -48,7 +48,7 @@ enum AppLocale with BaseAppLocale<AppLocale, _StringsEn> {
/// Usage:
/// String a = t.someKey.anotherKey;
/// String b = t['someKey.anotherKey']; // Only for edge cases!
_StringsEn get t => LocaleSettings.instance.currentTranslations;
Translations get t => LocaleSettings.instance.currentTranslations;

/// Method B: Advanced
///
Expand All @@ -65,17 +65,10 @@ _StringsEn get t => LocaleSettings.instance.currentTranslations;
/// final t = Translations.of(context); // Get t variable.
/// String a = t.someKey.anotherKey; // Use t variable.
/// String b = t['someKey.anotherKey']; // Only for edge cases!
class Translations {
Translations._(); // no constructor

static _StringsEn of(BuildContext context) => InheritedLocaleData.of<AppLocale, _StringsEn>(context).translations;
}

/// The provider for method B
class TranslationProvider extends BaseTranslationProvider<AppLocale, _StringsEn> {
class TranslationProvider extends BaseTranslationProvider<AppLocale, Translations> {
TranslationProvider({required super.child}) : super(settings: LocaleSettings.instance);

static InheritedLocaleData<AppLocale, _StringsEn> of(BuildContext context) => InheritedLocaleData.of<AppLocale, _StringsEn>(context);
static InheritedLocaleData<AppLocale, Translations> of(BuildContext context) => InheritedLocaleData.of<AppLocale, Translations>(context);
}

/// Method B shorthand via [BuildContext] extension method.
Expand All @@ -84,11 +77,11 @@ class TranslationProvider extends BaseTranslationProvider<AppLocale, _StringsEn>
/// Usage (e.g. in a widget's build method):
/// context.t.someKey.anotherKey
extension BuildContextTranslationsExtension on BuildContext {
_StringsEn get t => TranslationProvider.of(this).translations;
Translations get t => TranslationProvider.of(this).translations;
}

/// Manages all translation instances and the current locale
class LocaleSettings extends BaseFlutterLocaleSettings<AppLocale, _StringsEn> {
class LocaleSettings extends BaseFlutterLocaleSettings<AppLocale, Translations> {
LocaleSettings._() : super(utils: AppLocaleUtils.instance);

static final instance = LocaleSettings._();
Expand All @@ -110,7 +103,7 @@ class LocaleSettings extends BaseFlutterLocaleSettings<AppLocale, _StringsEn> {
}

/// Provides utility functions without any side effects.
class AppLocaleUtils extends BaseAppLocaleUtils<AppLocale, _StringsEn> {
class AppLocaleUtils extends BaseAppLocaleUtils<AppLocale, Translations> {
AppLocaleUtils._() : super(baseLocale: _baseLocale, locales: AppLocale.values);

static final instance = AppLocaleUtils._();
Expand All @@ -126,11 +119,13 @@ class AppLocaleUtils extends BaseAppLocaleUtils<AppLocale, _StringsEn> {
// translations

// Path: <root>
class _StringsEn implements BaseTranslations<AppLocale, _StringsEn> {
class Translations implements BaseTranslations<AppLocale, Translations> {
/// final t = Translations.of(context); // Get t variable.
static Translations of(BuildContext context) => InheritedLocaleData.of<AppLocale, Translations>(context).translations;

/// You can call this constructor and build your own translation instance of this locale.
/// Constructing via the enum [AppLocale.build] is preferred.
_StringsEn.build({Map<String, Node>? overrides, PluralResolver? cardinalResolver, PluralResolver? ordinalResolver})
Translations.build({Map<String, Node>? overrides, PluralResolver? cardinalResolver, PluralResolver? ordinalResolver})
: assert(overrides == null, 'Set "translation_overrides: true" in order to enable this feature.'),
$meta = TranslationMetadata(
locale: AppLocale.en,
Expand All @@ -142,12 +137,12 @@ class _StringsEn implements BaseTranslations<AppLocale, _StringsEn> {
}

/// Metadata for the translations of <en>.
@override final TranslationMetadata<AppLocale, _StringsEn> $meta;
@override final TranslationMetadata<AppLocale, Translations> $meta;

/// Access flat map
dynamic operator[](String key) => $meta.getTranslation(key);

late final _StringsEn _root = this; // ignore: unused_field
late final Translations _root = this; // ignore: unused_field

// Translations
late final _StringsMainScreenEn mainScreen = _StringsMainScreenEn._(_root);
Expand All @@ -161,7 +156,7 @@ class _StringsEn implements BaseTranslations<AppLocale, _StringsEn> {
class _StringsMainScreenEn {
_StringsMainScreenEn._(this._root);

final _StringsEn _root; // ignore: unused_field
final Translations _root; // ignore: unused_field

// Translations
String get title => 'An English Title';
Expand All @@ -173,8 +168,7 @@ class _StringsMainScreenEn {
}

// Path: <root>
class _StringsDe implements _StringsEn {

class _StringsDe implements Translations {
/// You can call this constructor and build your own translation instance of this locale.
/// Constructing via the enum [AppLocale.build] is preferred.
_StringsDe.build({Map<String, Node>? overrides, PluralResolver? cardinalResolver, PluralResolver? ordinalResolver})
Expand All @@ -189,7 +183,7 @@ class _StringsDe implements _StringsEn {
}

/// Metadata for the translations of <de>.
@override final TranslationMetadata<AppLocale, _StringsEn> $meta;
@override final TranslationMetadata<AppLocale, Translations> $meta;

/// Access flat map
@override dynamic operator[](String key) => $meta.getTranslation(key);
Expand Down Expand Up @@ -222,7 +216,7 @@ class _StringsMainScreenDe implements _StringsMainScreenEn {
/// Flat map(s) containing all translations.
/// Only for edge cases! For simple maps, use the map function of this library.
extension on _StringsEn {
extension on Translations {
dynamic _flatMapFunction(String path) {
switch (path) {
case 'mainScreen.title': return 'An English Title';
Expand Down
59 changes: 20 additions & 39 deletions slang/lib/builder/generator/generate_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ String generateHeader(
List<I18nData> allLocales,
) {
const String baseLocaleVar = '_baseLocale';
final String baseClassName = getClassNameRoot(
baseName: config.baseName,
visibility: config.translationClassVisibility,
locale: config.baseLocale,
);
const String pluralResolverType = 'PluralResolver';
const String pluralResolverMapCardinal = '_pluralResolversCardinal';
const String pluralResolverMapOrdinal = '_pluralResolversOrdinal';
Expand Down Expand Up @@ -57,21 +52,18 @@ String generateHeader(
buffer: buffer,
config: config,
allLocales: allLocales,
baseClassName: baseClassName,
);

if (config.localeHandling) {
_generateTranslationGetter(
buffer: buffer,
config: config,
baseClassName: baseClassName,
);

_generateLocaleSettings(
buffer: buffer,
config: config,
allLocales: allLocales,
baseClassName: baseClassName,
pluralResolverType: pluralResolverType,
pluralResolverCardinal: pluralResolverMapCardinal,
pluralResolverOrdinal: pluralResolverMapOrdinal,
Expand All @@ -82,7 +74,6 @@ String generateHeader(
buffer: buffer,
config: config,
baseLocaleVar: baseLocaleVar,
baseClassName: baseClassName,
);

_generateContextEnums(buffer: buffer, config: config);
Expand Down Expand Up @@ -234,7 +225,6 @@ void _generateEnum({
required StringBuffer buffer,
required GenerateConfig config,
required List<I18nData> allLocales,
required String baseClassName,
}) {
final String enumName = config.enumName;
final String baseLocaleEnumConstant =
Expand All @@ -252,14 +242,9 @@ void _generateEnum({
'/// - if (LocaleSettings.currentLocale == $baseLocaleEnumConstant) // locale check');

buffer.writeln(
'enum $enumName with BaseAppLocale<$enumName, $baseClassName> {');
'enum $enumName with BaseAppLocale<$enumName, ${config.className}> {');
for (int i = 0; i < allLocales.length; i++) {
final I18nLocale locale = allLocales[i].locale;
final String className = getClassNameRoot(
baseName: config.baseName,
visibility: config.translationClassVisibility,
locale: locale,
);

buffer
.write('\t${locale.enumConstant}(languageCode: \'${locale.language}\'');
Expand All @@ -269,7 +254,16 @@ void _generateEnum({
if (locale.country != null) {
buffer.write(', countryCode: \'${locale.country}\'');
}

final String className = allLocales[i].base
? config.className
: getClassNameRoot(
baseName: config.baseName,
visibility: config.translationClassVisibility,
locale: locale,
);
buffer.write(', build: $className.build)');

if (i != allLocales.length - 1) {
buffer.writeln(',');
} else {
Expand All @@ -286,12 +280,12 @@ void _generateEnum({
buffer.writeln('\t@override final String? scriptCode;');
buffer.writeln('\t@override final String? countryCode;');
buffer.writeln(
'\t@override final TranslationBuilder<$enumName, $baseClassName> build;');
'\t@override final TranslationBuilder<$enumName, ${config.className}> build;');
if (config.localeHandling) {
buffer.writeln();
buffer.writeln('\t/// Gets current instance managed by [LocaleSettings].');
buffer.writeln(
'\t$baseClassName get translations => LocaleSettings.instance.translationMap[this]!;');
'\t${config.className} get translations => LocaleSettings.instance.translationMap[this]!;');
}

buffer.writeln('}');
Expand All @@ -300,7 +294,6 @@ void _generateEnum({
void _generateTranslationGetter({
required StringBuffer buffer,
required GenerateConfig config,
required String baseClassName,
}) {
final String translationsClass = config.className;
final String translateVar = config.translateVariable;
Expand All @@ -322,7 +315,7 @@ void _generateTranslationGetter({
'/// String b = $translateVar[\'someKey.anotherKey\']; // Only for edge cases!');
}
buffer.writeln(
'$baseClassName get $translateVar => LocaleSettings.instance.currentTranslations;');
'${config.className} get $translateVar => LocaleSettings.instance.currentTranslations;');

// t getter (advanced)
if (config.flutterIntegration) {
Expand All @@ -349,23 +342,13 @@ void _generateTranslationGetter({
buffer.writeln(
'/// String b = $translateVar[\'someKey.anotherKey\']; // Only for edge cases!');
}
buffer.writeln('class $translationsClass {');
buffer.writeln('\t$translationsClass._(); // no constructor');
buffer.writeln();
buffer.writeln(
'\tstatic $baseClassName of(BuildContext context) => InheritedLocaleData.of<$enumName, $baseClassName>(context).translations;');
buffer.writeln('}');

// provider
buffer.writeln();
buffer.writeln('/// The provider for method B');
buffer.writeln(
'class TranslationProvider extends BaseTranslationProvider<$enumName, $baseClassName> {');
'class TranslationProvider extends BaseTranslationProvider<$enumName, ${config.className}> {');
buffer.writeln(
'\tTranslationProvider({required super.child}) : super(settings: LocaleSettings.instance);');
buffer.writeln();
buffer.writeln(
'\tstatic InheritedLocaleData<$enumName, $baseClassName> of(BuildContext context) => InheritedLocaleData.of<$enumName, $baseClassName>(context);');
'\tstatic InheritedLocaleData<$enumName, ${config.className}> of(BuildContext context) => InheritedLocaleData.of<$enumName, ${config.className}>(context);');
buffer.writeln('}');

// BuildContext extension for provider
Expand All @@ -379,7 +362,7 @@ void _generateTranslationGetter({
buffer.writeln(
'extension BuildContextTranslationsExtension on BuildContext {');
buffer.writeln(
'\t$baseClassName get $translateVar => TranslationProvider.of(this).translations;');
'\t${config.className} get $translateVar => TranslationProvider.of(this).translations;');
buffer.writeln('}');
}
}
Expand All @@ -388,7 +371,6 @@ void _generateLocaleSettings({
required StringBuffer buffer,
required GenerateConfig config,
required List<I18nData> allLocales,
required String baseClassName,
required String pluralResolverType,
required String pluralResolverCardinal,
required String pluralResolverOrdinal,
Expand All @@ -403,7 +385,7 @@ void _generateLocaleSettings({
buffer
.writeln('/// Manages all translation instances and the current locale');
buffer.writeln(
'class $settingsClass extends $baseClass<$enumName, $baseClassName> {');
'class $settingsClass extends $baseClass<$enumName, ${config.className}> {');
buffer
.writeln('\t$settingsClass._() : super(utils: AppLocaleUtils.instance);');
buffer.writeln();
Expand Down Expand Up @@ -449,15 +431,14 @@ void _generateUtil({
required StringBuffer buffer,
required GenerateConfig config,
required String baseLocaleVar,
required String baseClassName,
}) {
const String utilClass = 'AppLocaleUtils';
final String enumName = config.enumName;

buffer.writeln();
buffer.writeln('/// Provides utility functions without any side effects.');
buffer.writeln(
'class $utilClass extends BaseAppLocaleUtils<$enumName, $baseClassName> {');
'class $utilClass extends BaseAppLocaleUtils<$enumName, ${config.className}> {');
buffer.writeln(
'\t$utilClass._() : super(baseLocale: $baseLocaleVar, locales: $enumName.values${config.translationOverrides ? ', buildConfig: _buildConfig' : ''});');
buffer.writeln();
Expand All @@ -480,9 +461,9 @@ void _generateUtil({
'\tstatic List<String> get supportedLocalesRaw => instance.supportedLocalesRaw;');
if (config.translationOverrides) {
buffer.writeln(
'\tstatic $baseClassName buildWithOverrides({required AppLocale locale, required FileType fileType, required String content, PluralResolver? cardinalResolver, PluralResolver? ordinalResolver}) => instance.buildWithOverrides(locale: locale, fileType: fileType, content: content, cardinalResolver: cardinalResolver, ordinalResolver: ordinalResolver);');
'\tstatic ${config.className} buildWithOverrides({required AppLocale locale, required FileType fileType, required String content, PluralResolver? cardinalResolver, PluralResolver? ordinalResolver}) => instance.buildWithOverrides(locale: locale, fileType: fileType, content: content, cardinalResolver: cardinalResolver, ordinalResolver: ordinalResolver);');
buffer.writeln(
'\tstatic $baseClassName buildWithOverridesFromMap({required AppLocale locale, required bool isFlatMap, required Map map, PluralResolver? cardinalResolver, PluralResolver? ordinalResolver}) => instance.buildWithOverridesFromMap(locale: locale, isFlatMap: isFlatMap, map: map, cardinalResolver: cardinalResolver, ordinalResolver: ordinalResolver);');
'\tstatic ${config.className} buildWithOverridesFromMap({required AppLocale locale, required bool isFlatMap, required Map map, PluralResolver? cardinalResolver, PluralResolver? ordinalResolver}) => instance.buildWithOverridesFromMap(locale: locale, isFlatMap: isFlatMap, map: map, cardinalResolver: cardinalResolver, ordinalResolver: ordinalResolver);');
}

buffer.writeln('}');
Expand Down
2 changes: 1 addition & 1 deletion slang/lib/builder/generator/generate_translation_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ String generateTranslationMap(

buffer.writeln();
buffer.writeln(
'extension on ${getClassNameRoot(baseName: config.baseName, locale: localeData.locale, visibility: config.translationClassVisibility)} {');
'extension on ${localeData.base ? config.className : getClassNameRoot(baseName: config.baseName, locale: localeData.locale, visibility: config.translationClassVisibility)} {');
buffer.writeln('\tdynamic _flatMapFunction(String path) {');

buffer.writeln('\t\tswitch (path) {');
Expand Down
Loading

0 comments on commit 4d48ab6

Please sign in to comment.