Unverified Commit 82c00c91 authored by Hans Muller's avatar Hans Muller Committed by GitHub

Move the localization_utils camelCase function to LocaleInfo (#50152)

parent c3e0f2d4
...@@ -151,7 +151,7 @@ String generateArbBasedLocalizationSubclasses({ ...@@ -151,7 +151,7 @@ String generateArbBasedLocalizationSubclasses({
output.writeln(generateClassDeclaration( output.writeln(generateClassDeclaration(
scriptBaseLocale, scriptBaseLocale,
generatedClassPrefix, generatedClassPrefix,
'$generatedClassPrefix${camelCase(languageLocale)}', '$generatedClassPrefix${languageLocale.camelCase()}',
)); ));
output.writeln(generateConstructor(scriptBaseLocale)); output.writeln(generateConstructor(scriptBaseLocale));
final Map<String, String> scriptResources = localeToResources[scriptBaseLocale]; final Map<String, String> scriptResources = localeToResources[scriptBaseLocale];
...@@ -175,7 +175,7 @@ String generateArbBasedLocalizationSubclasses({ ...@@ -175,7 +175,7 @@ String generateArbBasedLocalizationSubclasses({
output.writeln(generateClassDeclaration( output.writeln(generateClassDeclaration(
locale, locale,
generatedClassPrefix, generatedClassPrefix,
'$generatedClassPrefix${camelCase(scriptBaseLocale)}', '$generatedClassPrefix${scriptBaseLocale.camelCase()}',
)); ));
output.writeln(generateConstructor(locale)); output.writeln(generateConstructor(locale));
final Map<String, String> localeResources = localeToResources[locale]; final Map<String, String> localeResources = localeToResources[locale];
...@@ -201,7 +201,7 @@ String generateArbBasedLocalizationSubclasses({ ...@@ -201,7 +201,7 @@ String generateArbBasedLocalizationSubclasses({
output.writeln(generateClassDeclaration( output.writeln(generateClassDeclaration(
locale, locale,
generatedClassPrefix, generatedClassPrefix,
'$generatedClassPrefix${camelCase(languageLocale)}', '$generatedClassPrefix${languageLocale.camelCase()}',
)); ));
output.writeln(generateConstructor(locale)); output.writeln(generateConstructor(locale));
for (final String key in localeResources.keys) { for (final String key in localeResources.keys) {
...@@ -267,7 +267,7 @@ $factoryDeclaration ...@@ -267,7 +267,7 @@ $factoryDeclaration
if (languageToLocales[language].length == 1) { if (languageToLocales[language].length == 1) {
output.writeln(''' output.writeln('''
case '$language': case '$language':
return $generatedClassPrefix${camelCase(languageToLocales[language][0])}($factoryArguments);'''); return $generatedClassPrefix${(languageToLocales[language][0]).camelCase()}($factoryArguments);''');
} else if (!languageToScriptCodes.containsKey(language)) { // Does not distinguish between scripts. Switch on countryCode directly. } else if (!languageToScriptCodes.containsKey(language)) { // Does not distinguish between scripts. Switch on countryCode directly.
output.writeln(''' output.writeln('''
case '$language': { case '$language': {
...@@ -279,11 +279,11 @@ $factoryDeclaration ...@@ -279,11 +279,11 @@ $factoryDeclaration
final String countryCode = locale.countryCode; final String countryCode = locale.countryCode;
output.writeln(''' output.writeln('''
case '$countryCode': case '$countryCode':
return $generatedClassPrefix${camelCase(locale)}($factoryArguments);'''); return $generatedClassPrefix${locale.camelCase()}($factoryArguments);''');
} }
output.writeln(''' output.writeln('''
} }
return $generatedClassPrefix${camelCase(LocaleInfo.fromString(language))}($factoryArguments); return $generatedClassPrefix${LocaleInfo.fromString(language).camelCase()}($factoryArguments);
}'''); }''');
} else { // Language has scriptCode, add additional switch logic. } else { // Language has scriptCode, add additional switch logic.
bool hasCountryCode = false; bool hasCountryCode = false;
...@@ -309,7 +309,7 @@ $factoryDeclaration ...@@ -309,7 +309,7 @@ $factoryDeclaration
final String countryCode = locale.countryCode; final String countryCode = locale.countryCode;
output.writeln(''' output.writeln('''
case '$countryCode': case '$countryCode':
return $generatedClassPrefix${camelCase(locale)}($factoryArguments);'''); return $generatedClassPrefix${locale.camelCase()}($factoryArguments);''');
} }
} }
// Return a fallback locale that matches scriptCode, but not countryCode. // Return a fallback locale that matches scriptCode, but not countryCode.
...@@ -321,7 +321,7 @@ $factoryDeclaration ...@@ -321,7 +321,7 @@ $factoryDeclaration
}'''); }''');
} }
output.writeln(''' output.writeln('''
return $generatedClassPrefix${camelCase(scriptLocale)}($factoryArguments); return $generatedClassPrefix${scriptLocale.camelCase()}($factoryArguments);
}'''); }''');
} else { } else {
// Not Explicitly defined, fallback to first locale with the same language and // Not Explicitly defined, fallback to first locale with the same language and
...@@ -334,7 +334,7 @@ $factoryDeclaration ...@@ -334,7 +334,7 @@ $factoryDeclaration
}'''); }''');
} }
output.writeln(''' output.writeln('''
return $generatedClassPrefix${camelCase(scriptLocale)}($factoryArguments); return $generatedClassPrefix${scriptLocale.camelCase()}($factoryArguments);
}'''); }''');
break; break;
} }
...@@ -354,13 +354,13 @@ $factoryDeclaration ...@@ -354,13 +354,13 @@ $factoryDeclaration
final String countryCode = locale.countryCode; final String countryCode = locale.countryCode;
output.writeln(''' output.writeln('''
case '$countryCode': case '$countryCode':
return $generatedClassPrefix${camelCase(locale)}($factoryArguments);'''); return $generatedClassPrefix${locale.camelCase()}($factoryArguments);''');
} }
output.writeln(''' output.writeln('''
}'''); }''');
} }
output.writeln(''' output.writeln('''
return $generatedClassPrefix${camelCase(LocaleInfo.fromString(language))}($factoryArguments); return $generatedClassPrefix${LocaleInfo.fromString(language).camelCase()}($factoryArguments);
}'''); }''');
} }
} }
......
...@@ -38,7 +38,7 @@ ConstructorGenerator generateCupertinoConstructor = (LocaleInfo locale) { ...@@ -38,7 +38,7 @@ ConstructorGenerator generateCupertinoConstructor = (LocaleInfo locale) {
/// Create an instance of the translation bundle for ${describeLocale(localeName)}. /// Create an instance of the translation bundle for ${describeLocale(localeName)}.
/// ///
/// For details on the meaning of the arguments, see [GlobalCupertinoLocalizations]. /// For details on the meaning of the arguments, see [GlobalCupertinoLocalizations].
const CupertinoLocalization${camelCase(locale)}({ const CupertinoLocalization${locale.camelCase()}({
String localeName = '$localeName', String localeName = '$localeName',
@required intl.DateFormat fullYearFormat, @required intl.DateFormat fullYearFormat,
@required intl.DateFormat dayFormat, @required intl.DateFormat dayFormat,
......
...@@ -38,7 +38,7 @@ ConstructorGenerator generateMaterialConstructor = (LocaleInfo locale) { ...@@ -38,7 +38,7 @@ ConstructorGenerator generateMaterialConstructor = (LocaleInfo locale) {
/// Create an instance of the translation bundle for ${describeLocale(localeName)}. /// Create an instance of the translation bundle for ${describeLocale(localeName)}.
/// ///
/// For details on the meaning of the arguments, see [GlobalMaterialLocalizations]. /// For details on the meaning of the arguments, see [GlobalMaterialLocalizations].
const MaterialLocalization${camelCase(locale)}({ const MaterialLocalization${locale.camelCase()}({
String localeName = '$localeName', String localeName = '$localeName',
@required intl.DateFormat fullYearFormat, @required intl.DateFormat fullYearFormat,
@required intl.DateFormat mediumDateFormat, @required intl.DateFormat mediumDateFormat,
......
...@@ -112,6 +112,13 @@ class LocaleInfo implements Comparable<LocaleInfo> { ...@@ -112,6 +112,13 @@ class LocaleInfo implements Comparable<LocaleInfo> {
final int length; // The number of fields. Ranges from 1-3. final int length; // The number of fields. Ranges from 1-3.
final String originalString; // Original un-parsed locale string. final String originalString; // Original un-parsed locale string.
String camelCase() {
return originalString
.split('_')
.map<String>((String part) => part.substring(0, 1).toUpperCase() + part.substring(1).toLowerCase())
.join('');
}
@override @override
bool operator ==(Object other) { bool operator ==(Object other) {
return other is LocaleInfo return other is LocaleInfo
...@@ -220,13 +227,6 @@ void checkCwdIsRepoRoot(String commandName) { ...@@ -220,13 +227,6 @@ void checkCwdIsRepoRoot(String commandName) {
} }
} }
String camelCase(LocaleInfo locale) {
return locale.originalString
.split('_')
.map<String>((String part) => part.substring(0, 1).toUpperCase() + part.substring(1).toLowerCase())
.join('');
}
GeneratorOptions parseArgs(List<String> rawArgs) { GeneratorOptions parseArgs(List<String> rawArgs) {
final argslib.ArgParser argParser = argslib.ArgParser() final argslib.ArgParser argParser = argslib.ArgParser()
..addFlag( ..addFlag(
...@@ -364,7 +364,7 @@ String generateClassDeclaration( ...@@ -364,7 +364,7 @@ String generateClassDeclaration(
String classNamePrefix, String classNamePrefix,
String superClass, String superClass,
) { ) {
final String camelCaseName = camelCase(locale); final String camelCaseName = locale.camelCase();
return ''' return '''
/// The translations for ${describeLocale(locale.originalString)} (`${locale.originalString}`). /// The translations for ${describeLocale(locale.originalString)} (`${locale.originalString}`).
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment