-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app-config): add i18n and its integration with cypress
- Loading branch information
1 parent
990c3c5
commit 5ae5bd2
Showing
16 changed files
with
169 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
/* eslint-disable jest/valid-expect-in-promise */ | ||
/// <reference types="cypress" /> | ||
|
||
context('Index page', () => { | ||
context("Index page", () => { | ||
beforeEach(() => { | ||
cy.visit(Cypress.config().baseUrl) | ||
}) | ||
cy.visit(Cypress.config().baseUrl); | ||
}); | ||
|
||
it('should contain the title', () => { | ||
cy.contains('Pipeline - The Game that Delivers!') | ||
}) | ||
}) | ||
it("should contain the title", () => { | ||
cy.containsTranslation("home.title"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/// <reference types="Cypress" /> | ||
|
||
declare namespace Cypress { | ||
interface Chainable<Subject = any> { | ||
containsTranslation(key: string): Chainable<Subject>; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import I18n from 'i18n-js'; | ||
import enTranslations from '@assets/i18n/en'; | ||
import { reducer, actions, name } from './slice'; | ||
import { useTranslate } from './useTranslate'; | ||
|
||
|
||
export function initializeI18n() { | ||
const defaultLocale = "en-EN"; | ||
|
||
I18n.defaultLocale = defaultLocale; | ||
I18n.locale = defaultLocale; | ||
|
||
I18n.translations[defaultLocale] = enTranslations; | ||
console.log(I18n.translations); | ||
} | ||
|
||
export {reducer, actions, name, useTranslate} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createSelector, createSlice, PayloadAction } from "@reduxjs/toolkit"; | ||
|
||
export interface State { | ||
currentLocale: string; | ||
} | ||
|
||
const initialState = { | ||
currentLocale: "en-EN" | ||
} as State; | ||
|
||
const slice = createSlice({ | ||
name: "i18n", | ||
initialState: initialState, | ||
reducers: { | ||
changeLanguage(state, action: PayloadAction<string>){ | ||
state.currentLocale = action.payload; | ||
} | ||
} | ||
}); | ||
|
||
export const getCurrentLanguage = createSelector( | ||
(state: {[name]: State}) => state[name], | ||
(i18nState) => i18nState.currentLocale | ||
); | ||
|
||
export const reducer = slice.reducer; | ||
export const actions = slice.actions; | ||
export const name = slice.name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { getCurrentLanguage } from './slice'; | ||
import { useSelector } from 'react-redux'; | ||
import { Path } from './utils'; | ||
import enTranslations from '@assets/i18n/en'; | ||
import I18n from 'i18n-js'; | ||
|
||
|
||
|
||
export function useTranslate() { | ||
const currentLanguage = useSelector(getCurrentLanguage); | ||
return translateFactory(currentLanguage); | ||
} | ||
|
||
export function translateFactory(language: string){ | ||
return function translate(key: Path<typeof enTranslations>){ | ||
return I18n.t(key, {locale: language}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
type PathImpl<T, Key extends keyof T> = | ||
Key extends string | ||
? T[Key] extends Record<string, any> | ||
? | `${Key}.${PathImpl<T[Key], Exclude<keyof T[Key], keyof any[]>> & string}` | ||
| `${Key}.${Exclude<keyof T[Key], keyof any[]> & string}` | ||
: never | ||
: never; | ||
|
||
type PathImpl2<T> = PathImpl<T, keyof T>; | ||
|
||
/** | ||
* Type to extract the union of all the nested paths (in "."-notation). | ||
* | ||
* Example: | ||
* | ||
* The object: | ||
* { | ||
* "a": { | ||
* "a1": "valuea1" | ||
* }, | ||
* "b": { | ||
* "b1": "valueb1" | ||
* } | ||
* } | ||
* | ||
* results in the possible paths: | ||
* a.a1 | b.b1 | ||
*/ | ||
export type Path<T> = PathImpl2<T> extends string | keyof T ? PathImpl2<T> : keyof T; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
import {Action, ReducersMapObject} from "@reduxjs/toolkit"; | ||
import {name as i18nName, reducer as i18nReducer} from '@pipeline/i18n'; | ||
|
||
function testReducer(state = {}, action: Action) { | ||
return state; | ||
} | ||
|
||
const reducers: ReducersMapObject = { | ||
test: testReducer | ||
const reducers = { | ||
[i18nName]: i18nReducer | ||
}; | ||
|
||
export default reducers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// eslint-disable-next-line import/no-anonymous-default-export | ||
const translations = { | ||
"home":{ | ||
"title": "Pipeline - The Game that Delivers!", | ||
"subtitle": "Random stuff" | ||
}, | ||
"page2":{ | ||
"test1": "Pipeline - The Game that Delivers!", | ||
"test2": "Random stuff" | ||
} | ||
} | ||
|
||
export default translations; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Store } from "@reduxjs/toolkit"; | ||
import I18n from "i18n-js"; | ||
|
||
/** | ||
* Makes I18n and the store available for Cypress in order | ||
* to use them during the e2e tests. | ||
*/ | ||
export function makeCypressHappy(store: Store) { | ||
(window as any).store = store; | ||
(window as any).i18n = I18n; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters