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
b4fc2890
Commit
b4fc2890
authored
Nov 29, 2018
by
Vladislav Lagunov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[gettext] Испралена обработка формата po2json
parent
76e213e1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
22 deletions
+16
-22
gettext/index.ts
+16
-22
No files found.
gettext/index.ts
View file @
b4fc2890
...
...
@@ -8,8 +8,8 @@ const CONTEXT_DELIMITER = String.fromCharCode(4);
// Translations
export
type
Data
=
Record
<
string
,
Record
<
string
,
string
[]
|
string
>>
;
export
type
JedFormat
=
{
locale_data
:
{
messages
:
Record
<
string
,
string
[]
>
}
}
;
export
type
Translations
Data
=
Record
<
string
,
Record
<
string
,
string
[]
|
string
>>
;
export
type
POData
=
Record
<
string
,
string
[]
|
string
>
;
export
type
ES6Module
<
T
>
=
{
default
:
T
};
...
...
@@ -20,16 +20,16 @@ export type I18nString = (locale?: string|LocaleCtx|null) => string;
export
class
Translations
{
constructor
(
readonly
data
:
Data
readonly
data
:
TranslationsData
,
)
{}
static
from
Jed
(...
data
:
JedFormat
[]):
Translations
;
static
from
Jed
(...
data
:
ES6Module
<
JedFormat
>
[]):
Translations
;
static
from
Jed
(...
data
:
JedFormat
[][]):
Translations
;
static
from
Jed
(...
data
:
ES6Module
<
JedFormat
>
[][]):
Translations
;
static
from
Jed
(...
webpackContexts
)
{
const
jedTranslations
:
JedFormat
[]
=
flattenModules
(
webpackContexts
);
return
new
Translations
(
assign
JedData
({},
...
jedTranslations
));
static
from
PO
(...
data
:
POData
[]):
Translations
;
static
from
PO
(...
data
:
ES6Module
<
POData
>
[]):
Translations
;
static
from
PO
(...
data
:
POData
[][]):
Translations
;
static
from
PO
(...
data
:
ES6Module
<
POData
>
[][]):
Translations
;
static
from
PO
(...
webpackContexts
)
{
const
podata
:
POData
[]
=
flattenModules
(
webpackContexts
);
return
new
Translations
(
assign
Data
({},
...
podata
));
function
flattenModules
(
xs
:
any
[])
{
return
Array
.
prototype
.
concat
.
apply
([],
xs
.
map
(
x
=>
{
...
...
@@ -39,7 +39,7 @@ export class Translations {
}
}
__
:
(
singular_key
:
string
,
plural_key
?:
string
,
context
?:
string
,
n
?:
number
)
=>
I18nString
=
(
singular_key
,
plural_key
?,
context
?,
n
?)
=>
{
__
:
(
singular_key
:
string
,
context
?:
string
,
plural_key
?:
string
,
n
?:
number
)
=>
I18nString
=
(
singular_key
,
context
?,
plural_key
?,
n
?)
=>
{
return
localeOrCtx
=>
{
let
locale
=
!
localeOrCtx
?
'en-US'
:
typeof
(
localeOrCtx
)
===
'string'
?
localeOrCtx
:
localeOrCtx
.
locale
;
locale
=
locale
.
replace
(
/_/
,
'-'
);
...
...
@@ -80,7 +80,7 @@ export type LocaleCtx = { locale: string };
* ```
*/
export
function
withGettext
(...
webpackContexts
)
{
const
translations
:
Translations
=
webpackContexts
[
0
]
instanceof
Translations
?
webpackContexts
[
0
]
:
Translations
.
from
Jed
(...
webpackContexts
);
const
translations
:
Translations
=
webpackContexts
[
0
]
instanceof
Translations
?
webpackContexts
[
0
]
:
Translations
.
from
PO
(...
webpackContexts
);
return
<
P
extends
{
__
:
Gettext
,
ctx
?:
{
locale
:
string
}
}
>
(
Component
:
React
.
ComponentType
<
P
>
)
=>
{
class
WithTranslations
extends
React
.
Component
<
Omit
<
P
,
'__'
>>
{
...
...
@@ -104,27 +104,21 @@ export function withGettext(...webpackContexts) {
/**
* Добавление переводов из `srcs` в `dst`
*/
function
assign
JedData
(
dst
:
Data
,
...
srcs
:
JedFormat
[]):
Data
{
function
assign
Data
(
dst
:
TranslationsData
,
...
srcs
:
POData
[]):
Translations
Data
{
srcs
.
forEach
(
data
=>
{
// @ts-ignore
const
locale_
=
data
[
''
].
language
||
data
[
''
].
lang
||
data
[
''
].
locale
;
if
(
!
locale_
||
typeof
(
locale_
)
!==
'string'
)
return
;
const
locale
=
locale_
.
replace
(
/_/g
,
'-'
);
const
podata
=
data
[
'%podata'
];
for
(
const
k
of
Object
.
keys
(
data
))
{
if
(
k
===
''
||
k
===
'%podata'
)
continue
;
if
(
k
===
''
)
continue
;
dst
[
locale
]
=
dst
[
locale
]
||
{};
if
(
Array
.
isArray
(
data
[
k
])
&&
data
[
k
][
0
]
===
null
)
{
if
(
Array
.
isArray
(
data
[
k
]))
{
dst
[
locale
][
k
]
=
data
[
k
].
slice
(
1
);
if
(
dst
[
locale
][
k
].
length
===
1
)
dst
[
locale
][
k
]
=
dst
[
locale
][
k
][
0
];
}
else
{
dst
[
locale
][
k
]
=
data
[
k
];
}
}
if
(
podata
)
for
(
const
explicit
of
podata
)
{
const
key
=
explicit
.
msgctxt
?
explicit
.
msgctxt
+
CONTEXT_DELIMITER
+
explicit
.
msgid
:
explicit
.
msgid
;
dst
[
locale
]
=
dst
[
locale
]
||
{};
dst
[
locale
][
key
]
=
explicit
.
msgstr
;
}
});
return
dst
;
}
...
...
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