Skip to content

Commit

Permalink
Add typing to i18n locales
Browse files Browse the repository at this point in the history
  • Loading branch information
mgonzalezg9 committed Apr 22, 2024
1 parent 9b434f9 commit 851835e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
9 changes: 3 additions & 6 deletions src/components/weather/TimeWeather.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const TimeWeather = ({

return (
<DefaultView style={styles.container} {...props}>
<Text style={styles.timeContainer}>{now}</Text>
<Text style={styles.timeWeatherText}>{now}</Text>
<ConditionIcon
width={25}
height={25}
Expand All @@ -30,7 +30,7 @@ const TimeWeather = ({
sunrise={sunrise}
sunset={sunset}
/>
<Text style={styles.temperatureContainer}>{computedTemperature}º</Text>
<Text style={styles.timeWeatherText}>{computedTemperature}º</Text>
</DefaultView>
);
};
Expand All @@ -44,10 +44,7 @@ const styles = StyleSheet.create({
justifyContent: 'space-between',
height: 115,
},
timeContainer: {
fontSize: 16,
},
temperatureContainer: {
timeWeatherText: {
fontSize: 16,
},
});
8 changes: 6 additions & 2 deletions src/i18n/locales/en.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import { SweatherLocale } from "./types";

const en: SweatherLocale = {
appTitle: 'Sweather',
nameCity: 'Name your town or city',
sampleCityInput: 'Eg. London',
Expand All @@ -23,4 +25,6 @@ export default {
Ash: "Ash",
Tornado: "Tornado",
}
}
};

export default en;
8 changes: 6 additions & 2 deletions src/i18n/locales/es.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import { SweatherLocale } from "./types";

const es: SweatherLocale = {
appTitle: 'Sweather',
nameCity: 'Nombre de la ciudad o pueblo',
sampleCityInput: 'Ej. Barcelona',
Expand All @@ -23,4 +25,6 @@ export default {
Ash: 'Ceniza',
Tornado: 'Tornado',
}
}
};

export default es;
26 changes: 26 additions & 0 deletions src/i18n/locales/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export type SweatherLocale = {
appTitle: string,
nameCity: string,
sampleCityInput: string,
weatherServiceError: string,
locationDisabledError: string,
poweredBy: string,
conditions: {
Clear: string,
Clouds: string,
Smoke: string,
Fog: string,
Haze: string,
Mist: string,
Rain: string,
Squall: string,
Drizzle: string,
Snow: string,
Thunderstorm: string,
Wind: string,
Dust: string,
Sand: string,
Ash: string,
Tornado: string,
}
}
5 changes: 3 additions & 2 deletions src/logic/error/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { SweatherLocale } from "@/i18n/locales/types";

export enum SweatherErrorCode {
USER_LOCATION_DENIED = 'USER_LOCATION_DENIED',
WEATHER_SERVICE_ERROR = 'WEATHER_SERVICE_ERROR',
WALLPAPER_SERVICE_ERROR = 'WALLPAPER_SERVICE_ERROR',
}

// Map matching error codes with i18n locales
export const SWEATHER_ERROR_MESSAGE_MAP: Record<SweatherErrorCode, string> = {
export const SWEATHER_ERROR_MESSAGE_MAP: Partial<Record<SweatherErrorCode, keyof SweatherLocale>> = {
[SweatherErrorCode.USER_LOCATION_DENIED]: 'locationDisabledError',
[SweatherErrorCode.WEATHER_SERVICE_ERROR]: 'weatherServiceError',
[SweatherErrorCode.WALLPAPER_SERVICE_ERROR]: 'wallpaperServiceError'
} as const;
2 changes: 1 addition & 1 deletion src/screens/LocationRequest/ErrorSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const ErrorSection: React.FC<ErrorSectionProps> = ({ error }) => {

return (
<Text style={styles.errorText}>
{i18n.t(textKey)}
{textKey && i18n.t(textKey)}
</Text>
)
};
Expand Down

0 comments on commit 851835e

Please sign in to comment.