Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
common
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
npm
common
Commits
5eac1bf0
Commit
5eac1bf0
authored
Nov 09, 2018
by
Vladislav Lagunov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Рефакторинг ~/gettext
parent
1aec7d53
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
3 deletions
+8
-3
gettext/index.ts
+8
-3
No files found.
gettext/index.ts
View file @
5eac1bf0
...
@@ -10,6 +10,7 @@ const CONTEXT_DELIMITER = String.fromCharCode(4);
...
@@ -10,6 +10,7 @@ const CONTEXT_DELIMITER = String.fromCharCode(4);
// Translations
// Translations
export
type
Data
=
Record
<
string
,
Record
<
string
,
string
[]
|
string
>>
;
export
type
Data
=
Record
<
string
,
Record
<
string
,
string
[]
|
string
>>
;
export
type
JedFormat
=
{
locale_data
:
{
messages
:
Record
<
string
,
string
[]
>
}
};
export
type
JedFormat
=
{
locale_data
:
{
messages
:
Record
<
string
,
string
[]
>
}
};
export
type
ES6Module
<
T
>
=
{
default
:
T
};
// Хелпер для перевода
// Хелпер для перевода
...
@@ -22,7 +23,11 @@ export class Translations {
...
@@ -22,7 +23,11 @@ export class Translations {
readonly
data
:
Data
readonly
data
:
Data
)
{}
)
{}
static
concat
(...
webpackContexts
)
{
static
fromJed
(...
data
:
JedFormat
[]):
Translations
;
static
fromJed
(...
data
:
ES6Module
<
JedFormat
>
[]):
Translations
;
static
fromJed
(...
data
:
JedFormat
[][]):
Translations
;
static
fromJed
(...
data
:
ES6Module
<
JedFormat
>
[][]):
Translations
;
static
fromJed
(...
webpackContexts
)
{
const
jedTranslations
:
JedFormat
[]
=
flattenModules
(
webpackContexts
);
const
jedTranslations
:
JedFormat
[]
=
flattenModules
(
webpackContexts
);
return
new
Translations
(
assignJedData
({},
...
jedTranslations
));
return
new
Translations
(
assignJedData
({},
...
jedTranslations
));
...
@@ -34,7 +39,7 @@ export class Translations {
...
@@ -34,7 +39,7 @@ export class Translations {
}
}
}
}
__
(
singular_key
:
string
,
plural_key
?:
string
,
context
?:
string
,
n
?:
number
):
I18nString
{
__
:
(
singular_key
:
string
,
plural_key
?:
string
,
context
?:
string
,
n
?:
number
)
=>
I18nString
=
(
singular_key
,
plural_key
?,
context
?,
n
?)
=>
{
return
localeOrCtx
=>
{
return
localeOrCtx
=>
{
let
locale
=
!
localeOrCtx
?
'en-US'
:
typeof
(
localeOrCtx
)
===
'string'
?
localeOrCtx
:
localeOrCtx
.
locale
;
let
locale
=
!
localeOrCtx
?
'en-US'
:
typeof
(
localeOrCtx
)
===
'string'
?
localeOrCtx
:
localeOrCtx
.
locale
;
locale
=
locale
.
replace
(
/_/
,
'-'
);
locale
=
locale
.
replace
(
/_/
,
'-'
);
...
@@ -75,7 +80,7 @@ export type LocaleCtx = { locale: string };
...
@@ -75,7 +80,7 @@ export type LocaleCtx = { locale: string };
* ```
* ```
*/
*/
export
function
withGettext
(...
webpackContexts
)
{
export
function
withGettext
(...
webpackContexts
)
{
const
translations
:
Translations
=
webpackContexts
[
0
]
instanceof
Translations
?
webpackContexts
[
0
]
:
Translations
.
concat
(...
webpackContexts
);
const
translations
:
Translations
=
webpackContexts
[
0
]
instanceof
Translations
?
webpackContexts
[
0
]
:
Translations
.
fromJed
(...
webpackContexts
);
return
<
P
extends
{
__
:
Gettext
,
ctx
?:
{
locale
:
string
}
}
>
(
Component
:
React
.
ComponentType
<
P
>
)
=>
{
return
<
P
extends
{
__
:
Gettext
,
ctx
?:
{
locale
:
string
}
}
>
(
Component
:
React
.
ComponentType
<
P
>
)
=>
{
class
WithTranslations
extends
React
.
Component
<
Omit
<
P
,
'__'
>>
{
class
WithTranslations
extends
React
.
Component
<
Omit
<
P
,
'__'
>>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment